Sub topic 14
Pinning digests, scanning, and keeping base images current
This chapter assumes you can write a multi-stage Dockerfile and that you know the difference between scratch, distroless static, and distroless base. Minimal images have few packages, but they still come from a registry and they still age. Three habits keep them trustworthy: pin by digest, scan, and rebuild on a schedule.
Why tags are not enough
A tag such as gcr.io/distroless/static-debian12:nonroot is a movable label. The registry owner can point it at a new image at any time. Two builds a week apart can pull different bases without any change in your Dockerfile.
A digest is a SHA-256 hash of the image manifest. It names exactly one image. If the content changes, the digest changes.
Find and use a digest
Pull the image, then read its digest.
docker pull gcr.io/distroless/static-debian12:nonroot
docker inspect --format='{{index .RepoDigests 0}}' gcr.io/distroless/static-debian12:nonrootThe output looks like gcr.io/distroless/static-debian12@sha256:.... Put the digest after the tag in the FROM line. Docker uses the digest and ignores the tag, but the tag stays as a readable note.
FROM golang:1.22@sha256:REPLACE_WITH_DIGEST AS build
WORKDIR /src
COPY go.mod ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/app .
FROM gcr.io/distroless/static-debian12:nonroot@sha256:REPLACE_WITH_DIGEST
COPY --from=build /out/app /app
EXPOSE 8080
ENTRYPOINT ["/app"]Replace each REPLACE_WITH_DIGEST with the real value from your own pull. Pin the builder stage too, because a changed compiler affects the binary you ship.
Pinning has a cost
A pinned digest never changes, so it also never receives security fixes. Pinning without a process to update it freezes your vulnerabilities in place. Pair it with automation:
- Dependabot and Renovate can open pull requests that bump the digest when a new image appears.
- Each bump goes through your normal build, test, and review.
- You get a reviewable record of when the base changed.
Scan images
A scanner reads the packages and libraries in an image and compares them with vulnerability databases. Common open source scanners are Trivy and Grype.
trivy image --severity HIGH,CRITICAL myapp:1.0Know what a scan can and cannot see on a minimal image:
- Distroless images carry a package database, so scanners can list their OS packages.
- A
scratchimage has no package database. A scanner sees only your binary. - For Go and Rust binaries, scanners read the module list embedded in the executable, or an SBOM. Go embeds build information by default. If you strip aggressively or remove it, results get worse.
- A clean scan means no known issues, not no issues.
A software bill of materials (SBOM) is a list of the components in an image. Generate one at build time so you can answer "are we affected?" without rescanning everything.
Keep images current
Rebuild on a schedule, not only when your code changes. A weekly rebuild picks up new base image contents and fixed dependencies. A new CVE in a base layer is fixed by a rebuild, not by a code change.
A workable routine:
- Renovate or Dependabot proposes a new base digest.
- CI builds the image and runs your tests.
- CI scans the image and fails on fixed critical or high findings.
- You merge and deploy.
Also rebuild when your Go toolchain gets a security release. The standard library is compiled into your binary, so a fixed net/http reaches you only after you rebuild with the new compiler.
Exercise
Your Dockerfile pins the distroless base by digest. Three months later a scanner reports a fixed critical vulnerability in that base. What is the cause, and what is the fix?
Show answer
The digest never moves, so the image still contains the old, vulnerable layer. Update the digest to the current image (ideally through a Renovate or Dependabot pull request), rebuild, test, and redeploy. Add a scheduled rebuild so this does not depend on someone noticing.