How to stop multiple Docker Container at once
Docker containers can be stopped simply with the docker stop
command. Of course, it is possible to append multiple container names or container IDs to the docker stop
command to stop multiple containers at once. This is especially useful on test systems to quickly find a clean environment again.
Stop all running Container
Instead of manually listing the individual IDs or names of the desired containers, you can also cleverly combine the docker ps
(which is the same like docker containers list
) and the docker stop
commands to stop all running Docker containers, for example:
docker ps --format {{.ID}} | xargs docker stop
The first command docker ps --format {{.ID}}
gets a list of the current running Docker containers and only outputs the IDs of the running containers.
The second command xargs
then gets the result of the first command via the pipe Operator |
and provides the IDs as an argument to the following docker stop
command.
Stop only specific Container
By extending the first command with the --filter
(short -f
) argument, you're able to reduce the result of docker ps
. For example if you want to stop all container running which have mongodb in their name, you can run the following command:
docker ps -f "name=mongodb" --format {{.ID}} | xargs docker stop
If you just want to test your result-set before passing all results to docker stop
you can just use docker ps -f "name=mongodb"
until you're happy with the result set.
A full list of all possible commands and options of docker ps
can be found in the Docker documentation. If you have any questions feel free to ask in the comments.
Member discussion