Sub topic 14
Pinning images by digest
This chapter assumes you can build and run images, know that an image is made of layers plus a config and a manifest, and have read the chapter on digests and content addressing. It also assumes you know the registry/repository:tag naming scheme. Here you will learn why tags are unreliable as identifiers, how to reference an image by digest, and how to keep pinned images up to date.
Why tags are not enough
A tag such as alpine:3.20 is a label stored in a registry. The registry maps that label to a manifest digest. The mapping can change. A maintainer can push a patched image under the same tag, and the label then points to different content.
This causes several problems:
- Two builds run a week apart can start from different base images, even though the Dockerfile has not changed.
- A bug fix or a vulnerability can appear or disappear without any change in your repository.
- If someone gains push access to a repository, they can move a tag to a malicious image. Your next pull would fetch it.
- You cannot tell from a Dockerfile or a deployment file exactly which bytes ran in production.
A digest does not have this problem. It is a SHA-256 hash of the manifest, so a different image always has a different digest. Changing the image content changes the digest.
The following diagram shows the difference.
Finding a digest
You can read the digest of a local image with docker images --digests. This works for images that were pulled from or pushed to a registry. A locally built image has no registry digest until you push it.
docker pull alpine:3.20
docker images --digests alpineThe output has a DIGEST column that shows a value such as sha256:.... To read the digest without pulling, ask the registry directly:
docker buildx imagetools inspect alpine:3.20The first lines of the output include a Digest: field. For a multi-platform image, this is the digest of the image index, not of one platform's image. Pinning the index digest still gives you the correct platform on each machine, which is usually what you want.
Pulling by digest
Use @ instead of : to separate the repository from the digest:
docker pull alpine@sha256:<digest>Replace <digest> with a real value. The Docker client checks that the manifest it receives hashes to this digest. It also checks each layer against the digest listed in the manifest. If anything differs, the pull fails. This is why a digest protects you even if the registry or the network is compromised.
You can also use both a tag and a digest:
alpine:3.20@sha256:<digest>When both are present, Docker uses the digest and ignores the tag. The tag is kept only as a human-readable hint. If the tag later points elsewhere, the reference still resolves to the pinned content.
Pinning in a Dockerfile
Pin the base image in the FROM line. In a multi-stage build, pin every stage's base image.
FROM golang:1.22@sha256:<digest> AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12@sha256:<digest>
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Now the build is repeatable in one respect: the starting point is fixed. The build as a whole is still not fully reproducible. Commands such as apt-get install or go mod download can fetch different content over time, and file timestamps differ between builds. Pinning removes one source of change but not all.
Pinning at deploy time
Digests matter most where an image is deployed. Compose files, Kubernetes manifests, and scripts that call docker run should reference the digest of the image that was tested:
docker run --rm registry.example.com/team/myapp@sha256:<digest>A common workflow is:
- CI builds the image and pushes it with a tag, for example
myapp:1.0. - CI records the digest that the push reports.
- Tests run against that digest.
- The same digest is promoted to staging and production.
This guarantees that production runs the same bytes that passed the tests. You can still keep human-friendly tags next to the digest for browsing.
Trade-offs and pitfalls
Pinning has a cost. A pinned base image does not receive security patches by itself. If you pin and never update, you slowly accumulate known vulnerabilities. Digests give you control, and you then have to take on the job of updating them.
Other points to watch:
- Digests are hard to read. A line of 64 hex characters tells a reviewer nothing. Keep the tag next to it (
name:tag@sha256:...) so people can see the intended version. - Digests differ per build. Rebuilding the same source can produce a different image digest, because of timestamps and other metadata. Do not expect to compute a digest from source.
- Index digest versus platform digest. The digest of a multi-platform image index differs from the digest of each platform image inside it. Mixing them up is a common cause of "manifest unknown" errors.
- Registry cleanup can delete pinned content. If a retention policy removes untagged manifests, an image referenced only by digest may be deleted. Keep a tag on anything you pin, or exempt it from cleanup.
- Local images.
docker pull name@sha256:...will not match a locally built image that was never pushed. Local builds have an image ID, which is a different value from the registry manifest digest.
Keeping pins current
To avoid stale pins, automate updates. Tools such as Dependabot and Renovate read the FROM line, check the registry for a newer image under the same tag, and open a pull request that replaces the digest. A human or a test suite then reviews the change. This gives you the best of both approaches: every change is deliberate, visible in version control, and tested.
A reasonable policy is:
- Pin production images and base images by digest.
- Use tags in local experiments and development, where convenience matters more.
- Run automated update checks at least weekly, and more often for security fixes.
Summary table
| Reference | Example | Can change over time? | Verified by client? |
|---|---|---|---|
| Tag | `alpine:3.20` | Yes | No |
| Digest | `alpine@sha256:<digest>` | No | Yes |
| Tag and digest | `alpine:3.20@sha256:<digest>` | No, digest wins | Yes |
Exercise
A teammate says: "We pin FROM node:20@sha256:<digest>, so we never need to think about base image updates again." Name two problems with this and say how you would fix them.
Show answer
First, the pinned base image never receives security patches, so known vulnerabilities accumulate. Second, the digest is unreadable and can be forgotten, so nobody notices it is stale. Fix both by using an automated tool such as Dependabot or Renovate to open pull requests that update the digest, and by keeping the tag in the reference (node:20@sha256:...) so the intended version is visible.
Exercise
You run docker pull registry.example.com/team/myapp:1.0@sha256:<digest>, and the tag 1.0 has since been moved to a different image. Which image do you get, and why?
Show answer
You get the image identified by the digest. When a reference contains both a tag and a digest, Docker resolves by the digest and ignores the tag. The client also verifies that the content hashes to that digest.