Hope this brief article makes it a little easier to find how to stop all docker containers. Let’s discuss some scenarios.
To stop all Docker containers, run the following command on your terminal:
docker stop $(docker ps -q)
docker kill $(docker ps -q)
docker ps will list all containers that are in the running state in your computer/server. The
-q flag lists only the IDs (quiet mode) for those containers.
When we have a list of all container IDs, we can execute the above commands to terminate all the containers that are in a running state.
How To Remove All Docker Containers
If you do not want to stop the running containers and to delete all the containers, you can go one step further and execute the following command,
docker rm $(docker ps -aq)
Now, we already know that Docker lists all the running containers IDs by executing
docker ps -q. What is the
-a flag? Well, not only will it run, but all containers will return. Therefore, this command will remove all containers (including running and stopped containers).
How To Remove All Docker Images
To remove all Docker images, run this command,
docker rmi $(docker images -q)
docker rm $(docker images -q)
docker images -q lists all image IDs. We will send these IDs to the
docker rmi command as input, which results in removing all the docker images existing in your computer.