Docker Community Forums

Share and learn in the Docker community.

How to filter docker ps by *exact* name?


(Marius311) #1

docker ps -f name=foo returns all containers whose name contains foo. What if I want to just ps on the container whose name is exactly foo?


(Joe) #2
docker ps -f name=foo | grep -w foo

should do it!

-Joe


(Marius311) #3

Ah thanks. And docker ps | awk '{print $NF}' | grep -w foo to be safer in case one of the other columns matches (and removing Docker’s filtering which you don’t really need).


(Danrichards) #4

For a container named foo:

docker ps -a --no-trunc --filter name=^/foo$

(Bruno Bronosky) #5

This really should be noted at https://docs.docker.com/engine/reference/commandline/ps/


(Namgivu) #6

May you explain what ^/ means? Thank you.l


(Pbatey) #7

It’s regular expression syntax. The caret(^) at the beginning is to match the beginning of the name rather than anywhere in the name and the dollar($) at the end means to match the end of a line.

It appears that as far as search filters are concerned names are prefixed with a slash(/) - so ^/ is required to match the start of a name.


(Baibhav) #9

docker ps -a | awk '{print $NF}' | grep -w "$containerName" | cat

When you are using in scripts, use this instead. As grep being at the end of pipeline causes the script to exit, so accepted solution didnt workout for me.
Also docker filter is bad, use awk instead, cleaner approach if you want to match exact names. I had containers with names like $containerName-old4. Docker filter will pick these as well.