Docker can run multiple containers at the same time. In some cases, we may need to stop all currently running containers in an easy way with a single command. In this tutorial, we provide different ways to stop all docker containers.
Stop All Docker Containers with “docker stop”
Docker provides the docker stop
command in order to stop provided containers. The “docker stop” command syntax requires where the docker name is provided. We will use the docker ps -q
command in order to provide all running docker names to the “docker stop” command.
$ docker stop $(docker ps -q)
The -q
flag only lists the docker containing IDs and this ID list is provided to the “docker stop” command. The “docker stop” command stops all provided IDs.
Stop All Docker Containers with “docker kill”
Another way to stop all running docker containers is using the docker kill
command. Actually, the “docker kill” command is very similar to the “docker stop” command where the “docker top” command sends SIGTERM
but the “docker kill” command sends SIGKILL
signal for all running docker containers.
$ docker kill $(docker ps -q)
Check If All Docker Containers Stopped
After using the “docker kill” or “docker stop” commands to stop all running containers we can check if all dockers are stopped properly by using the “docker ps” command.
$ docker ps
Stop All Docker Containers with Specified Name
In some cases, we may need to stop all running docker containers with a specific name. For example, if we need to stop all docker containers those names contain “postgre” we can use the following command where --filter
option is used to filter running containers according to their names.
$ docker kill $(docker ps -q --filter "name=postgre")
Remove All Docker Containers
All existing docker containers can be removed with the docker rm
command. The container should be in a stopped state which is described above.
$ docker rm $(docker ps -a -q)
Remove All Docker Container Images
After stopping running containers we may need to remove all docker images as we do not need them anymore. The docker rmi
command can be used to remove all docker images. The docker images
command is used to list all containers and the -q
option is provided to list only container IDs.
$ docker rmi $(docker images -q)