5

Perhaps kill is incorrect terminology, or I'm at least using it incorrectly.

How do I destroy docker containers? Put them in the trash. Delete. Remove. Erase.

docker container kill $(docker ps -a -q  | awk '{print $1}')

doesn't quite work:

Error response from daemon: Cannot kill container: 06e44d24ba8d: Container 06e44d24ba8d3df83e10a1829f04755cb9053faa68ab3dabccd10c3ee1c80322 is not running
Error response from daemon: Cannot kill container: 22539a069e3b: Container 22539a069e3b8bbbd27b062603a7667857ac1a5e64242004fe7b72411a9cdbca is not running
Error response from daemon: Cannot kill container: 1f7cf43ab398: Container 1f7cf43ab398e6e18416f69fc947b49d9792b1932a4b157f0514f9f6638ca185 is not running
Error response from daemon: Cannot kill container: 7fc8c24ccabe: Container 7fc8c24ccabef04215df9ba7b8c171df6b963942e6e90c92a462cc0ab9d5dcb0 is not running

See also:

https://stackoverflow.com/q/51015597/4531180

1 Answer 1

7

docker kill will not destroy containers, it will just kill (stop) the running containers. But docker ps -a will show also the non-running containers, so you get an error for all these non-running containers.

Leave out the -a to kill only all running containers:

docker kill $(docker ps -q)

Use docker stop instead of docker kill if you want the processes to first try to terminate themselves (SIGTERM) instead of killing directly (SIGKILL).

Or use docker rm to remove (destroy) containers:

docker rm --force $(docker ps -a -q)

--force or -f is needed to also rm running containers.

Note:
this will remove all containers.

Use docker container prune to remove all stopped containers.


Please read the docs or run docker help to get more information about all the commands.

2
  • what's the distinction then between docker kill and docker stop in relation to your answer? (Yes, I'll RTFM -- but thanks for the answer.) Aug 4, 2020 at 7:37
  • 1
    The difference is: stop: The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL. and for kill: The main process inside the container is sent SIGKILL
    – pLumo
    Aug 4, 2020 at 7:41

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.