Sub topic 11
Content-addressable storage: digests and manifests
This chapter assumes you can run docker pull and docker build, and that you know an image is a stack of layers. Here you will learn how Docker names those layers and the image that holds them. The names are not labels someone picked. They are computed from the bytes.
Content addressing in one idea
In a content-addressable store, the name of an object is a hash of its content. Docker and the OCI (Open Container Initiative) image format use SHA-256. A name like this is called a digest:
sha256:3b18e2b4a1c6d0f7e5a9c2d8b1f4a7e6c0d3b9a8f5e2c1d4b7a6e9f0c3d2b1a8Two properties follow from this.
- Identical content always gets the same digest, so it is stored once.
- Changing a single byte produces a different digest, so a stored object cannot be altered without its name changing.
The following Python script is a toy content-addressable store. It runs as shown with any Python 3.
import hashlib
import json
import pathlib
STORE = pathlib.Path("blobs") / "sha256"
def put(data: bytes) -> str:
"""Store bytes under their SHA-256 digest and return the digest."""
hex_digest = hashlib.sha256(data).hexdigest()
STORE.mkdir(parents=True, exist_ok=True)
path = STORE / hex_digest
if not path.exists():
path.write_bytes(data)
return "sha256:" + hex_digest
def get(digest: str) -> bytes:
"""Read a blob and verify it still matches its name."""
hex_digest = digest.split(":", 1)[1]
data = (STORE / hex_digest).read_bytes()
if hashlib.sha256(data).hexdigest() != hex_digest:
raise ValueError("blob does not match its digest")
return data
layer_a = put(b"contents of layer A")
layer_b = put(b"contents of layer B")
layer_a_again = put(b"contents of layer A")
print(layer_a == layer_a_again) # True: same bytes, same name, stored once
print(len(list(STORE.iterdir()))) # 2 files on disk, not 3
manifest = {"schemaVersion": 2, "layers": [layer_a, layer_b]}
manifest_digest = put(json.dumps(manifest, sort_keys=True).encode())
print(manifest_digest)The manifest in the last lines is itself a blob. It lists other blobs by digest, and the manifest has a digest of its own. A real registry works the same way, with more fields.
The pieces of an image
An image in a registry is a small tree of blobs. Each blob is addressed by its digest.
| Object | What it contains | What its digest identifies |
|---|---|---|
| Layer blob | A tar archive of filesystem changes, usually gzip-compressed | The compressed bytes as stored in the registry |
| Config blob | JSON with the command, environment, history, and the list of layer diff IDs | The image itself, as Docker reports it (the image ID) |
| Manifest | JSON listing one config blob and the ordered layer blobs, each with a media type, digest, and size | One image for one platform |
| Image index | JSON listing manifests, each tagged with an OS and architecture | A multi-platform image |
An abbreviated OCI manifest looks like this:
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"config": {
"mediaType": "application/vnd.oci.image.config.v1+json",
"digest": "sha256:aaaa...",
"size": 1469
},
"layers": [
{
"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
"digest": "sha256:bbbb...",
"size": 3623807
}
]
}The size field lets a client check the download length before it trusts the data. The digest then confirms the content.
Two digests for one layer
A layer has two different digests, and mixing them up causes confusion.
- The layer digest in the manifest hashes the compressed tarball, which is the exact thing sent over the network.
- The diff ID in the config blob hashes the uncompressed tar. It is the same no matter which compression was used.
The config lists diff IDs under rootfs.diff_ids. The command docker image inspect --format '{{json .RootFS.Layers}}' from the earlier chapter on inspecting layers prints these diff IDs. They will not match the digests you see in a registry manifest.
Compression is one reason for keeping both. Gzip output can differ between tools and settings, even for the same files. The diff ID stays stable, so the local store can identify a layer regardless of how it was shipped.
The image ID is the digest of the config blob. Because the config contains every diff ID, any change to any layer changes the config, which changes the image ID.
Tags are pointers, digests are identities
A tag such as node:20-slim is a mutable name. The registry maps it to a manifest digest, and the maintainers can repoint it at any time. A digest reference cannot be repointed, because it is the hash of the manifest.
docker pull alpine:3.20
# The digest the registry resolved this tag to
docker image inspect --format '{{index .RepoDigests 0}}' alpine:3.20
# Show the index and per-platform manifests
docker buildx imagetools inspect alpine:3.20
# Pin to the exact digest printed above (replace the placeholder)
docker pull alpine@sha256:<digest-from-the-previous-command>In a Dockerfile you can pin the base image the same way, and keep the tag for readability:
FROM alpine:3.20@sha256:<digest>When both are present, Docker resolves by digest and ignores the tag for selection. The tag only documents your intent.
Pinning gives reproducible base images. It also means you stop receiving security patches until you update the digest. Many teams pin and then use an automated dependency tool to bump the digest.
Verification on pull and push
When a client pulls an image, it follows this sequence:
- Request the manifest by tag or digest.
- If the result is an image index, choose the entry matching the local platform and fetch that manifest.
- Fetch the config blob and each layer blob by digest.
- Hash each downloaded blob and compare it with the digest that named it.
A mismatch fails the pull. This is why a corrupted download or a tampered mirror is detected without any extra signing step. Note the limit: the check proves the bytes match the name. It does not prove that the name came from a publisher you trust. For that you need signatures, which are outside this chapter.
Pushing runs the same logic in reverse. The client asks the registry whether a blob with a given digest already exists. If it does, the upload is skipped. Layers shared with other images in the same repository, or mounted from another repository the client can read, are never sent twice.
Gotchas
Re-serializing a manifest changes its digest. The digest covers the exact bytes. If a tool parses a manifest and writes it back with different whitespace or key order, the result is a different digest, even though the meaning is identical. Never reformat a manifest and expect the old digest to hold.
Rebuilding the same Dockerfile often yields new layer digests. A build that misses the cache re-runs the instruction. The resulting tar usually contains different file timestamps, so the digest differs even when the files look the same. The build cache does not decide hits by comparing layer digests. It compares the instruction and its inputs, as covered in the chapter on cache hits and misses. For COPY, the inputs are checksums of the file contents. Content addressing identifies the output of a build. The cache decides whether to produce that output again.
The local store may use different digests than the registry. The classic Docker image store keeps layers uncompressed, so after a pull only diff IDs and the manifest digest exist locally. The containerd image store keeps the original compressed blobs. Output from docker image inspect and docker image ls --digests can therefore differ between Docker installations. Compare digests only between like things: manifest to manifest, diff ID to diff ID.
Identical content under two names is one blob. Two unrelated images that both start from the same base layer share that blob on disk and in the registry. Deleting one image removes only the blobs that no other image still references.
Summary
- Every blob, manifest, and config is named by the SHA-256 of its bytes.
- A manifest lists a config and ordered layers. An index lists manifests for several platforms.
- The layer digest hashes the compressed archive. The diff ID hashes the uncompressed tar. The image ID is the config blob's digest.
- Tags move. Digests do not.
- Clients verify every blob against its digest, and skip transfers for blobs that already exist.
Exercise
You run docker image inspect --format '{{json .RootFS.Layers}}' myapp:dev and then look at the same image's manifest in the registry. None of the layer digests match. Is the image corrupted? Explain what you are comparing.
Show answer
No. The inspect command prints diff IDs, which are SHA-256 hashes of the uncompressed layer tars from the config blob. The registry manifest lists layer digests, which are hashes of the compressed blobs as stored and transferred. Compression changes the bytes, so the two sets of digests differ by design. To check for corruption, compare each registry blob with its own digest, or compare diff IDs with diff IDs.
Exercise
A teammate says FROM python:3.12-slim is safe for reproducible builds because the version is fixed. What can still change between two builds, and how do you prevent it?
Show answer
The tag python:3.12-slim is a mutable pointer. The maintainers can push a new image under it, for example to include a security patch, so two builds may start from different base layers. To prevent this, pin the manifest digest: FROM python:3.12-slim@sha256:<digest>. Use the digest of the image index so it works on every platform. You then need to update the digest on purpose to receive base image patches.