Docker Stop All Containers in 3 Ways
If you are working with Docker for the first time or starting to run multiple docker containers on a single host or across multiple hosts, you are probably starting to manage the hosts and running into just day-to-day housekeeping items you want to accomplish, like stopping containers, rebooting hosts, etc. One of the basic tasks you may want to know how to do is stopping all containers on a host. Let’s look at the topic of docker stop all containers and we will see three ways to do this.
Table of contents
1. Using the command line
This is the first way that you can use to stop all containers. Using the command line is simple, its efficient, and it is effective to perform the task of stopping all containers. What is the command?
docker stop $(docker ps -a -q)
In the above command the docker stop portion is the command to stop containers. However, we are passing in the value of a command $(docker ps -a -q). This is what that portion does:
docker ps -a -q
: Lists all containers (-a
for all,-q
for only the container IDs).docker stop
: Stops the containers of the IDs that are captured as arguments.$()
: This captures the output ofdocker ps -a -q
and passes it as an argument todocker stop
.
In the command output above, you will see the container IDs enumerated that were stopped and then the command will return to your bash prompt.
2. Using Portainer
After the command line, if you are a more “point-and-click” type person who likes doing things from a GUI, Portainer offers a great solution to manage docker in general and it allows you to stop all containers at the same time as well.
Once you onboard your docker container host into Portainer by installing the portainer agent, you can browse to containers in the left-hand navigation menu. Then select all containers with the tick box above the list of containers. Place a check there, and then click the stop button.
Portainer is a free tool using the community edition that you can run with most features and functionality. You can also get a free 3-node business edition license that allows you to have even more features. You can check out that offer here: Take 3.
Of course there are many other advantages to using something like Portainer for stopping your containers as it is a solution that is complete when it comes to fully managing the environment, including lifecycle management, etc.
3. Komodo
There is another free tool that provides a really great GUI to manage your docker containers as well, called Komodo. I recently posted a detailed post covering many of the features of Komodo. You can read that here: Portainer Alternative Komodo for Docker Stack Management and Deployment.
In brief, Komodo is a free and open source solution that you can download without limitations and start adding your docker container hosts. It has really nice features for a free tool and is on par with Portainer for most features of docker management. Let’s look at how to bulk stop containers with it.
In Komodo, you navigate to your docker container host “server” as they are called in Komodo. Then you will see under the Execute section of the dashboard, you have the option to Stop Containers.
If you click the button to stop containers, Komodo actually prompts you to confirm that you want to stop all containers. You have to type in the name of the docker container host and then click Stop Containers and then Confirm.
Komodo will bulk stop all the docker containers running on your host. One of the notes to make about Komodo. In my testing, it won’t stop the “periphery” agent that is its equivalent to the portainer agent. I am guessing this function has been coded this way so that you don’t stop the agent where komodo can’t connect to the docker host. Keep this in mind though if you literally want to stop all containers.
Wrapping up
If you need to stop all docker containers running on a host, using the three methods shown in the blog will help you to have a variety of ways to do this. Keep in mind that the command line solution is native to docker. So, you won’t have to install any other tools or utilities. However, Portainer and Komodo are two very good tools to have GUI management for your docker container hosts. Each of these tools allow you to stop all the running docker containers on your container hosts.