Sub topic 10
One command for everything: docker system prune
This chapter assumes you can run basic Docker commands and know the difference between an image and a container. You should also know the individual prune commands: container prune, image prune, builder prune, and network prune.
What system prune combines
docker system prune runs several of those cleanups in one step. By default it removes:
- all stopped containers
- all networks not used by at least one container
- all dangling images (untagged images that no tag points to)
- unused build cache
It does not remove volumes, and it does not remove tagged images that are merely unused. Both of those need an explicit opt-in.
Running it
Run the command with no flags first. Docker prints a warning that lists the categories, then asks for confirmation.
docker system pruneAnswer y to proceed. At the end Docker reports the total space reclaimed. Compare that figure with docker system df from before the run to confirm the effect.
Widening the scope
Two flags extend what gets removed:
| Flag | Extra effect |
|---|---|
| `-a` | Also removes every image not used by a container, not only dangling ones |
| `--volumes` | Also removes volumes that no container references |
The -a flag makes the next docker run or docker build pull or rebuild images from scratch. That costs time and bandwidth, but it loses no data.
The --volumes flag is different, because volumes often hold databases and uploads.
docker volume prune
docker system prune --volumesLimiting by age
The --filter option works with system prune too. The until filter keeps anything newer than the given age, so recent work is not touched.
docker system prune -f --filter "until=168h"This removes stopped containers, unused networks, dangling images, and unused build cache older than seven days. The -f flag skips the confirmation prompt, so it suits scripts but not casual use. Without --volumes, volumes stay untouched.
Common errors
- Expecting old tagged images to disappear. Plain
system prunekeeps them. Add-aonly if you want them gone. - Expecting volumes to be cleaned. They are skipped unless you pass
--volumes. - Seeing little space reclaimed. Running containers keep their images and networks in use. Large log files can also account for the usage, and they are not pruned. Log rotation is covered in a later chapter.
- Using
-finteractively. With no prompt, a typo or a wrong filter deletes things immediately. - Assuming a dry run exists. There is no dry-run flag. Preview with the listing commands instead, such as
docker ps -a --filter status=exitedanddocker image ls --filter dangling=true.
Choosing between system prune and the specific commands
Use docker system prune when you want a general tidy-up and you know no important data lives in stopped containers. Use the specific commands, such as docker image prune or docker builder prune, when you need to target one kind of object. Only the specific commands let you reclaim, for example, build cache without removing stopped containers you still want to inspect.
Exercise
A teammate runs docker system prune -a on a server. Which of these are removed: a stopped container, a tagged image no container uses, a volume holding a stopped database's data? Which command would remove the volume?
Show answer
The stopped container and the unused tagged image are removed, because -a extends the image cleanup to all unused images. The volume is kept, because volumes need --volumes. Running docker system prune --volumes or docker volume prune would remove it, and the database data would be lost.