Sub topic 10
Pinning an Image by Digest
This chapter assumes you can build, tag, and push images, and that you know how a tag differs from a repository name. Tags are labels that can be moved. A digest is a fingerprint of the content, so it cannot move. Here you will see how to find a digest and use it to pin a deployment.
What a digest is
A digest is a SHA-256 hash of an image's manifest. The manifest is a small JSON document that lists the image's configuration and layers. If any byte of the image changes, the digest changes. Nobody can push different content under the same digest.
A reference that uses a digest looks like this:
registry.example.com/team/myapp@sha256:3f1c...e9a2The @ replaces the : that introduces a tag. A tag names a label. A digest names content.
Finding the digest
Registries assign the digest when you push. Docker records it locally after a successful push or pull. This command prints it:
docker inspect --format '{{index .RepoDigests 0}}' registry.example.com/team/myapp:1.4.2The output is a full reference in the form repository@sha256:.... You can paste it into a deployment file.
The RepoDigests list is empty for an image that was built locally and never pushed or pulled. In that case the command fails with an index error. Push the image first.
Pinning in practice
You can use the digest anywhere Docker accepts an image reference.
docker pull registry.example.com/team/myapp@sha256:3f1c...e9a2
docker run registry.example.com/team/myapp@sha256:3f1c...e9a2Replace the ... with the full 64-character hash. The shortened form above is only for readability.
In a Dockerfile, pin a base image the same way:
FROM python:3.12-slim@sha256:3f1c...e9a2When both a tag and a digest are present, Docker resolves by digest and ignores the tag. The tag stays in the line as a note for readers. Because the tag is ignored, it can become wrong without any error. If someone updates the digest but forgets the tag, the line misleads.
Tag or digest
The tradeoffs:
| Property | Tag such as `1.4` | Digest |
|---|---|---|
| Readable | Yes | No |
| Can change meaning | Yes | No |
| Gets patches automatically | Yes, if re-pushed | No |
| Reproducible deploys | Not guaranteed | Guaranteed |
Pinning gives you reproducibility and removes surprise. The cost is that you stop receiving updates. A pinned base image will not pick up a security patch until you change the digest.
Keeping pins fresh
Pin, then update on purpose. A common pattern is to let a tool propose updates. Dependabot and Renovate can open a pull request when a tag such as python:3.12-slim gets a new digest. A reviewer sees the change and your tests run before it merges.
For your own images, record the digest at release time. A release script can read it after the push and write it into the deployment configuration. Humans still refer to 1.4.2, while the cluster runs the exact digest that 1.4.2 pointed to at release.
Gotchas
- Multi-architecture images have two kinds of digest. The digest of the index (the list of per-platform manifests) is stable across platforms. The digest of a single platform's manifest is different. Pinning the index keeps multi-platform support. Pinning a platform manifest locks you to one architecture.
- A digest can be deleted. If a registry retention policy removes an untagged image, a pinned reference to it will fail to pull. Keep a tag on anything you deploy.
- The digest you see locally can differ from the registry's if you rebuild. Always read it after pushing, not after building.
Exercise
Your deployment file contains myapp:1.4@sha256:aaa.... You re-push a new image under the tag 1.4. Which image runs on the next deploy, and why?
Show answer
The image with digest sha256:aaa... runs. When a reference has both a tag and a digest, Docker resolves by the digest and ignores the tag. The re-pushed tag does not change what the digest points to.
Exercise
A teammate runs docker inspect --format '{{index .RepoDigests 0}}' myapp:dev on an image they just built locally, and gets an error. Why, and how do they fix it?
Show answer
A locally built image has no repository digest until it is pushed to or pulled from a registry, so RepoDigests is empty and indexing it fails. Pushing the image to a registry creates the digest, after which the command works.