Sub topic 5
Pruning images: dangling versus all unused
This chapter assumes you can run docker build, docker ps and docker images. It also assumes you know that an image is a read-only template and a container is a running or stopped instance of one. Images are often the largest consumer of disk space on a build machine, so it pays to know exactly what docker image prune removes.
Two kinds of removable image
Docker treats two groups of images differently.
- A dangling image has no tag. It shows as
<none>:<none>. It usually appears when you rebuild an image and the tag moves to the new build, which leaves the old one untagged. - An unused image has a tag but no container, running or stopped, refers to it. Every dangling image is also unused, but the reverse is not true.
Plain docker image prune removes only dangling images. Adding -a widens it to every image that no container uses.
Preview before you delete
docker image prune has no dry-run flag. List what it would target with the same condition:
docker image ls --filter dangling=trueReview the output, then prune. Docker asks for confirmation and prints the space reclaimed.
docker image prunePruning all unused images
Use -a when you want tagged images gone too, for example old versions of a base image you no longer build from.
docker image prune -aAfter this, the next docker run or docker build that needs a removed image must pull it again. That costs time and bandwidth, so avoid -a on machines with slow or metered connections, or where the registry is unreachable.
Limiting by age
The until filter keeps recent images and removes only older ones. The value is a duration, so 168h means seven days.
docker image prune -a --filter "until=168h"This removes unused images created more than seven days ago. Note that until compares against the image's creation time, not when you pulled it. A freshly pulled image built months ago can match the filter, as long as no container uses it.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Space barely changes after `docker image prune` | The images are tagged, so they are not dangling | Use `-a`, ideally with `until` |
| `-a` leaves old images in place | Stopped containers still reference them | Run `docker container prune` first |
| Prune is slow to free space, `docker system df` still shows usage | Build cache holds layers separately | Run `docker builder prune` |
| Images you wanted come back as `<none>` after a rebuild | The tag moved to the new build | Expected. Prune the old untagged layers |
Confirm the result with docker system df, which reports the reclaimable portion for images.
Exercise
You run docker image prune and little space is freed, although docker images lists many old tagged images. What explains this, and which command with which filter removes only those older than 30 days?
Show answer
Plain docker image prune removes only dangling (untagged) images, and the old images still have tags. Use docker image prune -a --filter "until=720h", since 30 days is 720 hours. Images used by any container, including stopped ones, are still kept.