Reputation: 4371
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
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
Reputation: 2276
You can try this:
docker ps -a | grep `docker images | grep IMAGE_ID | awk '{print $1":"$2}'` | awk '{print $1}'
Example:
docker ps -a \
| grep `docker images | grep 3a041c1b0a05 | awk '{print $1":"$2}'` \
| awk '{print $1}'
Output:
4d6fb8a7149f
2baa726b1aa5
Hope this helps.
Upvotes: 1
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