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 digest, a SHA-256 hash of its content.
Why identical layers are stored once
Because a layer is addressed by its digest, two images that contain the same layer point to the same stored data. Docker keeps one copy on disk, and a registry keeps one copy as a blob. The images only differ in their manifests, which are lists of the layer digests that make up each image.
A common case is several images built FROM the same base image. Each of them reuses the base layers, and only the layers added after FROM are unique.
Sharing on a pull
When you pull an image, Docker downloads the manifest first. It then checks which layer digests are already in local storage. Only the missing layers are downloaded. Layers that exist locally are reported as Already exists.
docker pull node:20-slim
docker pull node:20-bookworm-slimIf the two tags share base layers, the second pull downloads only the layers that differ. Pulling a new version of an image that changed only its top layer downloads only that layer.
Sharing on a push
A push works the same way in reverse. For each layer, Docker asks the registry whether a blob with that digest already exists. If it does, the layer is skipped and the output shows Layer already exists. Only new layers are uploaded.
docker build -t registry.example.com/app:1.1 .
docker push registry.example.com/app:1.1If you rebuild after changing only source code, the dependency layers come from the build cache and keep their digests. The push then uploads only the layers that changed.
A registry can also let you push a layer from one repository to another in the same registry without re-uploading it. This is called a cross-repository blob mount. It depends on the registry and on your permissions.
What breaks sharing
Sharing depends on identical content. Two layers that look the same to you can still have different digests, and then they are stored and transferred twice.
- A step that writes a timestamp, a random value, or a build number into a file produces different content on each build.
- Different base image tags or different versions of a base image produce different base layers.
- Installing the same packages in a different order, or at a different time, can yield different file contents.
- Changing any step above a layer changes that layer's cache key. The rebuilt layer can then have a new digest even if the files look the same.
Reproducible builds help here. Pinning base image versions and package versions makes the same steps produce the same layers.
Checking what is shared locally
You can compare the layer digests of two images to see what they share.
docker image inspect --format '{{json .RootFS.Layers}}' app-a:latest
docker image inspect --format '{{json .RootFS.Layers}}' app-b:latestDigests that appear in both lists are stored once on your machine. The command docker system df -v reports a SHARED SIZE for each image, which is the part of the image used by other images.
Exercise
Exercise
You pull app:2.0, which differs from the app:1.9 you already have only in its top layer. What does Docker download, and how does it decide?
Show answer
Docker downloads the manifest for app:2.0 and compares its layer digests with the ones in local storage. It skips every layer whose digest already exists, which is all the layers shared with app:1.9, and downloads only the changed top layer.
Exercise
You rebuild an image and push it. A layer that you did not intend to change is uploaded again. Name one likely cause.
Show answer
A step in the Dockerfile produces different content on each build, such as writing a timestamp into a file. The layer gets a new digest, so the registry does not already have it.