Sub topic 8
Scanning images for known vulnerabilities
This chapter assumes you can build images and run containers, and that you know what a registry is. It also assumes you have read the earlier chapters on base images and pinning. Scanning builds on both.
What a scanner does
A vulnerability scanner reads an image and lists the software inside it. It then compares that list with databases of published flaws. Each flaw has a CVE identifier (Common Vulnerabilities and Exposures), such as CVE-2024-12345.
The scanner inspects two kinds of content:
- OS packages, such as
opensslorlibc, read from the package manager's database (dpkg, apk, rpm). - Application dependencies, such as npm, pip, Go modules, or Maven packages, read from lockfiles or from metadata inside binaries.
Scanning does not run the image, and it does not prove that your container is safe. It only reports flaws that are already public and already in the database. It cannot find a bug nobody has reported, a weak configuration in your own code, or a leaked secret. Those need other controls from this tutorial.
Running a scan
Two common tools are Docker Scout, which is built into Docker Desktop and recent Docker CLI releases, and Trivy, an open-source scanner from Aqua Security. Either one gives you a list of findings.
docker scout cves myapp:1.0
# or, with an open-source scanner:
trivy image --severity HIGH,CRITICAL myapp:1.0Both commands list known vulnerabilities in the image's OS packages and application dependencies. The Trivy variant shows only high and critical findings.
A typical finding contains these fields:
| Field | Meaning |
|---|---|
| Package | The affected library or OS package and its installed version |
| CVE ID | The public identifier of the flaw |
| Severity | Low, medium, high, or critical, usually derived from a CVSS score |
| Fixed version | The first release that contains a fix, if one exists |
| Source | The layer or file where the package was found |
The fixed version is the most useful field. It tells you whether you can act on the finding.
Reading results
A raw report for a full debian or ubuntu image can list hundreds of CVEs. Most are noise for your workload. Triage the list with these questions:
- Is a fix available? If the fixed version column is empty, no patched package exists yet. You cannot update your way out of it.
- Is the package reachable? A flaw in a
tarutility matters little if your application never runs it and no attacker can reach a shell. A flaw in your HTTP library, which parses untrusted input, matters a lot. - Is the severity accurate for your context? A CVSS score measures a flaw in general. A "high" flaw that needs local access is less urgent for a service with no shell.
- Where does it come from? A finding in the base image is fixed by rebuilding on a newer base. A finding in
package-lock.jsonis fixed by updating that dependency.
The base image usually accounts for most findings. Switching from a full OS image to a slim or distroless one often removes most of the list without any other change. Chapter outlines on base images and multi-stage builds cover that approach.
Acting on results
Once you have triaged the list, there are four ways to respond:
- Rebuild on a newer base. Pull the latest patch release of the base tag and rebuild with
docker build --pull. This clears many OS-level findings. - Update the dependency. Raise the version in your lockfile to the fixed release, then rebuild and rescan.
- Remove the package. If you do not need the vulnerable package, delete it from the image. A multi-stage build keeps compilers and package managers out of the final stage entirely.
- Accept the risk, with a record. If a finding is not exploitable in your setup, or no fix exists, document it and suppress it.
Suppression should be explicit and reviewable. Trivy reads a .trivyignore file in the working directory, with one CVE ID per line:
# Not reachable: we do not ship the affected binary path. Review by 2027-01-31.
CVE-2024-12345Always include a reason and a review date as comments. Without them, an ignore file turns into a list nobody understands.
Gating builds in CI
A scan that nobody reads does nothing. Make the build fail when a scan finds something you have decided is unacceptable. Trivy returns exit code 0 by default, so you set --exit-code 1 to fail the job.
The script below builds an image, scans it, and stops the pipeline on fixable high or critical findings:
#!/usr/bin/env bash
set -euo pipefail
IMAGE="myapp:${GIT_SHA:-dev}"
docker build --pull -t "$IMAGE" .
trivy image \
--severity HIGH,CRITICAL \
--ignore-unfixed \
--exit-code 1 \
"$IMAGE"
echo "Scan passed for $IMAGE"Each flag has a purpose:
--severity HIGH,CRITICALlimits both the report and the gate to the levels you chose.--ignore-unfixedskips findings that have no patch. Without it, a flaw you cannot fix would block every build.--exit-code 1makes Trivy return a non-zero status when any remaining finding matches, which fails the job.
Docker Scout offers a similar filter. docker scout cves --only-severity critical,high --only-fixed myapp:1.0 shows only high and critical findings that have a fix.
For dashboards or code review integration, ask for machine-readable output with --format json or --format sarif, and write it to a file with --output.
Scan again after you ship
An image that was clean on Monday can have a critical finding on Friday. Nothing in the image changed. The database did. New CVEs are published daily and are matched against software you already deployed.
This means a build-time scan is not enough. Add two more checks:
- Scheduled rescans of images in the registry. Run the same scanner nightly against the tags you have deployed. Some registries and Docker Scout can do this continuously.
- Rebuilds on a schedule. Rebuild and redeploy active images regularly, for example weekly, so that base image patches reach production even when your code has not changed.
Pinned tags and digests make builds reproducible, but they also freeze known flaws in place. Pinning needs a matching process that updates the pin. Automated dependency tools such as Dependabot or Renovate can open pull requests when a new base digest appears.
Limits and failure modes
Know where scanners go wrong, so you do not trust them too much:
- False negatives from unknown packages. Software copied in as a bare binary, or built from source with no package metadata, may not be detected. A Go binary is detected only if it embeds module information.
- False positives from backported fixes. Distributions often patch a flaw without changing the upstream version number. A scanner that matches only on version may report a fixed package as vulnerable. Scanners that read the distribution's own security data handle this better.
- Stale databases. If the scanner runs offline or caches an old database, it misses recent CVEs. Check that the database updates before each run.
- Unsupported or end-of-life bases. Once a distribution release is end-of-life, it no longer receives fixes. The scanner may report few findings simply because no one is publishing advisories for it. The base is still unsafe.
- Scope. Scanning covers the image only. It does not check how you run the container, so a clean image can still run with
--privileged.
Exercises
Exercise
Your CI scan of myapp:2.1 reports a critical CVE in libexpat. The "fixed version" column is empty. Should the build fail, and what should you record?
Show answer
With --ignore-unfixed, the build should not fail, because no patched package exists and you cannot fix it by updating. You should still record the finding: add it to the ignore file or risk register with a reason and a review date, and check again when a fix is published. You can also reduce exposure by removing the package or switching to a base image that does not include it.
Exercise
An image passed its scan when you deployed it three weeks ago. A new scan today reports a critical finding in the same image digest. Explain why, and name two practices that catch this.
Show answer
The image did not change, but the vulnerability database did. A CVE was published after your deployment and now matches a package already in the image. Scheduled rescans of images in the registry detect it, and regular rebuilds on a patched base image deliver the fix. Automated pull requests that update the pinned base digest also help.