Docker allows us to use the Docker rm and Docker stop commands to remove or stop one or more containers. However, if you want to stop and remove all the containers simultaneously, you can combine sub-commands to list all containers with the Docker stop and remove commands. Moreover, we can only remove those containers that are not actively running in our host machine.
Hence, it’s very necessary to stop all the containers before we try to remove them. We can either use the force option along with the Docker rm command to remove all containers forcefully or first stop all Docker containers and then remove them. We can also use the Docker kill command along with a sub-command to kill all the containers simultaneously.
Also, check out our complete and free Docker Tutorials.
How to Stop all Docker Containers?
The Docker command to stop one or more containers is –
$ docker stop [OPTIONS] CONTAINER [CONTAINER...]
We can use the –time option to provide a grace period before stopping a container. To remove all the containers simultaneously, we need to tweak this command a bit. We can use a subcommand in place of the container that will list all the container IDs. This will help us to stop all Docker containers.
$ docker stop $(docker ps -a -q)
In the above command, we have used the Docker stop command. In place of the container name, we have used another sub-command called docker ps along with options such as a and q. The -a option stands for all and is used to list all containers. The -q option is known as quiet and it lists only the container IDs. Let’s try to execute this command.
We can use the docker ps command to list all the active containers first.
$ docker ps
Now, we can use the above-discussed docker stop command.
You can see that we have successfully stopped all containers simultaneoulsy.
How to stop and remove all Docker containers?
We can use a similar method to remove all Docker containers together. Along with the Docker rm command, we can use a sub-command that will list all the Docker container’s ID. However, to remove Docker containers, we need to make sure that none of them are running actively. For that, either we can first stop all containers and execute this command. Or we can use the force option to remove all containers forcefully. Let’s try to do the second one.
$ docker rm -f $(docker ps -a -q)
How to Kill all Docker Containers?
The Docker kill command helps us to kill all the processes running inside Docker containers. We can use the Docker kill command along with a sub-command to list all container IDs. This will help us to kill all Docker containers at once.
$ docker kill $(docker ps -q)
You can see that all the active containers have been killed.
We can use the sub-command to list all container IDs along with the Docker stop, remove, and kill commands. This will allow us to stop all Docker containers at once and remove them. In this article, we have explained how to stop, remove, and kill all Docker containers together. If you have any queries or suggestions, please mention them in the comment box and we will have our expert get back to you as soon as possible.