1. Stopping containers
First we have to figure out which containers are currently running with docker ps
.
docker ps
You can stop a single container by running docker stop
followed by the container id or name.
docker stop a557431a5588
You can also specify and stop multiple containers.
docker stop a557431a5588 clever_fermi
Of course, you might also want to stop all containers at once. To do so, we’d have to pass all the container ids we want to stop to the stop
command. We can achieve this by combining two commands.
docker ps -q
By combining the output of the ps
command with the stop
commands we can easily stop all containers with one statement.
docker stop $(docker ps -q)
FOR /f "tokens=*" %i IN ('docker ps -q') DO docker stop %i
If you now run docker ps
the list of containers should be empty.
2. Removing containers
Now that we’ve stopped all the containers we can also remove them. Let’s see how many stopped containers we have on our system by running docker ps -a
.
docker ps -a
You can remove a container by running docker rm
followed by its container id or name.
docker rm big_newton
Of course, in this case we’d rather remove all containers at once
docker rm $(docker ps -a -q)
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
If you now list all stopped containers again, docker ps -a
, no containers should be returned.This does not mean that the images are no longer available, as you can see by running docker images
. We’ve only removed the containers which are, in essence, instances of the images available on your system.