Sub topic 9
Scanning an Image with trivy image
This chapter assumes you can pull and run Docker images, read a Dockerfile, and use a terminal. It also assumes Trivy is installed (covered in the previous chapter) and that you know what a CVE is. Here you run your first scan and learn to read the output.
What trivy image does
trivy image takes an image reference, builds an inventory of the packages inside it, and matches that inventory against Trivy's vulnerability database. If the image is not in your local Docker daemon, Trivy pulls it from the registry for you.
trivy image python:3.12-slimOn the first run, Trivy downloads its vulnerability database. Later runs reuse the cached copy and refresh it when it gets old. The scan prints a table of vulnerabilities for the operating system packages and any language dependencies Trivy finds.
How a scan proceeds
Because Trivy reads the packages that are present in the final filesystem, a package you installed in one layer and removed in a later layer does not appear in the report. A package that is still installed does, even if your application never uses it.
Reading the output
Trivy groups results by target. A typical image produces one section for the operating system and one more for each language ecosystem it detects, such as Python packages or Node modules. The header of each section shows the target name and the number of findings by severity.
Each row in a table has these columns:
| Column | Meaning |
|---|---|
| Library | The package that contains the vulnerable code |
| Vulnerability | The identifier, usually a CVE ID |
| Severity | LOW, MEDIUM, HIGH or CRITICAL (UNKNOWN when no rating exists) |
| Status | Whether a fix exists: fixed, affected, or will_not_fix |
| Installed Version | The version found in the image |
| Fixed Version | The version that contains the patch, if one exists |
| Title | A short description with a link to the advisory |
The Fixed Version column drives your next step. If it shows a version, upgrading the package or rebuilding on a newer base image can resolve the finding. If it is empty, the vendor has not released a fix yet.
Scanning images you just built
You do not need to push an image before scanning it. Build locally, then scan the tag:
docker build -t myapp:dev .
trivy image myapp:devTrivy finds the image in the local Docker daemon. This lets you scan before anything reaches a registry.
Common errors
- Database download fails. Trivy needs network access to fetch its database on the first run. Behind a proxy or in an offline environment, you must supply the database another way, or the scan stops with an error.
- Image not found. A typo in the tag, or a private registry without credentials, produces a pull error. Run
docker loginfirst for private registries. - Zero findings on an unusual image. Trivy can only report what it can identify. An image built from
scratchthat contains a statically compiled binary may show no operating system packages at all. An empty report means nothing was matched, which is not proof that the image is safe. - Long, noisy output. A full scan of a large image can list hundreds of rows. The next chapter shows how to narrow the report by severity and fix status.
Exercise
You scan an image with trivy image. One row shows severity HIGH, an installed version of 1.2.3 and an empty Fixed Version column. What does the empty column tell you, and what can you not do yet?
Show answer
It tells you that no patched version is available from the data source yet. You cannot fix the finding by upgrading the package or rebuilding on a newer base image, because no fixed version exists to move to. You would need to wait for a fix, switch to a different base image or package, or record the finding as an accepted risk.
Exercise
You install a build tool in one RUN instruction and delete it in a later RUN instruction. Will trivy image report vulnerabilities in that tool? Why?
Show answer
No. Trivy inventories the packages present in the final filesystem of the image, and the deleted tool is no longer there. The files still exist in the earlier layer, but they are not part of the final filesystem that Trivy reads for its package inventory.