How to filter docker ps by *exact* name?

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?

docker ps -f name=foo | grep -w foo

should do it!

-Joe

1 Like

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).

1 Like

For a container named foo:

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

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

1 Like

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

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.

2 Likes

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.

docker ps -a --format "table {{.Names}}" | grep <EXACT_NAME>

Iā€™d add the -w flag to grep, to limit matches to words (or use ^<EXACT_NAME>$), for otherwise <EXACT_NAME> may still yield a partial match to another longer name? But like noted in an earlier answer:

So, without grep, if for some reason one only wants the container name (which, of course, will simply match what is searched for, just like with the grep example), this may suffice:

docker ps -a --format "{{.Names}}" --filter name=^/<EXACT_NAME>$

(Not sure about having multiples names for a single container?)

Hello I am trying to do something like this but be able to put it into a variable

exclude_containers_to_stop=(
  "MariaDB"
  "Redis"
)
#Suppose to get the ID of the containers from the list above (Doesn't work) Still need to get the ID only as well 
mapfile -t container_names_IDs < <(docker ps --filter 'name="${exclude_containers_to_stop[@]}"')
# Gets the ID's of all docker containers
mapfile -t container_ids < <(docker container ls -q 2>/dev/null)

The containers names are as typed above I manually checked it with docker ps and copied and paste them.
The problem is when I run this command I donā€™t get any output. My goal is to get this to get the container IDā€™s of those with the names above and the ability to add more as needed. I then need to match them something like the sample code below:

if [["${container_ids[@]}" == "${container_names_IDs [@]}"; then
       docker container stop X
else
       echo "Container X"
fi

Again I am not sure if the code above is the best or even possible to do it that way.
Any help would be great.

You can use a variable, but

  • You are using an array which you canā€™t use as a filter. You need to convert that array to a regex.
  • the filter canā€™t contain a quoatation mark. By using apostrophes around the filter the quotation marks will be added to the string which is not in the image name. Not to mention that you canā€™t use a variable between apostrophes in shell.

This should work

--filter "name=^($(echo "${exclude_containers_to_stop[*]}" | tr ' ' '|'))$"

Without the variable it would look like this:

--filter "name=^(MariaDB|Redis)$"

But if you can label the containers, you could do it easier. For example you would use a ā€œstoppable=1ā€ label or something like that and filter to labels.

On the other hand, in your case a simple grep would be even more easier:

docker ps -a --format '{{ .Names }}' \
  | grep -E -v "^($(echo "${exclude_containers_to_stop[*]}" | tr ' ' '|'))$" \
  | xargs -I '{}' -- docker stop '{}'

This would not just filter to the containers but stop all the containers which are not named as MAriaDB or Redis

1 Like

Thank you so much. I am by no means good at this. I will give that a try. Any other tips you can think of or help would be appreciated!

It should pretty much solve your whole problem, so no, I donā€™t have more tips unless you have more questions. :slight_smile:

I have tried adding it to a script I am trying to modify and want to know your thoughts. It is an rsync script that backups my computer but I am having to modify it a lot for my needs, do you think this will work? The original code it commented out.

if [[ "$remote_dst_path" ]] || [[ ! "$ssh_login" ]]; then
      mapfile -t container_ids < <(docker container ls -q 2>/dev/null)
    fi
    if [[ ${#container_ids[@]} -gt 0 ]]; then
        echo "Stop containers:"
        #Changed Command
        docker ps -a --format '{{ .Names }}'   | grep -E -v "^($(echo "${exclude_containers_to_stop[*]}" | tr ' ' '|'))$"   | xargs -I '{}' -- docker stop '{}'
        # docker container stop "${container_ids[@]}"

This topic is about using the docker ps command and filter the list of containers by name. Not to mention that it was already marked as solved, so I wouldnā€™t start to discuss backup strategies and bash syntax. You can try the script in a test environment and see if it works or not. Search for ā€œcheck if variable is emptyā€ on Google. If you want to discuss it further, please open a new topic,. Thank you.

1 Like