Matthew Buckett
Matthew Buckett

Reputation: 4371

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?

Upvotes: 10

Views: 15422

Answers (3)

Martin Luther ETOUMAN
Martin Luther ETOUMAN

Reputation: 174

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}}"

Upvotes: 10

Pedro Lopez
Pedro Lopez

Reputation: 2276

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.

Upvotes: 1

user2915097
user2915097

Reputation: 32176

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)

Upvotes: 0

Related Questions