Sub topic 17
Image Security Basics
This chapter assumes you can write a Dockerfile, build and tag images, and push them to a registry. It covers the habits that reduce risk in the images you build and run.
Start from a small, trusted base
Every package in your image is something that can contain a vulnerability. A minimal base such as gcr.io/distroless/static-debian12 has no shell and no package manager, so there is less to patch and less for an attacker to use. Choose bases from official or verified publishers, and avoid latest so you know which base you built on.
The multi-stage Dockerfile from earlier chapters already follows this pattern. Compilers and build tools stay in the build stage, and only the binary reaches the final image.
Pin what you depend on
A tag is a movable label. The publisher can push a new image under alpine:3.20 tomorrow, and your next build will silently use it. A digest identifies exact content, so pulling by digest gives you the same bytes every time.
docker pull alpine@sha256:<digest>Pinning trades convenience for control. You must update the digest on purpose to receive security fixes, so pair pinning with a regular rebuild schedule.
Do not run as root
Containers run as root by default. If an attacker breaks into the process, root inside the container gives them more room to work. Create an unprivileged user and switch to it.
FROM alpine:3.20
RUN addgroup -S app && adduser -S app -G app
USER app
CMD ["id"]Distroless images also provide a nonroot variant, such as gcr.io/distroless/static-debian12:nonroot.
Keep secrets out of images
Every layer is stored permanently and can be read by anyone who can pull the image. Deleting a file in a later layer does not remove it from the earlier one.
BuildKit can expose a secret to a single RUN step without writing it into a layer:
# syntax=docker/dockerfile:1
FROM alpine:3.20
RUN --mount=type=secret,id=token \
test -s /run/secrets/token && echo "token available"Build it with docker build --secret id=token,src=token.txt ..
Scan and update
A scanner compares the packages in your image against databases of known vulnerabilities. Run one in CI, for example docker scout cves myapp:1.0 or an open-source tool such as Trivy. A scan only reports what is known today, so scan images again after they are built, not just once.
Summary checklist
| Practice | Risk it reduces |
|---|---|
| Minimal base image | Large attack surface |
| Digest pinning | Unexpected content changes |
| Non-root `USER` | Privilege gained after a break-in |
| No secrets in layers | Credential leaks |
| Regular scans and rebuilds | Known vulnerabilities |
Exercise
A teammate deletes a file containing a password with RUN rm /app/password.txt in a later Dockerfile step. Why is the password still exposed, and what is a better approach?
Show answer
Each instruction creates a separate layer, and the earlier layer that added the file still holds it. Anyone who pulls the image can extract that layer. Instead, never copy the file into the image. Pass the secret at run time, or use a BuildKit secret mount, which does not write it into any layer.