Sub topic 5
Pruning images: dangling versus all unused
This chapter assumes you can run docker build, docker run, and docker ps, and that you know an image is a read-only template and a container is a running or stopped instance of one. It also assumes you have seen docker system df and understand that images share layers. Here you will learn what docker image prune removes, how that changes with -a, and which mistakes to avoid.
Two categories of removable image
Docker treats an image as removable in one of two ways, and the difference decides which flag you need.
- A dangling image has no tag and no other image or container depending on it.
docker image lsshows it as<none>:<none>. It usually appears when you rebuild an image under a tag that already exists. The tag moves to the new build, and the old build loses its name. - An unused image is any image that no container uses, whether the container is running or stopped. It can still have a tag, such as
postgres:15.
Every dangling image is also unused, but most unused images are not dangling. Pulled base images, old releases, and images from past projects are tagged, so they are not dangling. They only become removable once you ask for all unused images.
A tagged image that a stopped container references does not count as unused. Docker keeps it because the container still points at it. This is why container cleanup comes before image cleanup. If you remove stopped containers first, more images become eligible for removal.
Finding what a prune would remove
docker image prune has no dry-run flag. Preview the targets with the matching list command first.
docker image ls --filter dangling=trueThe output lists only the dangling images, with their sizes. For the wider -a case, list everything and compare it against your containers:
docker image ls
docker ps -a --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"Any image in the first list that does not appear in the second list is a candidate for docker image prune -a. The comparison is approximate. A container may reference an image by ID instead of by tag, so match on the image ID if a tag looks missing.
Removing dangling images
Run the command with no flags to remove only dangling images.
docker image pruneDocker shows a warning and asks for confirmation:
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N]After you answer y, it prints the deleted layer IDs and the total reclaimed space. This is the safest image prune. A dangling image has no name, so you cannot start a container from it by name, and you were unlikely to be relying on it.
Dangling images are often small compared with the total. On a machine that builds often, they still add up. Each rebuild of a large application can leave a few hundred megabytes behind.
Removing all unused images
Add -a (or --all) to include tagged images that no container uses.
docker image prune -aThe warning changes to say it will remove all images without at least one container associated to them. This usually frees much more space than the plain form, and it has a cost. Every removed image must be pulled or rebuilt the next time you need it. That costs bandwidth and time, and it can fail if the registry is down, the tag was deleted upstream, or you are offline.
Also consider images you need but are not running right now. A base image such as node:20 that you build from daily has no container attached to it, so -a removes it. The next build then downloads it again.
Limiting by age
The until filter restricts the prune to images created before a given time. This keeps recent work and removes old images.
docker image prune -a --filter "until=168h"This removes unused images created more than 168 hours (7 days) ago. Note the word created. The filter uses the image's creation timestamp, not the time you pulled it. If you pulled a three-year-old image yesterday, it matches until=168h and is removed. An image that was built a few hours ago is kept regardless of whether it is used.
You can also filter by label with --filter "label=<key>" or --filter "label!=<key>". This lets you mark images that should survive, or select only the ones you want to remove. See the filtering chapter for details.
Edge cases and gotchas
Images used by stopped containers are kept
If docker image prune -a seems to free less than you expected, check for stopped containers.
docker ps -a --filter status=exitedEach exited container holds a reference to its image. Remove the containers you no longer need with docker container prune, then run the image prune again.
Shared layers are freed only when no image needs them
Images are built from layers, and many images share the same base layers. Removing one image deletes only the layers that no remaining image uses. The reclaimed space is therefore often smaller than the size column in docker image ls. That column counts shared layers in full for each image. docker system df reports shared size separately, which gives a more accurate picture.
Pruning a tagged image that has other tags
An image can carry several tags. When you remove an image by name with docker rmi, Docker removes that tag, and the image data is deleted only when its last tag goes. Prune works on whole images, so it is not affected by this.
Images in use by in-progress builds
Under BuildKit, intermediate results live in the build cache, not in the image list. Pruning images does not clear that cache. Use docker builder prune for it, which a later chapter covers. If docker system df still shows large usage after an image prune, the build cache is the likely reason.
Prompts in scripts
docker image prune waits for input and will hang or abort in a non-interactive script. Pass -f (or --force) to skip the prompt. Add -f only after you are sure of the command's scope, because there is no second chance.
docker image prune -a -f --filter "until=168h"Removing specific images
Prune is broad. When you want to remove one image or a short list, name them directly.
docker image rm myapp:old
docker rmi 3f1c9a2b7d10Docker refuses to remove an image that a container uses and prints the container that blocks it. Remove the container first, or use -f to force removal of the tag. Forcing does not delete the layers of an image that a container still needs.
Choosing between the two commands
| Situation | Command | Risk |
|---|---|---|
| Routine tidy-up after many rebuilds | `docker image prune` | Low. Removes untagged, unused images only. |
| Disk is nearly full and you can re-pull images | `docker image prune -a` | Medium. Tagged images vanish and must be re-pulled or rebuilt. |
| Keep recent images, drop old ones | `docker image prune -a --filter "until=168h"` | Low to medium. Judged by creation date, not pull date. |
| Remove one known image | `docker image rm NAME` | Low. Fails if a container uses it. |
Common errors
- Expecting
docker image pruneto remove tagged images. It removes only dangling images. Add-afor the rest. - Running
-awhile a needed container is stopped and then deleted. An image used only by a stopped container is safe, but once that container is gone the image is not. Check your containers before you remove them. - Assuming
untilmeans "not used since". It filters on creation time, so freshly pulled old images are removed. - Reading the size column as the space you will recover. Shared layers make the real gain smaller.
- Forgetting the build cache. Image pruning leaves it untouched.
Exercise
You run docker image prune and it frees only 200 MB, but docker image ls shows several tagged images from last year with no containers using them. Which command removes them, and which command would you use to keep anything created in the last 30 days?
Show answer
Plain docker image prune removes only dangling images, so the tagged images stay. docker image prune -a removes all unused images. To keep anything created in the last 30 days, use docker image prune -a --filter "until=720h", because 30 days is 720 hours and the filter matches images created before that point.
Exercise
After docker image prune -a, an image you expected to be removed is still present. docker ps -a shows an exited container. What is the likely cause, and what do you do?
Show answer
The exited container still references the image, so Docker treats the image as in use. Remove the container with docker container prune (or docker rm NAME for one container), then run docker image prune -a again.