Find the docker containers using an image? - docker

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)

Related

How to docker remove all containers based on image name

My docker sometimes create randomw container name based on my docker image e.g. yeeyi
How to docker rm all off the containers where the image is yeeyi?
is there something like? docker rm all --image yeeyi in a single command line?
You can do this using this command:
docker rm $(docker ps -a -q --filter "ancestor=ubuntu")
replace ubuntu with your image name.
This basically gets all the container ids (running or otherwise) that use the image ubuntu and then removes them.
Try the below workaround(update the grep string according to your need);
docker ps --filter "status=exited" | grep yeeyi
Check the output of above command, if you have the correct list, then use the below command;
docker rm (docker ps --filter "status=exited" | grep yeeyi | awk '{print $1}')
Another option is to check the exit code of such randomly created container. If that code is different then rest you can use the below command to get list of such containers;
docker ps -a --filter "exited=<status code>"

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

Remove all Docker containers except one

I have script that stops containers and then removes them
docker stop $(docker ps -q)
docker rm $(docker ps -a -q)
But I don't want to remove the docker container with name "my_docker".
How can I remove all containers except this one?
You can try this, which will
Filter out the unwanted item (grep -v), and then
returns the first column, which contains the container id
Run this command:
docker rm $(docker ps -a | grep -v "my_docker" | awk 'NR>1 {print $1}')
To use cut instead of awk, try this:
docker rm $(docker ps -a | grep -v "my_docker" | cut -d ' ' -f1)
Examples for awk/cut usage here: bash: shortest way to get n-th column of output
The title of the question asks for images, not containers. For those stumbling across this question looking to remove all images except one, you can use docker prune along with filter flags:
docker image prune -a --force --filter "label!=image_name"
replacing image_name with the name of your image.
You can also use the "until=" flag to prune your images by date.
This is what's actually happening docker rm $(List of container Ids). So it's just a matter of how you can filter the List of Container Ids.
For example: If you are looking to delete all the container but one with a specific container Id, then this docker rm $(docker ps -a -q | grep -v "my_container_id") will do the trick.
I achieved this by the following command:
docker image rm -f $(docker images -a | grep -v "image_repository_name" | awk 'NR>1 {print $1}')
For those, who want to exclude more than 1 container just add
grep -v "container_name2" |
after the grep -v "container_name1" command.
The final command might look like
docker rm $(docker ps -a | grep -v "my_docker1" | grep -v "my_docker2" | cut -d ' ' -f1)
I would prefer to test the container name using something along the lines of (untested)
docker inspect --format '{{ .Name }}' $(docker ps -aq)
this will give the names of the (running or not) containers, and you can filter and
docker rm
using this information
Using docker ps with --filter name=<my_docker>
docker rm $(docker ps -a -q | grep -v `docker ps -a -q --filter "name=my_docker"`)
Old question, but I like reviving posts.
For such case you could use Spotify's Docker GC: https://github.com/spotify/docker-gc#excluding-containers-from-garbage-collection
You could do:
echo "my_docker" >> /tmp/docker-gc-exclude-containers
echo '*' > /tmp/docker-gc-exclude
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc:ro -v /tmp/docker-gc-exclude-containers:/etc/docker-gc-exclude-containers:ro -v /tmp/docker-gc-exclude:/etc/docker-gc-exclude:ro spotify/docker-gc
(if you would like to get your images cleaned off too, you can avoid mounting the docker-gc-exclude file)
To stop
docker stop $(docker ps -a -q | grep -v "my_container_id")
To remove
docker rm $(docker ps -a -q | grep -v "my_container_id")
Deletes all the images except node image
sudo docker rmi -f $(sudo docker image ls -a | grep -v "node" | awk 'NR>1 {print $3}')
I know its little late response but it can help any windows user. Here is the command I prepared:
docker rmi $(
docker image list -a --no-trunc --format "table {{.ID}},{{.Repository}}" |
Where-Object {
$_ -NOTMATCH "mcr.microsoft.com/dotnet/core/aspnet" -and
$_ -NOTMATCH "ubuntu" -and
$_ -NOTMATCH "busybox" -and
$_ -NOTMATCH "alpine"
} | Select-Object -Skip 1 | %{ $_.Split(',')[0];}
)
Maybe you can start all the containers you don't want to prune. Then, run:
docker container prune -a

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