Sub topic 8
Removing containers
This chapter covers how to delete containers you no longer need. It assumes you can list containers with docker ps, you know a container's name or ID, and you know how to stop one. It also assumes you understand the difference between an image and a container.
Why remove containers
A stopped container is not gone. Docker keeps its metadata, its logs, and its writable layer on disk. Over time these pile up and clutter docker ps -a. Removing a container frees that space and releases its name, so you can reuse the name for a new container.
Removing a container deletes the container only. It never deletes the image the container was created from.
Removing a stopped container
Use docker rm with a name or ID. Stop the container first, then remove it.
docker stop web
docker rm webOn success, docker rm prints the name or ID you gave it. You can pass several containers in one command, separated by spaces:
docker rm web db cacheRemoving a running container
If you try to remove a running container, Docker refuses and prints an error like this:
Error response from daemon: You cannot remove a running container ... Stop the container before attempting removal or force removeYou have two options. The safe one is to run docker stop first. The faster one is -f (force), which sends SIGKILL to the container's main process and then removes it. The process gets no chance to shut down cleanly.
docker rm -f webPrefer docker stop followed by docker rm when the application needs to flush data or close connections.
Removing a container's volumes
Some images declare volumes, which are directories stored outside the container's writable layer. If you start a container without naming a volume for such a directory, Docker creates an anonymous volume. It has a random ID as its name.
By default, docker rm leaves anonymous volumes behind. Add -v to delete them together with the container:
docker rm -f -v webNamed volumes are not removed by -v. They are meant to outlive containers, so you must remove them yourself with docker volume rm.
Removing automatically with --rm
For throwaway containers, pass --rm to docker run. Docker removes the container as soon as it exits.
docker run --rm alpine echo "hello"The container prints hello, exits, and is deleted. Running docker ps -a afterward shows no trace of it. This works well for one-off commands, quick tests, and scripts. Do not use it when you need to inspect logs or the filesystem of a container after it exits. With --rm, anonymous volumes are also removed along with the container.
Removing many containers
To remove all stopped containers, use docker container prune. Docker asks for confirmation before it deletes anything.
docker stop $(docker ps -q)
docker container pruneThe first command uses docker ps -q, which prints only the IDs of running containers, and passes them to docker stop. The second removes every stopped container. Check what is stopped first with docker ps -a, because prune does not distinguish between containers you still want and ones you forgot about.
You can narrow prune with a filter. This removes only containers that stopped more than 24 hours ago:
docker container prune --filter "until=24h"Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `You cannot remove a running container` | The container is still running | Run `docker stop` first, or use `docker rm -f` |
| `No such container` | The name or ID is wrong, or it was already removed | Check `docker ps -a` |
| `Conflict. The container name is already in use` when running a new container | A stopped container still holds the name | Remove the old container with `docker rm`, or choose another name |
| Disk space does not drop after removal | Anonymous volumes or images remain | Use `docker rm -v`, and clean unused volumes and images separately |
Exercise
You run docker run -d --name web nginx:alpine, and later docker stop web. You then run docker run -d --name web nginx:alpine again and get a name conflict error. Why does this happen, and what single command fixes it?
Show answer
Stopping a container does not remove it, so the stopped container web still holds the name. Run docker rm web to remove it, then start the new container. Alternatively, use docker run --rm next time so the container is deleted when it exits.
Exercise
What is the difference between docker rm web and docker rm -f -v web?
Show answer
docker rm web only removes a stopped container and keeps its anonymous volumes. docker rm -f -v web also works on a running container, because -f kills it with SIGKILL first, and -v deletes its anonymous volumes. Named volumes are kept in both cases.