In this tutorial, we show how to stop all container. Also, we show how to remove or kill all docker containers.
Please note that sometimes docker containers has executed and exited and are not in the running mode, on the other hand, it is possible we have a docker container which is running and needs to be stopped and removed.
If you don’t know what is docker container, we recommend you have a look at the essential docker tutorial to understand the docker concepts and the differences between docker containers and docker images.
For example, every time you run “docker run hello-world“, a new docker container form hello-world image gets created and executed and after printing the output the docker container exited from running.
In below we ran the command “docker run hello-world” two times and you can see the outputs of the “docker ps –all” command:
docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1e514798dff2 hello-world "/hello" 4 minutes ago Exited (0) 4 minutes ago compassionate_dewdney
46bba1f9d4b8 hello-world "/hello" 34 minutes ago Exited (0) 34 minutes ago priceless_faraday
In order to only stop a docker container, we simply run the command: “docker stop container-id” and you can find container-id by simply running “docker ps –all” command.
However, if you have many docker containers and you want to stop them all together you simply run:
“docker stop $(docker ps -a -q)”
Similarly, if you want to remove or kill a docker container run command: “docker rm container-id” but if you want to remove or kill all docker containers run: “docker rm $(docker ps -a -q)” and if you want to remove or kill by force all docker containers run:
“docker rm -f $(docker ps -a -q)”