Reputation: 2398
I want to check how many containers are running an image, I could do that using docker ps --filter ancestor ="imagename"
and then count the number of containers.But my machine does not support this command is there another way to do it?
Upvotes: 13
Views: 31583
Reputation: 32176
Can you use something like
docker inspect --format='{{.Container.Spec.Image}}' $(docker ps -q)
and test the image returned by this command?
UPDATE:
How about combining this with the accepted answer (as of Mar-2022)? Into something like this:
docker inspect --format='{{.Config.Image}}' $(docker ps -q) | grep imagename | wc -l
Upvotes: 3
Reputation: 3095
The accepted answer didnt work for me nor did https://stackoverflow.com/users/2915097/user2915097
So I kinda tweaked it;
docker ps -q | wc -l
All the answers basically try to get the list and do a line count.
Upvotes: 32
Reputation: 1752
You can also try this
docker ps -f ancestor="imagename" --format '{{.Names}}' | wc -l
If you want to print the containers info and then the count
docker ps -f ancestor="imagename" --format '{{.Names}}' | tee >(wc -l)
Upvotes: 0