Now and then, especially when working on a development environment, you need to stop multiple Docker containers. Quite often, you need to stop all of the currently running containers. I’m going to show you one of the possible ways.
You need to use a container name or container ID with the docker stop command.
For example, I have an nginx load balancer container:
Based on this output, I can stop my nginx container like this:
… or like that:
Since I also have a MariaDB container named db, I might need stop it together with nginx.
Here’s the info on the db container:
If I ever decide to stop both nginx and db together, I can do it like this:
As you can see from previous examples, docker stop simply takes a list of containers to stop. If there’s more than one container, just use space as a delimiter between container names or IDs.
This also allows us to use a clever shell expansion trick: you can some other command, and pass its output to the docker stop container.
For instance, this shows us the list of all the IDs for currently running Docker containers:
What we can do now is pass the result of this command as the parameter for the docker stop command:
And just to check, running docker ps now won’t show any running containers:
IMPORTANT: make sure you double-check what you’re doing! Specifically, run docker ps -q, compare it to docker ps, this kind of thing. Because once containers stopped you may not have an easy way to generate the list of same containers to restart.
In my case, I’m just specifying them manually as the parameters for docker start:
That’s it for today! Hope you enjoyed this quick how-to, let me know if you have any questions, Docker and whatnot!