1

I have a named container that may or may not be up and if it is up I want to be able to kill all the containers but that one by name. Basically what I want is:

docker kill $(docker ps -aq --filter="name!=<container-name>")

sadly the internal docker ps command here raises the error:

Error response from daemon: Invalid filter 'name!'

I tried getting a similar result using grep -v:

docker kill $(docker ps -q | grep -v $( docker ps -q -f name=<container-name>))

The problem is that if the container is not up (which very well may be) then the "docker ps -q -f name=" returns nothing and grep doesn't work with no pattern so this entire thing fails.

Any suggestions?

Itay Weiss
  • 133
  • 5
  • Looks like as a one-liner this is only possible with zsh: https://unix.stackexchange.com/questions/325679/bash-one-liner-set-variable-to-output-of-command-or-to-default-value-if-output – Gerald Schneider Sep 11 '19 at 12:30

1 Answers1

0

From the documentation, filter only supports the type=value notation with the ps command. To handle the scenario where the container may not exist, you can use an if/else:

if [ -n "$( docker ps -q -f name=<container-name>)" ]; then
  docker kill $(docker ps -q | grep -v $( docker ps -q -f name=<container-name>))
else
  docker kill $(docker ps -q)
fi
BMitch
  • 5,966
  • 1
  • 25
  • 32