Sub topic 8
Scanning Images for Known Vulnerabilities
This chapter assumes you can build images and write a Dockerfile, and that you know what a registry is. It also assumes you have read the chapters on base images and pinning. Scanning builds on both.
What a scanner does
An image scanner unpacks an image's layers and builds an inventory of what is inside. It looks at OS packages (such as apt or apk packages) and application dependencies (such as npm, pip, or Go modules). It then compares each package and version against vulnerability databases. A match is reported as a CVE (Common Vulnerabilities and Exposures), which is a public identifier like CVE-2024-12345 for a known flaw.
A scanner can only find flaws that are already published. It does not find bugs in your own code, misconfigurations at runtime, or unknown flaws.
Running a scan
Docker Scout and Trivy both read the image directly.
docker scout cves myapp:1.0
# or, with an open-source scanner:
trivy image --severity HIGH,CRITICAL myapp:1.0The Trivy --severity flag limits output to high and critical findings. Without it, the report can run to hundreds of lines and hide the items that matter.
Each finding usually shows the package, the installed version, the severity, and the fixed version if one exists. The fixed version tells you what to do next.
Reading the results
Not every finding needs the same response. Ask three questions:
- Is a fix available? If the field is empty, you cannot patch it yet.
- Is the vulnerable code reachable? A flaw in a library your application never loads is lower risk than one in your web server.
- Is it in the final image? Build tools in an earlier stage do not ship, so they do not count.
The last question connects to multi-stage builds. A smaller final image has fewer packages, so it has fewer findings.
Acting on findings
Fixes usually fall into one of these categories:
| Finding is in | Typical fix |
|---|---|
| Base image OS package | Pull a newer base image tag or digest and rebuild |
| Application dependency | Upgrade the dependency in your lockfile |
| Package you do not need | Remove it, or switch to a smaller base such as distroless |
| No fix published | Document the risk, monitor, and consider an alternative package |
Rebuilding is what applies a fix. A running container does not update itself, so you must rebuild the image and redeploy.
Scanning in CI and over time
Run a scan in your CI pipeline after the build. Most scanners can return a non-zero exit code when findings meet a threshold, which fails the pipeline. With Trivy, add --exit-code 1 to the command above.
Scan again after you publish. A clean image today can have new CVEs next week, because databases keep growing while your image stays the same. Schedule rescans of images that are deployed, not only images being built.
Common mistakes
- Treating a clean scan as proof of safety. It only means no known CVEs matched.
- Failing builds on every severity. Teams then ignore or disable the check. Start with critical and high findings that have fixes.
- Scanning only once, at build time.
- Using a floating tag like
latest, so two scans of the "same" image cover different content. Pin tags or digests so results can be reproduced. - Suppressing findings without a record. If you ignore a CVE, write down why and set a review date.
Exercise
A scan of your image reports a critical CVE in an OS package. The report shows no fixed version. What are two reasonable responses, and why does running docker pull on the same tag not help?
Show answer
Two responses: remove the package if the application does not need it (or move to a smaller base image such as distroless that does not include it), or document the risk and rescan regularly until a fix is published. docker pull does not help because no fixed package exists yet, so a newer image of the same tag still contains the vulnerable version. Even when a fix does exist, you must rebuild and redeploy for it to take effect.