10

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?

2
  • I was trying something along the lines of: docker ps | tail -n +2 | awk '{print $2;}' | while read image; do docker history $image | awk '{print $1;}'| tail -n +2 ; done | sort| uniq I thought this would have listed all images referenced by the currently running containers, but it doesn't seem to. Commented Jul 8, 2015 at 9:51
  • 1
    if 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) Commented Jul 8, 2015 at 10:55

3 Answers 3

11

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}}"
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what's needed and w/o resorting to parsing of textual stdout output. :thumbs_up:
1

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.

Comments

0

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)

2 Comments

If the image has a nice name it will just output the name of the image, not the ID.
So let's print the Image name and the ID docker inspect -f '{{ .Config.Image}} {{ .State.Pid}}' $(docker ps -q) Is that what you want?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.