Sub topic 13
How Layers Are Shared Across Images, Pulls, and Pushes
This chapter assumes you can run docker build, docker pull, and docker push. It also assumes you know that an image is a stack of read-only layers, and that each layer is identified by a content digest. Here you will see how that identity lets Docker store, download, and upload each layer only once.
Why identical layers are stored once
A layer's identity comes from its content, not from the image or tag that uses it. Two images that contain a byte-identical layer point to the same layer in storage. Docker keeps one copy on disk.
Suppose you build two services from the same base image:
api:1starts fromnode:20-slimand adds the API code.worker:1starts fromnode:20-slimand adds the worker code.
Both images reference the same base layers. The local store holds those base layers once. Each image adds only its own top layers.
This is why docker images can mislead you. The SIZE column reports the full size of each image as if it stood alone. Adding up that column over-counts shared layers. To see real disk use, run docker system df, which counts shared data once.
docker images
docker system df
docker system df -vThe -v form lists images with a SHARED SIZE and a UNIQUE SIZE column. Shared size is the part used by at least one other image. Unique size is the part only that image uses.
What identifies a shared layer
Chapter material on content addressing introduced digests. Two different digests matter here, and mixing them up causes confusion.
| Identifier | What it hashes | Where you see it |
|---|---|---|
| Diff ID | The uncompressed layer tarball | `RootFS.Layers` in `docker image inspect` |
| Blob digest | The compressed layer tarball as stored in a registry | The image manifest in a registry |
Compression changes the bytes, so the compressed digest differs from the uncompressed one. The image config lists diff IDs. The registry manifest lists compressed blob digests. Docker keeps a mapping between the two so it can tell that a blob it has pulled corresponds to a layer it already has.
An image also has its own digest, the hash of its manifest. When you write node@sha256:..., you pin the exact manifest and therefore every layer it lists.
Sharing during a pull
A registry stores an image as a manifest plus blobs. The manifest lists the config blob and each layer blob by digest. When you run docker pull, the client does this:
- It fetches the manifest for the tag you asked for.
- It reads the list of layer digests.
- For each layer, it checks the local store.
- It downloads only the layers it does not already have.
The pull output shows the result. Layers already present print Already exists. Others print Pull complete after they download.
docker pull node:20-slim
docker pull myorg/api:1If myorg/api:1 is built from node:20-slim, the second pull skips the base layers and fetches only the application layers. Updating a service where only the top layer changed therefore costs a small download, not the full image size.
Layers download in parallel, but Docker unpacks them in order, because each layer builds on the one below it.
Sharing during a push
Push works the same way in reverse. Before uploading a layer, the client asks the registry whether it already has a blob with that digest. If it does, the client skips the upload. The output shows Layer already exists.
docker push registry.example.com/api:2For a rebuild where only the last COPY step changed, the push uploads that one layer plus the new manifest and config. The unchanged layers are already in the registry from the previous push.
Cross-repository mounts
Registries scope blobs to repositories, but the registry API allows a client to ask for a blob from one repository to be mounted into another in the same registry. If you push registry.example.com/team/worker and the client knows the base layer already exists in registry.example.com/team/api, it can request a mount instead of uploading the data again. The registry links the existing blob. The output shows Mounted from team/api.
This works only within one registry and only if the pushing account can read the source repository. Pushing to a different registry always uploads the data, because blobs are not shared between registries.
Sharing in the build cache
The same identity rules apply during docker build. A cache hit means the builder reuses an existing layer, so that layer is shared between the earlier build and the new one. Two projects on the same machine that start with the same FROM and the same first instructions can share cached layers, as long as the instruction and its inputs are identical.
The base image itself is the most common shared part. If you rebuild after the base tag moves to a new image, the FROM resolves to different layers. Every later step then misses the cache, as the invalidation chapter described. Sharing works only while the digests stay the same.
Why the same Dockerfile can produce different layer digests
A common surprise is that rebuilding the same Dockerfile on two machines produces different layer digests, even when the output looks identical. Sharing is lost when layer content differs by even one byte. Typical causes:
- Timestamps. Files created during a build carry modification times. A
RUNstep that writes a file records the time it ran, so two builds of that step usually yield different layers. - Non-deterministic output. Build tools that embed a build time, a random value, or an ordering that varies between runs change the bytes.
- Moving inputs.
apt-get installorpip installwithout pinned versions can fetch different packages on different days. - File metadata from
COPY. Changed file contents change the layer. Ownership and permissions are part of the layer too.
Because of this, cache reuse across machines does not come from rebuilding. It comes from sharing the layers that were already built: pulling the image, or importing build cache, as covered later in this tutorial. Rebuilding the same step on a second machine generally produces a new layer with a new digest.
Local storage on disk
On a Linux host with the classic storage driver, layers live under /var/lib/docker. With the overlay2 driver, each layer is a directory under /var/lib/docker/overlay2. When a container starts, the driver mounts the image layers as read-only lower directories and adds a writable upper directory for that container.
Many containers from the same image therefore share the same read-only layers. Each container pays only for its own writable layer. Starting ten containers from a 500 MB image does not use 5 GB. It uses 500 MB plus whatever each container writes.
Docker Engine versions that use the containerd image store keep layers as content in containerd's store instead. The sharing rules are the same, but the paths on disk differ. Check which store you use with docker info.
docker info --format '{{.Driver}}'
docker ps -sdocker ps -s shows two numbers per container. The first is the size of the writable layer. The virtual size includes the shared image layers.
Removing images and shared layers
When you remove an image, Docker deletes only the layers that no other image or container uses. A layer shared with another image stays.
docker rmi api:1
docker image prune
docker builder prunedocker rmiremoves a tag, and the layers only if nothing else references them.docker image pruneremoves dangling images, which are images with no tag.docker builder pruneclears build cache. Cache entries are separate from images, so pruning images does not free that space.
This explains a common question: why docker rmi of a large image freed little space. Most of its layers were shared with another image that is still present.
Practical consequences
- Put a common base in as many images as you can. A shared
FROMmeans one download per host and one upload per registry. - Keep rarely changing layers low and frequently changing layers high. A small top layer makes pulls and pushes small, which is the same ordering advice that keeps the build cache warm.
- Pin base image tags, or digests, so hosts agree on which base layers they hold.
- Do not expect a rebuild to give you a layer you can share. Share by pushing and pulling.
Exercise
You push registry.example.com/api:2 after changing only the final COPY . . step in a Dockerfile. The previous push of api:1 used the same earlier steps. Which layers does the push upload, and why does the output show Layer already exists for the others? Then explain why docker images may show two images of 400 MB each while docker system df reports far less than 800 MB in use.
Show answer
The push uploads the new layer produced by the changed COPY . . step, plus the new config and manifest. The client asks the registry whether each blob digest already exists, and for the earlier layers it does, because those layers are byte-identical to the ones from api:1, so it prints Layer already exists and skips them. docker images reports each image's full size as if it were alone, so shared layers are counted twice. docker system df counts a shared layer once, so if the two images share most of their layers, the real disk use is far below 800 MB.