Skip to content

Small images and vulnerability scanning

Select an icon to see what it is.

Step 6 of 6

Fix by rebuilding smaller and newer

Most findings are in packages you do not use. A smaller base image removes them, and a rebuild picks up fixed versions. Scan again after the rebuild, and run the scan in CI so a new finding fails the build.

PrevNext

Routine

1. pick a smaller or newer base image
2. docker build --pull -t web:1.1 .
3. scan again, expect fewer findings
4. fail the CI job on HIGH or CRITICAL findings

Try it yourself

Compare two Dockerfiles, then scan an image. The scan mounts the Docker socket into the scanner, which gives it control of your Docker host, so use it only with an image you trust.

$ printf 'FROM debian:bookworm-slim\nRUN apt-get update && apt-get install -y curl\nRUN rm -rf /var/lib/apt/lists/*\n' > Dockerfile.a
$ printf 'FROM debian:bookworm-slim\nRUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*\n' > Dockerfile.b
$ docker build -f Dockerfile.a -t sizea . && docker build -f Dockerfile.b -t sizeb .
$ docker images --filter 'reference=size*'
$ docker history sizea
$ docker pull alpine
$ docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy image alpine

Quiz

1. Why does a RUN that deletes files not shrink an image?
2. What does a scanner read to learn what is installed?
3. What is usually the best first fix for many findings?