In this quick guide, we will show you how to easily and quickly kill all running Docker containers using the terminal.
![Docker Kill All Containers](https://pimylifeup.com/wp-content/uploads/2024/07/Docker-Kill-All-Containers-Thumbnail.jpg)
Docker is a great tool for running and managing containerized applications on your machine. It can also significantly improve software deployment and testing.
There are various reasons why you may want to kill all the Docker containers on your system. One is that you potentially want to have a clean slate to run off of. Another possible reason is that you are having an issue with containers consuming too many resources on your host system, or the containers simply won’t stop gracefully as they should.
Whatever reason you want to kill your containers, Docker makes the whole process extremely easy, especially if you use the terminal.
In the next section, we will show you the very simple one-liner you can use to terminate any Docker container running on your system.
Using the Terminal to Kill All Docker Containers
If all you want to do is kill any running Docker container on your machine, you only need to use the following simple one-liner. This one-liner grabs a list of all the running containers and passes them straight into Docker’s built-in kill command.
This functionality is built directly into the Docker runtime, so we don’t have to worry about relying on additional software to stop containers.
Please note that killing a container will not allow it to shut down gracefully. This means you could potentially run into corrupted or lost data if your container is in the middle of writing to a volume. You should only use “kill
” when you are trying to stop a container that refuses to stop gracefully.
docker kill $(docker ps -q)
If you would prefer, you can also use the newer long-form syntax to kill your Docker containers. It works exactly the same as the previous command but just uses the new alias for each command.
This syntax change is basically to make it clearer what exactly your command will affect.
docker container kill $(docker container ls -q)
Explaining How This Command Works
Let us break down this command so you can get a better idea of what is happening here. First, this is actually a combination of two separate commands: “docker kill
” and “docker ps
.”
When this line is executed, the second command is actually executed first, as we insert its value into the line using what is called command substitution ($(COMMAND)
) .
The “docker ps
” command lets us easily get a list of all running Docker containers we want to kill. Normally this command will output various information about said containers, but the “-q
” option forces it to only return a list of container IDs.
Once Docker has returned its list of running processes, the result is fed directly into the “docker kill
” command. This command takes a list of container IDs and terminates all of them.
Each container fed into the Docker kill command will be sent a “SIGKILL
” signal and be forced to stop immediately. It is possible to change the signal sent by the kill command, but for most use cases, if you are resorting to this, you will want to use “SIGKILL
” anyway.
Example of How to Kill All Docker Containers
1. Let us now walk you through a quick example of how this all works in a real-world environment.
To showcase this, we will first spin up three containers. What they are for doesn’t matter as we don’t intend to use them.
In this case, we will start up two empty Alpine containers to kill. We are using Alpine here because it is super lightweight and won’t consume too much space to test.
With these commands, we are use the “-i
” option to make the container interactive and the “-t
” option to feed it into a pseudo-terminal. Finally, we use the “-d
” option to start the Alpine container in detached mode.
docker run -dit alpine
docker run -dit alpine
2. With the Docker containers up and running, we can now kill all of them by running the following command in the terminal.
docker kill $(docker ps -q)
After running the command, you will see the ID of each container that the Docker runtime has killed. An example of this is what we have shown below
7e358c6271ee
4c58965990da
3. We can verify that we no longer have any running containers by using Docker’s list container command.
This command, by default, will only list containers running on your system.
docker container ls
If the kill command works successfully, you will see that nothing has been returned. However, if a container has been set to restart automatically, it may have been brought back online.
Conclusion
Hopefully, at this stage, you will have a good understanding of how to easily kill all running Docker containers on your system.
Thanks to the Docker runtime’s built-in functionality, killing containers is a relatively straightforward process. You don’t have to stress about installing any software, as all required functionality is built in.
Feel free to post a comment below if you have had any trouble with the steps provided in this guide.
If you liked this tutorial, we highly recommend you check out our many other Docker tutorials and guides.