Sub topic 8
Getting started with Trivy
This chapter assumes you can pull and run Docker images, use a terminal, and read a Dockerfile. It also assumes you know what a software dependency is. Earlier chapters covered how image contents become findings and how CVEs and severities work. Here you install Trivy and run a first scan.
What Trivy is
Trivy is an open-source scanner from Aqua Security. It reads an image, builds a list of the operating system packages and language dependencies inside it, and matches that list against vulnerability databases. It runs as a single command-line program and needs no account.
Trivy can also scan filesystems, Git repositories, and configuration files. This tutorial uses only trivy image.
How a scan works
Trivy does not run your container. It unpacks the image layers, finds package metadata, and compares what it finds with a local copy of its vulnerability database. The database is downloaded on first use and refreshed when it gets old.
Because matching happens against a local database, results depend on when the database was last updated. The same image can show new findings tomorrow without any change to the image.
Installing Trivy
Pick the method that fits your machine.
| Platform | Command |
|---|---|
| macOS or Linux with Homebrew | `brew install trivy` |
| Any system with Docker | `docker run aquasec/trivy:latest image <name>` |
| Linux packages and binaries | See the releases page of the Trivy project |
Check the install with a version command.
trivy --versionYour first scan
Scan a public image by name. Trivy pulls it from the registry if it is not available locally.
trivy image python:3.12-slimThe first run prints progress lines while it downloads the database. It then prints a summary and one table per target. A target is a group of packages Trivy scanned together, such as the operating system packages or a Python environment.
Each table row has these columns:
- Library: the affected package.
- Vulnerability: the CVE or advisory ID.
- Severity: LOW, MEDIUM, HIGH, or CRITICAL.
- Status: for example
fixed,affected, orwill_not_fix. - Installed Version and Fixed Version: what you have, and the release that contains the fix, if one exists.
- Title: a short description with a link for details.
An empty Fixed Version means the vendor has not published a fix yet. That matters when you decide what to act on, which later chapters cover.
Common errors
- Database download fails. Trivy needs network access on first run. Behind a proxy, set
HTTPS_PROXY. In an offline environment, you must supply the database another way. - Image not found. If you scan a local image that exists only in your Docker daemon, Trivy needs access to the daemon. With the container form, mount the socket as shown above.
- Unexpected "no vulnerabilities". A clean report means no known matches in the database. It does not prove the image is safe. Some images, such as those with stripped package metadata, give Trivy little to inspect.
- Results change between runs. The database updated. Record the scan date with your results.
Exercise
You scan the same image on Monday and Friday without rebuilding it. Friday's report has two extra findings. What is the most likely cause, and why can a clean report still be misleading?
Show answer
Trivy's vulnerability database was updated between the scans, so newly published CVEs now match packages already in the image. A clean report only means no known matches exist in the database. It can miss packages Trivy cannot detect, and it cannot report vulnerabilities not yet published.