Sub topic 4
Removing Stopped Containers
This chapter assumes you can run docker run, docker ps, and docker images, and that you know an image is a template and a container is a running or stopped instance of it.
Why stopped containers take space
A container that exits is not deleted. Docker keeps its writable layer (files the container created or changed), its metadata, and its logs on disk. A container that ran for a minute and wrote nothing may use only a few kilobytes. One that wrote files or produced large logs can use gigabytes.
Stopped containers also block image cleanup. An image used by any container, even an exited one, is not "unused", so later image prunes skip it. Removing stopped containers first is usually the right order.
Finding stopped containers
List containers in the exited state before you delete anything. Docker's prune commands have no dry-run flag, so a filtered listing is your preview.
docker ps -a --filter status=exited
docker system dfThe -a flag includes containers that are not running. In docker system df, the Containers row shows how much space the writable layers use and how much of it is reclaimable.
Removing them with prune
docker container prune removes every stopped container and asks for confirmation first.
docker container pruneDocker prints a warning, waits for y, then lists the removed container IDs and the space reclaimed. Add -f to skip the question, which is useful in scripts.
A container counts as stopped if its status is exited, created, or dead. Running and paused containers are never touched.
Removing specific containers
Prune is all-or-nothing within its filter. To remove one container, or a chosen set, use docker rm.
docker rm my-old-container
docker rm $(docker ps -aq --filter status=exited --filter name=test-)-q prints only IDs, so the command substitution passes them to docker rm. If the filter matches nothing, docker rm is called with no arguments and prints an error. That is harmless.
Limiting by age or label
docker container prune accepts --filter. The until filter keeps recent containers, and label selects by label.
docker container prune --filter "until=24h"
docker container prune --filter "label=env=ci"Use label!=keep style filters to protect containers you want to survive.
What you lose
Removing a container deletes its writable layer and its logs. Data written inside the container's filesystem is gone. Data in named volumes is not affected, because volumes are separate objects, and docker container prune never removes them. Anonymous volumes are the exception when you use docker rm -v, which removes the anonymous volumes attached to that container.
Avoiding the problem
Start short-lived containers with --rm, and Docker deletes them automatically when they exit.
docker run --rm alpine echo "hello"After this command, docker ps -a shows no leftover container.
Common mistakes
- Expecting
docker container pruneto free image space. It frees only container layers. Run an image prune afterwards. - Assuming a container is safe because it is stopped. A stopped database container may be the only place its data lives if you did not mount a volume.
- Running
docker rmon a running container. It fails unless you add-f, which kills the container.
Exercise
You run docker image prune -a and an old image is not removed, even though no container is running from it. What is the likely cause, and what command fixes it?
Show answer
A stopped container still references the image, so it counts as in use. Remove stopped containers with docker container prune, then run docker image prune -a again.
Exercise
Write a command that removes only stopped containers that are more than 48 hours old, without asking for confirmation.
Show answer
docker container prune -f --filter "until=48h"