Sub topic 9
Scanning an image with trivy image
This chapter assumes you have Trivy installed, as covered in the previous chapter. It also assumes you can pull and run Docker images, and that you know what a CVE and a severity rating are. The goal is to run trivy image, understand what it does internally, and read its output with confidence.
What trivy image does
trivy image scans a container image for known vulnerabilities. Given an image reference, it follows these steps:
- It locates the image. By default it looks in the local Docker daemon, then falls back to other sources such as containerd, Podman, and finally a remote registry.
- It unpacks the image layers and inspects the files in them.
- It identifies the operating system and the installed packages, such as
apt,apk, orrpmpackages. - It finds language dependencies, such as Python packages, Node.js modules, or Go binaries.
- It matches each package name and version against its vulnerability database.
- It prints a report.
The vulnerability database is downloaded the first time you scan, and Trivy refreshes it when it becomes stale. Trivy compares versions using the rules of each ecosystem. A Debian package version is compared differently from a Python package version.
Running your first scan
Scan a public image with a single command.
trivy image python:3.12-slimOn the first run, Trivy downloads its vulnerability database and prints progress messages. Later runs reuse the cached copy, so they start faster. The output has two parts: a short log of what Trivy detected, then one report section for each scan target.
Reading the report
Trivy groups findings by target. An image can have several targets: one for the operating system packages and one for each language ecosystem it finds. For python:3.12-slim, the first target is the Debian packages. Each target prints a summary line with the total count and a breakdown by severity. A table of findings follows.
The table has these columns:
| Column | Meaning |
|---|---|
| Library | The name of the affected package |
| Vulnerability | The identifier, usually a CVE ID |
| Severity | UNKNOWN, LOW, MEDIUM, HIGH, or CRITICAL |
| Status | Whether a fix exists, for example `fixed`, `affected`, or `will_not_fix` |
| Installed Version | The version found in the image |
| Fixed Version | The first version that contains the fix, if one exists |
| Title | A short description with a link for details |
The Status and Fixed Version columns tell you what you can do about a finding. A row with a fixed version means an update is available. A row with status affected and no fixed version means the vendor has acknowledged the problem but has not shipped a patch yet. A row with will_not_fix means the distribution has decided not to patch it, often because the impact is low.
Understanding the scan targets
A scan may report more than one target. Knowing the target types helps you decide where a fix belongs.
- OS packages appear under a heading such as
python:3.12-slim (debian 12.x). You usually fix these by updating the base image or by running a package upgrade in your Dockerfile. - Language packages appear under a heading that names a file or ecosystem, for example
Pythonorusr/local/bin/app (gobinary). You fix these by updating the dependency in your own project. - Targets with no findings may be omitted from the table output when nothing is found.
If a language package appears in a finding that you did not install yourself, it may come from the base image. The official Python image, for example, ships with pip and setuptools, and findings in those packages can show up in a scan of a minimal application.
Choosing the image source
By default, Trivy tries local sources before it contacts a registry. The --image-src option lets you choose explicitly.
trivy image --image-src docker python:3.12-slim
trivy image --image-src remote python:3.12-slimThe first command scans the copy in your local Docker daemon. The second scans the copy in the registry, even if you have a different local copy. This matters when your local tag is out of date. A tag such as 3.12-slim is a moving pointer, and the registry may hold a newer image than the one you pulled last month.
You can also scan an image that is saved as a tar archive, which is useful on machines that have no Docker daemon.
docker save -o myapp.tar myapp:1.0
trivy image --input myapp.tarScanning your own build
Scanning is most useful on images you build. Create a small Dockerfile and scan the result.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]Build and scan it:
docker build -t myapp:1.0 .
trivy image myapp:1.0The report now covers the base image's packages and the Python packages installed from requirements.txt. Trivy finds the Python packages by reading package metadata left in site-packages. It does not read your requirements.txt file in this mode. If a dependency is installed into the image, Trivy sees it.
Scanners and what they look for
Trivy can look for more than vulnerabilities. The --scanners option selects what runs.
| Scanner | What it finds |
|---|---|
| `vuln` | Known vulnerabilities in packages. This is the default for images. |
| `secret` | Hard-coded credentials and keys in files. |
| `misconfig` | Insecure settings in Dockerfiles and configuration files. |
| `license` | Package licenses. |
Recent Trivy versions run vuln and secret for images by default. To restrict a scan to vulnerabilities only, which also speeds it up, use this command:
trivy image --scanners vuln python:3.12-slimRestricting the scanners also makes the output easier to compare with Docker Scout, which reports package vulnerabilities.
Speed, caching, and offline use
The first scan is slow mostly because of the database download. Trivy stores the database and the results of previous layer analysis in a cache directory. On Linux the default is ~/.cache/trivy. Because layers are cached by content, rescanning an image that shares layers with one you scanned earlier is faster.
You can control this behavior:
--cache-dir PATHsets where Trivy stores its data. This is useful in CI, where you can restore the directory between runs.--skip-db-updatereuses the existing database without checking for a new one.--skip-version-checkstops Trivy from checking for a newer Trivy release.--quiethides progress and log messages, so only the report appears.
trivy image --quiet --scanners vuln --cache-dir /tmp/trivy-cache python:3.12-slimCommon errors and gotchas
The database download fails. Trivy downloads its database from a container registry. Rate limits or a blocked network can cause failures with messages about TOOMANYREQUESTS or a failed download. Retry later, use a mirror with --db-repository, or cache the database in CI.
The scan says the image was not found. Trivy could not find the image locally or in the registry. Check the spelling of the tag. For private registries, run docker login first, because Trivy reads your Docker credentials.
The report is empty but you expected findings. Trivy only reports what it recognizes. An image built FROM scratch that contains a statically linked binary has no package manager data, so Trivy cannot list OS packages. Go binaries with embedded module information are an exception, since Trivy reads that data.
Results change between two scans of the same image. The image did not change, but the vulnerability database did. New CVEs are published every day. A scan result is a snapshot of what is known at the time. This is why CI should rescan images on a schedule and not only at build time.
An old tag yields many findings. A moving tag such as latest may point to a different image than the one you scanned last week. Compare image digests when you need to know whether two scans covered the same content.
Reading the exit code
By default, trivy image exits with code 0 even when it finds vulnerabilities. A script cannot tell a clean image from a vulnerable one unless you ask. The --exit-code option sets a non-zero code when findings match your filters. A later chapter builds on this to gate CI builds.
trivy image --severity CRITICAL --exit-code 1 python:3.12-slim
echo "Exit code: $?"If the scan finds a critical vulnerability, the exit code is 1. Otherwise it is 0.
Exercise
You scan the same image tag twice, a month apart, without rebuilding it locally. The second scan reports several new CVEs. Name two possible explanations and one command option that helps you tell them apart.
Show answer
Either the vulnerability database gained new entries for packages that were already in the image, or the tag was repulled and now points to a different image. Using --image-src remote scans the registry copy, and comparing the image digest of both scans shows whether the content changed. If the digest is the same, the database explains the new findings.
Exercise
A scan of your application image shows a finding in a Python package, and the Fixed Version column is empty. What does this tell you, and what should you check next?
Show answer
No patched version has been published, or the vendor has not released one yet. Check the Status column for affected or will_not_fix, read the advisory link to see whether your code reaches the vulnerable function, and decide whether to wait, replace the package, or accept the risk with a documented ignore entry.