Sub topic 14
Pinning Images by Digest
This chapter assumes you know what a tag is, how to push and pull images, and that a digest is a SHA-256 hash of an image's manifest. It shows how to reference an image by digest and where that choice causes problems.
Why tags are not enough
A tag is a movable label. The owner of alpine:3.20 can push a new image under that tag tomorrow, and your next docker pull will fetch different content. Rebuilds of the same Dockerfile can then produce different results, and you may not notice.
A digest cannot move. It is computed from the content of the manifest, so changing any byte of the image produces a different digest. A reference that includes a digest always names exactly one image.
Finding and using a digest
List the digests of images you have pulled:
docker images --digests alpineThen pull by digest instead of by tag:
docker pull alpine@sha256:<digest>Replace <digest> with a real value from the previous command. The registry returns the content that digest identifies. The Docker client checks the hash of what it receives, so a registry cannot silently substitute other content.
Pinning in a Dockerfile
You can pin a base image in the FROM line:
FROM alpine:3.20@sha256:<digest>Keeping the tag next to the digest helps readers see which version you meant. When a tag and a digest are both present, the digest decides which image is pulled and the tag is ignored.
Common errors
- Pinning the wrong digest for multi-platform images. The digest of an image index is not the digest of a single platform image. Pulling by an index digest still selects the right platform for your machine. Pulling by a platform-specific digest locks you to one architecture.
- Copying a digest from the wrong place. A local image ID is not a registry digest. Use
docker images --digestsor your registry's interface. - Never updating the pin. A pinned base image stops receiving security fixes until you change the digest. Pinning trades automatic updates for repeatability.
- Expecting a digest after a rebuild to match. Rebuilding and pushing often yields a new digest, even for the same source, because layer metadata such as timestamps can change.
When to pin
| Situation | Reference to use |
|---|---|
| Local experiments | Tag |
| Production deployments | Digest |
| Base images in release builds | Tag plus digest |
| Images you publish for others | Publish tags and report the digest |
Exercise
Your Dockerfile has FROM alpine:3.20@sha256:<digest>. The maintainers push a patched image to alpine:3.20. Which image does your next build use, and what must you do to get the patch?
Show answer
The build still uses the image identified by the digest, because the digest decides the pulled image and the tag is ignored. To get the patch, find the new digest of alpine:3.20 and update the digest in the FROM line.