Sub topic 13
Tag immutability, digest pinning, and vulnerability scanning
This chapter assumes you can push and pull images with ECR or GHCR and that you know what an image reference looks like. It covers three controls that keep a deployed image the same as the one you tested.
Why tags are not enough
A tag such as 1.0.0 is a movable label. Anyone with push access can point it at a different image. A pipeline that deploys my-app:1.0.0 today may get different bytes tomorrow, and nothing in the reference tells you.
A digest is a SHA-256 hash of the image manifest. It identifies content, so it cannot be repointed. If the content changes, the digest changes.
Tag immutability
Tag immutability makes a registry reject any push to a tag that already exists. ECR supports it per repository. The repository in the next command also has scan-on-push turned on.
aws ecr create-repository \
--repository-name my-app \
--image-tag-mutability IMMUTABLE \
--image-scanning-configuration scanOnPush=true \
--region us-east-1Pushing 1.0.0 a second time now fails with ImageTagAlreadyExistsException. This forces a new tag for each build, and the commit SHA is a good choice.
Immutability has a cost. Moving tags such as latest no longer work in that repository. Use unique tags for releases, or keep mutable tags in a separate repository.
GHCR has no immutability setting. There you rely on unique tags and on digests.
Digest pinning
Pinning means deploying by digest instead of by tag. First, look up the digest of the image you tested.
aws ecr describe-images \
--repository-name my-app \
--image-ids imageTag=1.0.0 \
--query 'imageDetails[0].imageDigest' \
--output text \
--region us-east-1For any registry, docker buildx imagetools inspect ghcr.io/your-github-username/my-app:1.0.0 prints the digest.
Then reference it in the manifest:
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: my-app
image: 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0@sha256:3f5a9c1e7b2d4860a1c3e5f708192a3b4c5d6e7f8091a2b3c4d5e6f708192a3bWhen both a tag and a digest are present, the runtime pulls by digest and ignores the tag. The tag stays in the reference so people can read it.
The usual flow is to build, push, scan, record the digest, and deploy that digest.
Vulnerability scanning
A scanner compares the packages in each layer against databases of known vulnerabilities (CVEs). With scanOnPush=true, ECR scans each image as it arrives. Read the severity counts with:
aws ecr describe-image-scan-findings \
--repository-name my-app \
--image-id imageTag=1.0.0 \
--query 'imageScanFindings.findingSeverityCounts' \
--region us-east-1Scan results describe the image at the time of the scan. New CVEs are published later, so an image that was clean last month can have findings today. Rescan images that are already running, or use continuous scanning where your registry offers it. For GHCR, run a scanner such as Trivy in your CI workflow.
Common mistakes
- Pinning a digest once and never updating it, so security fixes never arrive. Automate digest bumps, for example with a dependency-update bot.
- Confusing the digest of a multi-architecture image index with the digest of one platform's image. Both are valid, but they differ.
- Treating a scan on push as a gate. It reports findings and does not block anything unless your pipeline checks the result and fails.
Exercise
Your ECR repository is immutable and a build tries to push my-app:latest for the second time. What happens, and what should the build tag with instead?
Show answer
The push fails with ImageTagAlreadyExistsException because the tag already exists. The build should push a unique tag, such as the commit SHA, and deploy by digest.