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?


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>))
  docker kill $(docker ps -q)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.