Remove docker images by repository - docker

I have a lot of docker images from the same repository, for example, docker.io/mycompany/myimage.
How can I delete all images from a particular repository?

docker images --format '{{.Repository}}:{{.Tag}}' | grep "^docker.io/mycompany/" | xargs -r docker rmi
To see the images that will be deleted without actually deleting them:
docker images --format '{{.Repository}}:{{.Tag}}' | grep "^docker.io/mycompany/" | xargs -r echo docker rmi

Related

How to remove <none> images after building successfull

After I build my image, there are a bunch of images. When I try to delete them I get “image has dependent child images” errors. Is there anyway to clean this up?
These do NOT work:
docker rmi $(docker images -q)
docker rmi $(docker images | grep “^” | awk “{print $3}”)
docker rmi $(docker images -f “dangling=true” -q)
docker rmi `docker images | grep "<none>" | awk {'print $3'}`
Try this:
docker rmi $(docker images | grep none | awk '{print $3}')
You can add -f to force image removal but I do not recommend doing so. If you cannot remove the images with the command above that means it is in partially in use or stopped containers are using the images you are trying to remove.
You can always check the stopped or Existed containers with
docker ps -a
Here, the Status column will indicate the container status.
docker rmi docker images -a | grep "<none>" | awk {'print $3'}
Adding -a is required.

Get the last Docker image built

This command gives a list of image and container IDs ordered from top to bottom by last build time:
docker images
All my docker images are appended with the hash git head commit.
The results
REPOSITORY TAG IMAGE ID CREATED SIZE
username/play-table-of-contents-1474f94 latest 6141b8177c2f 34 minutes ago 149MB
username/play-table-of-contents-2616f5f latest 2b5422dd91ba About an hour ago 149MB
Is there a way to get only the last image by name ? ( ie: case 6141b8177c2f )
I tried with
docker images --format "{{.Names}}"
My end goal is to run the last docker image built. To do this, I need to
get the last image name in bash script variable.
docker run ... $last_image ...
Docker command docker images list out most recently created images.
The following command list out the first image from the above list. I believe you are looking for this command.
docker images | awk '{print $1}' | awk 'NR==2'
You would probably deploy a container of the image from above command
docker run $(docker images | awk '{print $1}' | awk 'NR==2')
All the other answers' solution relies on the fact docker image sorts the output by date already. This may not be true. A consistent solution would be to sort them by the creation date and get the latest one. I used the following command, this is consistent.
docker images --format "{{.ID}} {{.CreatedAt}}" | sort -rk 2 | awk 'NR==1{print $1}'
This command sorts the output of the docker images command by CreatedAt column and print the id of the latest image
Short Answer
docker run ... $(docker ps -a --format "{{.Names}}" | head -1) ...
docker ps -a return the stopped and running containers in the order "Last to First".
Powershell
docker images --format "{{.ID}}" | select -first 1
example use with docker run:
docker run -it (docker images --format "{{.ID}}" | select -first 1)
Bash
docker images --format='{{.ID}}' | head -1
example use with docker run:
docker run -it $(docker images --format='{{.ID}}' | head -1)
This returns the IMAGEID of the latest built docker image:
docker images -q --format='{{.ID}}' | head -1
You can even collect it in a variable and use it as you like:
IMAGE_ID=$(docker images -q --format='{{.ID}}' | head -1)
If you want to enter the last docker image you ran :
docker run -it $(docker images | awk '{print $3}' | awk 'NR==2') /bin/sh
OR
docker run -it $(docker images | awk '{print $3}' | awk 'NR==2') bash
if you need get last build from an specific image name.. you can do:
IMG_NAME="my-image-name"
IMG_LAST_BUILD=$(docker images | grep $IMG_NAME | awk 'NR==1{printf("%s\:%s",$1,$2)}')
echo $IMG_LAST_BUILD
#my-image-name:tag_version

How to free wasted space used by docker?

I set up docker on my server. I use Jenkins for CI. Those tools are really cool. But, unfortunately, I still can't figure out how to get the wasted space back.
I tried different things proposed on the web and by using them I can only free a part of the wasted space. I need no any history and etc.
I tried different things, like rude docker rm $(docker ps -aq) and docker rmi $(docker images -q) while some of containers are running that make them prevented from deletion.
I also tried this script and similar.
#!/bin/bash
# remove exited containers:
docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v
# remove unused images:
docker images --no-trunc | grep '<none>' | awk '{ print $3 }' | xargs -r docker rmi
# remove unused volumes:
find '/var/lib/docker/volumes/' -mindepth 1 -maxdepth 1 -type d | grep -vFf <(
docker ps -aq | xargs docker inspect | jq -r '.[] | .Mounts | .[] | .Name | select(.)'
) | xargs -r rm -fr
I would like to expect that after first build of all images I have fixed size of the stuff by applying the clean up commands above. In other words I want the subsequent builds with clean up applied doesn't make the wasted space grow.
But anyway every execution of docker build occupies new space that I can't free up.
The directory /var/lib/docker/overlay grows up and doesn't consider my clean ups eventually.
Where am I wrong?
In the latest versions of Docker, you can now use docker system prune command (with --all option for complete cleanup).
https://docs.docker.com/engine/reference/commandline/system_prune/
In order to remove volumes use:
docker volume prune
In order to remove networks use:
docker network prune
In order to remove all stopped containers use:
docker container prune
In order to remove unused images use:
docker image prune

Find the docker containers using an image?

If I have the ID of an image how can I find out which containers are using this image? When removing an image that is still used you get an error message:
$ docker rmi 77b0318b76b3
Error response from daemon: Conflict, cannot delete 77b0318b76b3 because the container 21ee2cbc7cec is using it, use -f to force
But how could I find this out in an automated way without trying to remove it?
You can try this:
docker ps -a | grep `docker images | grep IMAGE_ID | awk '{print $1":"$2}'` | awk '{print $1}'
With docker ps -a you get the list of all the containers including
the ones you are interested in.
The problem is that you have the IMAGE NAME there but you need the IMAGE ID.
You can use docker images to get the IMAGE ID for a given IMAGE NAME and that is what you use
in your grep to filter by your IMAGE ID.
Finally you get the first column to show only the CONTAINER IDs.
Example:
docker ps -a \
| grep `docker images | grep 3a041c1b0a05 | awk '{print $1":"$2}'` \
| awk '{print $1}'
Output:
4d6fb8a7149f
2baa726b1aa5
Hope this helps.
This will list all containers using your $IMAGE_ID.
$IMAGE_ID can be 77b0318b76b3 or namespace/repo:tag.
E.g.: $IMAGE_ID="77b0318b76b3"
docker container ls --all --filter=ancestor=$IMAGE_ID --format "{{.ID}}"
f you want to list the images of the active containers docker inspect -f '{{ .Config.Image}}' $(docker ps -q) and for all the containers docker inspect -f '{{ .Config.Image}}' $(docker ps -qa)

how to physically remove untagged docker images

when I run a command such as sudo docker rmi me/myimage I get the responce ...image untagged, but, when I rerun sudo docker images I can see that this "untagged" image is still there, and, if I run df -h I can see that the actual files still exist and occupy the file system space.
What command or procedure can I use to physically remove the unneeded images?
You should be able to remove untagged Docker images using the "dangling=true" flag:
sudo docker rmi $(sudo docker images -f "dangling=true" -q)
source:
https://docs.docker.com/engine/reference/commandline/images/
First you need to remove exited containers, then remove dangling images.
docker rm $(docker ps -q -f status=exited)
docker rmi $(docker images -q -f dangling=true)
After all, I created the below script as ~/bin/dclean and have been using it.
#!/bin/sh
processes=$(docker ps -q -f status=exited)
if [ -n "$processes" ]; then
docker rm $processes
fi
images=$(docker images -q -f dangling=true)
if [ -n "$images" ]; then
docker rmi $images
fi
If John Petrone solution doesn't work, try removing those images referring explicitly the IMAGE ID you see when you run docker images. You can remove all of them with one command
for i insudo docker images | grep \ | awk '{print $3}'; do sudo docker rmi $i; done
PD: I don't know John Petrone answer. It works nicely with Docker 1.4.1
This command will remove all the dangling images and containers from docker.
docker system prune -f
you can delete single images by their image id...
docker images
docker rmi <image-id>
This commands also work
docker rmi $(docker images | grep "^<none>" | awk '{print $3}')
Delete images with force to forgo stopped containers that might be using image
docker rmi -f $(docker images | grep "^<none>" | awk '{print $3}')
In my case, i have removed the untagged image by the
below command
# find untagged images
IMAGE_IDS=$(sudo docker images | grep "^<none>" | awk '{print $"3"}')
# in case of no untagged images found do nothing
if [ -n "$IMAGE_IDS" ]; then
sudo docker rmi $IMAGE_IDS > /dev/null 2>&1
fi

Resources