Sub topic 9
Digests and content addressing
This chapter assumes you know how layers stack, how images are named with tags, and how to run docker pull and docker build.
What content addressing means
A digest is an identifier computed from the bytes it names. Docker runs the content through SHA-256 and writes the result as sha256: followed by 64 hex characters. Identical content always gets the same digest. Changing one byte gives a completely different digest.
A tag such as myapp:1.0 is a label someone can move to a different image at any time. A digest cannot be moved, because it is derived from the content itself.
Which things have digests
Several objects in an image are addressed this way:
- Layer blob digest: the hash of the compressed layer archive as stored in a registry.
- Config digest: the hash of the image config JSON. Docker shows this as the local image ID.
- Manifest digest: the hash of the manifest, the JSON file that lists the config and layers by digest. This is the digest you use in
image@sha256:...references.
The manifest names every layer by digest, and the digest of the manifest covers those names. So the manifest digest identifies the whole image.
Seeing digests
Pull an image, then list its digests:
docker pull alpine:3.20
docker images --digests alpine
docker image inspect --format '{{index .RepoDigests 0}}' alpine:3.20RepoDigests holds the manifest digest the registry reported. The image ID from docker image inspect --format '{{.Id}}' is a different value, because it hashes the config and not the manifest.
Why this is useful
Content addressing gives you three things:
- Deduplication. Two images that share a layer share one blob. A registry stores it once, and
docker pullskips blobs you already have. - Integrity. After a download, the client hashes the bytes and compares the result to the expected digest. A corrupted or tampered blob fails the check.
- Reproducible references. Pulling by digest always returns the same content, even if the tag has since moved.
Common errors
- Assuming a tag is stable.
alpine:3.20can point to a new image after a security rebuild. Use a digest when you need exact content. - Expecting identical rebuilds to match. Timestamps, file ordering, or a changed base image give a different layer digest. Matching source code does not guarantee a matching digest.
- Mixing up the IDs. The local image ID, the layer digests, and the manifest digest are three different hashes. A digest that works in
image@sha256:...must be the manifest digest, not the image ID. - Comparing compressed and uncompressed hashes. The same layer hashes differently before and after compression. Registry digests refer to the compressed form.
Exercise
You pushed myapp:1.0 yesterday. Today a teammate pushes a different build under the same tag. Your deployment file references myapp@sha256:abc... using yesterday's manifest digest. Which image does the deployment pull, and why?
Show answer
It pulls yesterday's image. The digest is a hash of the manifest content, so it identifies exactly that image. The tag moved to a new manifest with a different digest, but the digest reference does not follow tags.