Sub topic 9
Digests and content addressing
This chapter explains how Docker identifies image content by hash instead of by name. It assumes you can build and run images, know that images are made of layers, and have used tags such as myapp:1.0. You should be comfortable reading command output in a terminal.
Names can change, content cannot
A tag such as alpine:3.20 is a label. The registry lets its owner move that label to a different image at any time. Pulling alpine:3.20 today and again next month can give you two different images.
A digest identifies content. It is the result of running a hash function over bytes, so the same bytes always give the same digest, and different bytes give a different one. This idea is called content addressing: the address of a thing is derived from the thing itself.
What a digest looks like
A digest has two parts, an algorithm name and a hex string.
sha256:3f2b9c1e5a7d4c8b0e6f1a2d9c4b7e8f5a0d3c6b9e2f1a4d7c0b3e6f9a2d5c8bSHA-256 produces 32 bytes, written as 64 hexadecimal characters. Docker uses it for layers, image configs, and manifests. The hash covers exact bytes, so changing a single bit produces a completely different digest. There is no way to tell from two digests how similar the two contents are.
What gets a digest
An image is several objects, and each one has its own digest.
| Object | What it is | What its digest identifies |
|---|---|---|
| Layer blob | A compressed tar archive of file changes | The compressed bytes sent to and from a registry |
| Image config | JSON with environment, entrypoint, history, and layer list | The config document |
| Manifest | JSON listing the config and the layers | The manifest document |
| Image index | JSON listing manifests for several platforms | The index document |
The manifest refers to the config and each layer by digest. Because the manifest contains those digests, its own digest changes if any layer or the config changes. The manifest digest therefore acts as a fingerprint for the whole image. This is also the digest you normally use to reference an image.
Two kinds of layer digest
A layer has two digests, and mixing them up causes confusion.
- The diff ID is the hash of the uncompressed tar. It is stored in the image config, and Docker uses it to identify a layer on your machine.
- The blob digest is the hash of the compressed tar. The manifest lists it, and registries use it to store and fetch the layer.
The two differ because compression changes the bytes. Compressing the same tar with a different tool or level gives a different blob digest but the same diff ID. So two builds of identical files can produce different blob digests if they were compressed differently.
Viewing digests
Build an image, then ask Docker for its ID.
docker build -t myapp:1.0 .
docker history myapp:1.0
docker image inspect --format '{{.Id}}' myapp:1.0With the default image store, the ID printed by inspect is the digest of the image config. With the containerd image store, which newer Docker Desktop and Engine versions can use, the ID is the digest of the manifest or index. Do not assume the two are the same across setups.
To see the digest a registry knows, list images with the --digests flag.
docker images --digestsThe DIGEST column is empty for images that were built locally and never pushed or pulled. A local image has no registry digest until a registry has stored its manifest.
Where the registry digest comes from
The manifest digest is computed when a manifest is serialized. A push sends the layers and config, then the manifest. The registry stores the manifest and reports its digest in the output of docker push, on a line such as 1.0: digest: sha256:... size: 1570.
Pushing the same image to two registries gives the same manifest digest when both receive the same manifest bytes. Registries do not recompute the digest from a changed document. They hash what they receive.
Referencing an image by digest
Add @ and the digest after the repository name.
docker pull alpine@sha256:<digest>Replace <digest> with a real value, for example one shown by docker images --digests. A reference by digest cannot move. If the registry returns content whose hash does not match, Docker rejects it. This gives you two guarantees: you get exactly the image you asked for, and nobody can alter it in transit or on the registry without the change being detected.
You can include both a tag and a digest, as in alpine:3.20@sha256:<digest>. Docker resolves the image by the digest and ignores the tag. The tag stays as a readable note for people. If the tag has since moved elsewhere, the pull still gives the digest's content.
Deduplication and cache reuse
Because layers are addressed by content, Docker can share them automatically.
- On a registry, a layer with a given digest is stored once, no matter how many images use it.
- During a push, Docker asks the registry whether each blob already exists. It uploads only the missing ones.
- During a pull, Docker skips layers it already has locally.
- Two images built from the same base share the base layers on disk.
This is why pushing a small change to a large image is fast. Only the changed layers and the new manifest travel over the network.
Verification when you pull
When Docker downloads a blob, it hashes the bytes as they arrive and compares the result with the digest in the manifest. A mismatch fails the pull. This check applies to every pull, whether you use a tag or a digest. The difference is the starting point: with a tag, you trust the registry to give you the right manifest. With a digest, you supply the expected manifest hash yourself, so the whole chain is verified from your reference.
Common errors and gotchas
- Expecting tags to be stable. Tags such as
latestand3.20are mutable. A rebuilt image pushed under the same tag gets a new digest, and the old one becomes untagged, though it can still be pulled by digest as long as the registry keeps it. - Comparing the wrong digest. The image ID, the config digest, a layer diff ID, and the manifest digest are different values. Compare like with like.
- Using a digest from a multi-platform image on the wrong level. The digest of an image index is not the digest of any single platform's manifest. Pulling the index digest picks the platform for your machine. Pulling a platform manifest digest gives exactly that platform.
- Assuming rebuilds are reproducible. Identical Dockerfiles often produce different digests because timestamps, file ordering, or downloaded packages differ. Content addressing records what was built. It does not make builds repeatable.
- Copying a digest from the wrong place. A digest seen on one registry for a pushed image matches the manifest bytes sent. If a tool rewrites the manifest, such as converting its format, the digest changes.
Exercises
Exercise
You pull app:2.0 on Monday and record its digest. On Friday, pulling app:2.0 gives a different digest. What happened, and what reference would have given you Monday's content on Friday?
Show answer
The owner pushed a new image under the tag 2.0, so the tag now points to a different manifest. Monday's content would still be available by using the digest, for example app@sha256:<Monday digest>, as long as the registry has not deleted that manifest.
Exercise
Two builds compress the same layer files with different compression levels. Which digest stays the same, the diff ID or the blob digest, and why?
Show answer
The diff ID stays the same because it hashes the uncompressed tar, which has identical contents. The blob digest changes because it hashes the compressed bytes, and different compression produces different bytes.