Sub topic 11
Content-addressable storage: digests and manifests
This chapter assumes you can run docker build and docker pull, and that you know an image is a stack of read-only layers. Here you will see how Docker names those layers and the image that holds them.
Content addressing
Docker stores layers by what they contain, not by a name you choose. Each layer is a tar archive of filesystem changes. Docker runs a SHA-256 hash over it. The result is a digest such as sha256:3f4a.... Two layers with identical bytes have the same digest, and changing one byte gives a different digest.
This gives you three properties:
- Deduplication. Identical layers are stored once, however many images use them.
- Integrity. After a pull, Docker recomputes the hash and compares it to the expected digest. A corrupted or tampered layer fails the check.
- Safe caching. A digest is a stable identifier, so a layer that matches can be reused without question.
Two digests per layer
A layer has two digests, and mixing them up is a common source of confusion.
| Digest | Computed over | Where you see it |
|---|---|---|
| Diff ID | The uncompressed tar | `RootFS.Layers` in `docker image inspect` |
| Blob digest | The compressed tar sent to a registry | The manifest in a registry |
Compression changes the bytes, so the two values never match. Do not compare a diff ID from docker image inspect with a layer digest in a registry manifest and expect them to be equal.
Manifests and the image config
A registry stores an image as several blobs tied together by a manifest. A manifest is a small JSON document that lists the image's parts by digest.
- The config blob holds metadata: the environment,
CMD,ENTRYPOINT, and the ordered list of diff IDs. - The layers list gives the digest, size, and media type of each compressed layer.
The digest of the manifest itself identifies the image. That is the value after @ in a reference such as myapp@sha256:....
Looking at digests
The following commands show the image ID, the layer diff IDs, and the manifest digest.
docker pull debian:bookworm-slim
docker image inspect --format '{{.Id}}' debian:bookworm-slim
docker image inspect --format '{{json .RootFS.Layers}}' debian:bookworm-slim
docker buildx imagetools inspect debian:bookworm-slimThe image ID is the digest of the config blob. The imagetools inspect command prints the manifest digest and, for multi-platform images, one manifest per platform.
Tags versus digests
A tag such as node:20-slim is a movable pointer. The maintainer can point it at a new image at any time. A digest reference never moves.
FROM node:20-slim@sha256:<digest-from-imagetools-inspect>When you pin a base image by digest, the build cache's FROM step only changes when you change the digest. The cost is that you must update the digest yourself to receive security patches.
Replace the placeholder with a real digest from docker buildx imagetools inspect node:20-slim.
Multi-platform images
For an image that supports several CPU architectures, a tag points to an index (also called a manifest list). The index holds one manifest per platform. Docker picks the entry that matches your machine, then pulls only that manifest's layers.
Why this matters for builds
Content addressing is what lets layers be shared and verified. It does not decide cache hits, though. The build cache compares each instruction and its inputs, such as the files a COPY reads. A step that produces byte-identical output can still be a cache miss if its inputs changed. Conversely, a step that is cached keeps the exact layer digest it had before.
Exercise
You run docker image inspect on a local image and copy a layer digest from RootFS.Layers. You then search for that digest in the registry manifest of the same image and find nothing. Why?
Show answer
RootFS.Layers lists diff IDs, which are hashes of the uncompressed layer tars. The registry manifest lists blob digests, which are hashes of the compressed layers. Compression changes the bytes, so the two values differ even though they describe the same layer.