Sub topic 3
Layers, tags, and dangling images
This chapter explains how an image is stored on disk and why rebuilding or re-pulling an image leaves behind entries named <none>. It assumes you can run docker build, docker image ls, and docker run, and that you know an image is a template and a container is a running or stopped instance of one. Later chapters cover the prune commands. Here you learn what they target.
Images are stacks of layers
An image is an ordered stack of read-only layers. A layer is a set of filesystem changes: files added, modified, or deleted. Most Dockerfile instructions that change the filesystem, such as RUN, COPY, and ADD, create one layer. Instructions that only set metadata, such as ENV and CMD, add no meaningful file data.
Each layer is identified by a hash of its content, so two images that start from the same base share that base's layers on disk. Ten images built FROM alpine:3.20 store the Alpine layer once.
Three consequences matter for cleanup:
- The size column of
docker image lscounts every layer of an image, including shared ones. Adding up the sizes of all images overstates real disk use. - Deleting an image removes only the layers that no other image still uses. A 200 MB image may free only 5 MB if it shares the rest.
- A layer lives until the last image referring to it is gone.
You can inspect the layers of any image:
docker history alpine:3.20
docker image inspect --format '{{json .RootFS.Layers}}' alpine:3.20docker history shows one row per build step with its size. The inspect command prints the content hashes of the layers.
Tags are movable names
A tag is a human-readable name attached to an image, such as myapp:1.4 or alpine:latest. The image itself is identified by its ID, which is a content hash. A tag is only a pointer to that ID, and the pointer can be moved.
Two facts follow:
- One image can have several tags. For example,
myapp:1.4andmyapp:latestcan point to the same ID. - A tag points to exactly one image at a time. When you build or pull with a tag that already exists, the tag moves to the new image.
The image the tag used to point to is not deleted. It keeps its layers and its ID, but it has lost its name.
What dangling means
A dangling image is an image with no tag and no digest reference pointing to it, so it appears as <none> in both the REPOSITORY and TAG columns. It is no longer reachable by name. It takes up disk space until something removes it.
Dangling images mostly come from two routine events:
- Rebuilding an image under a tag that already exists, after any change that produces a different image.
- Pulling a tag whose upstream image changed, such as
docker pull alpine:latest. The tag moves to the new download and the old one is left unnamed.
This is why the problem grows quietly. A CI server that rebuilds myapp:latest on every commit leaves one dangling image per build.
Worked example: create a dangling image
This script builds the same tag twice with a different RUN command each time. Run it in an empty directory.
mkdir layers-demo && cd layers-demo
printf 'FROM alpine:3.20\nRUN echo "build one" > /version.txt\n' > Dockerfile
docker build -t layers-demo:latest .
printf 'FROM alpine:3.20\nRUN echo "build two" > /version.txt\n' > Dockerfile
docker build -t layers-demo:latest .
docker image lsThe listing now has two entries from this experiment. The exact IDs and sizes differ on your machine, but the shape looks like this:
REPOSITORY TAG IMAGE ID CREATED SIZE
layers-demo latest 5d1c0e7a93b2 5 seconds ago 7.8MB
<none> <none> c4f2a8b61d07 20 seconds ago 7.8MB
alpine 3.20 a606584aa9aa 3 weeks ago 7.8MBThe <none> row is the first build. The second build changed the text of the RUN instruction, so Docker could not reuse the cached layer and produced a new image. The tag moved to it.
List only dangling images with a filter:
docker image ls --filter dangling=true
docker image ls --filter dangling=true --quietThe second form prints only the IDs, which is useful in scripts. The old image is still fully usable by ID. You can run docker run --rm c4f2a8b61d07 cat /version.txt (substituting your own ID) and see build one.
Dangling versus unused
Two terms are easy to confuse, because both describe images you might want to remove.
- A dangling image has no name. This is a property of the image itself.
- An unused image is not used by any container, running or stopped. This is a relationship between the image and containers.
An image can be one, both, or neither:
| Image state | Has a tag | Used by a container | Default `docker image prune` | `docker image prune -a` |
|---|---|---|---|---|
| Tagged, in use | Yes | Yes | Kept | Kept |
| Tagged, not in use | Yes | No | Kept | Removed |
| Dangling, not in use | No | No | Removed | Removed |
| Dangling, in use | No | Yes | Kept | Kept |
A dangling image can be in use. If you start a container from layers-demo:latest and then rebuild the tag, the running container keeps its original image, which is now dangling. Prune skips it until the container is removed.
Pulled-by-digest images need a note. If you run docker pull alpine@sha256:..., the TAG column shows <none>, but the image has a name and digest, so it is not dangling. Use docker image ls --digests to see the difference.
Hidden intermediate layers
By default, docker image ls shows only images you would think of as images. Older builders recorded every build step as an untagged intermediate image, and docker image ls --all shows them. With BuildKit, the default builder in current Docker releases, intermediate steps live in the build cache instead and are not listed as images at all. That cache is a separate object with its own prune command, covered in the build cache chapter.
So an <none> row in your listing is almost always the final result of an earlier build or pull, not a leftover build step.
Untagging versus deleting
Because tags are pointers, removing a name is different from removing an image. Try this:
docker tag layers-demo:latest layers-demo:stable
docker rmi layers-demo:stableThe output reads Untagged: layers-demo:stable. The image still exists because layers-demo:latest still points to it. Only when you remove the last tag does Docker print Deleted: sha256:... and release the layers that no other image uses.
This also gives you a way to rescue a dangling image before cleanup. If the old build matters, give it a name:
docker tag c4f2a8b61d07 layers-demo:previousIt stops being dangling at once, and prune no longer touches it.
Why this matters for cleanup
Keep these rules in mind when you reach the prune chapters:
- Dangling images are the safest images to delete, because nothing refers to them by name.
- "Unused" is a wider set. It includes tagged images you may still want, such as a base image you pulled but have no container for right now.
- The space you recover depends on layer sharing, so the total freed is usually less than the sum of the SIZE column.
- Removing a container is what frees an image for deletion. The next chapter covers that order of operations.
To see how much of an image's size is shared with others, use the verbose disk usage report:
docker system df -vIts image table has SHARED SIZE and UNIQUE SIZE columns. Unique size is roughly what you would get back by deleting that image alone.
Exercise
You run docker pull alpine:latest on Monday and again on Friday, and the upstream image changed in between. After Friday's pull, the Monday image appears as <none>:<none> in docker image ls. Why, and is it removed by a default docker image prune if no container uses it?
Show answer
A tag points to one image at a time. Friday's pull moved the alpine:latest tag to the new image, so the Monday image lost its only tag and became dangling. A default docker image prune removes dangling images, so it deletes the Monday image if no container, running or stopped, uses it.
Exercise
An image has two tags, app:1.0 and app:latest. You run docker rmi app:latest. What does Docker print, what happens to the image's layers, and what would you have to do to actually delete the image?
Show answer
Docker prints Untagged: app:latest. The image and its layers stay because app:1.0 still points to it. To delete the image you must remove its last tag with docker rmi app:1.0, which prints Deleted: sha256:... and frees the layers that no other image shares. This assumes no container uses the image.