Sub topic 3
Layers, tags, and dangling images
This chapter assumes you can run docker build, docker images, and docker ps, and that you know an image is a template and a container is a running instance of one. It explains why some images take disk space and cannot be removed by a plain prune. That depends on how layers, tags, and "dangling" status relate.
Layers are shared
An image is a stack of read-only layers. Each instruction in a Dockerfile that changes the filesystem, such as RUN, COPY, or ADD, creates one layer. Docker identifies layers by content hash, so two images built from the same base reuse the same base layers on disk. Disk usage for an image is therefore not simply the sum of its size as listed.
A tag is a name that points to one image ID. Many tags can point to the same image, and removing a tag does not delete the image while another tag or container still references it.
What "dangling" means
A dangling image has no tag. It shows as <none>:<none> in listings. The usual cause is rebuilding a tag. When you run docker build -t myapp . a second time and the result differs, myapp moves to the new image. The old image loses its name and becomes dangling.
An unused image is a different case. It has a tag, but no container, running or stopped, was created from it. Both kinds take disk space, but Docker treats them differently:
| Kind | Has a tag | Used by a container | Removed by `docker image prune` | Removed by `docker image prune -a` |
|---|---|---|---|---|
| Dangling | No | No | Yes | Yes |
| Unused | Yes | No | No | Yes |
| In use | Either | Yes | No | No |
The default prune is conservative because a tagged image is something you named on purpose. Only -a widens the target to every image that no container uses.
Seeing dangling images
List them with a filter:
docker image ls --filter dangling=trueTo see the layers of one image and the size each adds, use docker history:
docker history myapp:latestEach row is one layer with the instruction that made it. A large COPY or a RUN that downloads packages usually accounts for most of the size.
Intermediate layers and the build
Rebuilding produces dangling images mainly when a layer changes and the final tag moves. Layers that did not change are reused from the build cache, so a rebuild with a small change adds only the changed layers. The old final image becomes dangling, but its unchanged layers stay in use by the new one. Removing the old image frees only the layers nothing else shares.
This is why freed space after a prune is often smaller than the size column suggested. The size shown for an image counts shared layers in full. docker system df -v reports a SHARED SIZE and a UNIQUE SIZE for each image, and the unique figure is closer to what removal would free.
Common errors
- Expecting
docker image pruneto remove old tagged versions. It does not. Use-a, or remove specific tags withdocker rmi. - Trying to remove an image that a stopped container still uses. Docker refuses. Remove the container first, as the next chapter shows.
- Adding up the
SIZEcolumn to estimate savings. Shared layers make the total too high.
Exercise
You run docker build -t api:1.0 . twice, and the second build changes one source file. After the second build, docker image ls shows an api:1.0 image and one <none>:<none> image. Which one does docker image prune (without -a) remove, and why might the space freed be smaller than that image's SIZE column?
Show answer
It removes the <none>:<none> image, because it is dangling (untagged). The freed space is smaller because most of its layers are shared with the new api:1.0 image, so only its unique layers are deleted.