Said  Saifi
Said Saifi

Reputation: 2398

How can I check how many containers are running a certain docker image?

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

Answers (4)

user2915097
user2915097

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

Its not blank
Its not blank

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

Dinesh Reddy
Dinesh Reddy

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

Michael
Michael

Reputation: 8663

How about

docker ps | grep imagename | wc -l

Upvotes: 26

Related Questions