1. Stopping containers

First we have to figure out which containers are currently running with docker ps.

Listing all running containers
docker ps

docker ps 2

You can stop a single container by running docker stop followed by the container id or name.

Stopping a single container
docker stop a557431a5588

You can also specify and stop multiple containers.

Stopping multiple containers
docker stop a557431a5588 clever_fermi
Stop all containers at once

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.

Output all the container ids of currently running containers
docker ps -q
Run docker ps --help to find out what the -q flag does exactly.

By combining the output of the ps command with the stop commands we can easily stop all containers with one statement.

Linux/Mac OS X/Windows (Cygwin)
docker stop $(docker ps -q)
Windows (DOS Prompt)
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.

Listing stopped containers
docker ps -a
Run docker ps --help to find out what the -a flag does.

You can remove a container by running docker rm followed by its container id or name.

Removing a container
docker rm big_newton
Removing all containers

Of course, in this case we’d rather remove all containers at once

Linux/Mac OS X/Windows (Cygwin)
docker rm $(docker ps -a -q)
Windows (DOS Prompt)
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.