Sub topic 13
Fixing findings: base images and multi-stage builds
This chapter assumes you can read docker scout cves or trivy image output and know what CVE IDs, severities and fixed versions mean. It also assumes you can write a basic Dockerfile. Here you will reduce findings by changing what goes into the image.
Where findings come from
A scanner reports packages that are in the image. Most findings come from three places:
- The base image's operating system packages.
- Language dependencies you install, such as pip or npm packages.
- Build tools left in the final image, such as compilers and package managers.
The fastest fixes remove or replace packages. Patching individual CVEs by hand comes last.
Step 1: Update or change the base image
Start with the base image, because it often accounts for most of the findings. Ask Docker Scout for suggestions:
docker scout recommendations nginx:latestThe output lists newer tags of the same image and alternatives with fewer vulnerabilities. Then confirm the effect with a scan, using the fix filter from the earlier chapter:
trivy image --severity HIGH,CRITICAL --ignore-unfixed python:3.12-slimPrefer these changes, from least to most disruptive:
- Rebuild on the latest patch release of the same tag. Many fixes arrive this way.
- Move to a
-slimor Alpine variant, which installs fewer packages. - Move to a distroless image, which has no shell or package manager.
Step 2: Use a multi-stage build
A multi-stage build uses one stage to compile and a second stage to run. Only the files you copy across reach the final image. Compilers, headers and package managers stay behind, so they cannot be reported.
FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /app .
FROM gcr.io/distroless/static-debian12
COPY --from=build /app /app
ENTRYPOINT ["/app"]The golang:1.22 image contains hundreds of packages. The final stage contains only the distroless base and your binary. CGO_ENABLED=0 produces a static binary, which is why it can run on the static base.
Step 3: Update language dependencies
If a finding is in a package such as requests or lodash, the base image is not the cause. Raise the version in your manifest to at least the fixed version the scanner shows. Then rebuild and rescan. Pin versions in a lock file so the next build does not change them without you noticing.
Step 4: Verify with a comparison
Check that the change helped and did not add new findings. Compare the new build with the old one:
docker scout compare --to myapp:1.0 myapp:2.0The output lists vulnerabilities that were added or removed. A fix that removes ten findings and adds three is still worth keeping, but you should know about the three.
When no fix exists
Some findings have no fixed version. You cannot patch them directly. Your options are to switch to a base image that does not include the package, remove the package, or accept the finding in your ignore file with a written reason. Accepting is covered in the triage chapter.
docker build --pull --no-cache -t myapp:2.0 .Exercise
A scan shows 40 high findings. All of them are in Debian packages that your Go binary never uses at runtime, and the image is built from golang:1.22. Which change is most likely to remove most of them, and why?
Show answer
Convert the Dockerfile to a multi-stage build that copies the compiled binary into a distroless or similarly minimal final image. The unused Debian packages and build tools are then not in the shipped image, so the scanner has nothing to report for them. Rescan afterward, and use docker scout compare to confirm the findings were removed.