Sub topic 8
Getting started with Trivy
Trivy is an open-source vulnerability scanner from Aqua Security. It reads a container image, builds a list of the packages inside it, and matches that list against vulnerability databases. This chapter covers installing Trivy, running a first scan, and understanding what Trivy downloads and caches. The next chapter goes deeper into the trivy image command.
Prerequisites
This chapter assumes you:
- Can pull, build and run images with Docker.
- Are comfortable in a terminal.
- Know that an image is made of layers and that the layers contain installed packages.
- Know that a CVE is a public identifier for a known vulnerability, as covered in the earlier chapters on image contents and CVEs.
What Trivy is and what it scans
Trivy is a single binary with no daemon and no required server. You point it at a target and it prints a report. The target can be:
- A container image (
trivy image). - A local directory or a Git repository (
trivy fs,trivy repo). - A configuration file such as a Dockerfile or Kubernetes manifest (
trivy config). - A running Kubernetes cluster (
trivy k8s).
This tutorial uses trivy image. Inside an image, Trivy looks for two kinds of packages:
- Operating system packages. Trivy detects the distribution from files such as
/etc/os-releaseand reads the package database (for exampledpkgon Debian and Ubuntu,apkon Alpine,rpmon Red Hat family systems). - Language dependencies. Trivy finds lock files and package manifests, such as
requirements.txt,package-lock.json,go.modorCargo.lock, and compiled Go binaries that embed module information.
Trivy can also scan for other risks, called scanners. The default scanners for an image are vulnerabilities and secrets. Misconfiguration scanning applies to configuration files rather than image contents by default.
How a scan works
A scan has four steps:
- Trivy loads the image, either from a registry or from a local Docker daemon.
- It unpacks the layers and builds an inventory of OS and language packages. This inventory is the same idea as an SBOM.
- It compares each package name and version against its local vulnerability database.
- It prints the matches, with severity and fixed-version information.
Step 3 uses a local copy of the database. That copy is why the first scan is slower than later ones, as the next sections explain.
Installing Trivy
Pick the method that fits your machine. All of them give you the same trivy command.
macOS and Linux with a package manager
On macOS with Homebrew:
brew install trivyOn Debian or Ubuntu, Aqua publishes an APT repository. Check the official installation page for the current repository commands, because the signing key and URLs can change.
Install script
The project provides a shell script that downloads a release binary:
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sudo sh -s -- -b /usr/local/binPiping a script into a shell runs code you have not read. For a shared or production machine, download the script first, read it, and pin a version.
Running Trivy as a container
You do not need to install anything if you run Trivy itself in a container. Because Trivy must read your images, you either mount the Docker socket or have Trivy pull from a registry:
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
-v trivy-cache:/root/.cache/ \
aquasec/trivy:latest image python:3.12-slimThe trivy-cache named volume keeps the database between runs. Without it, every run downloads the database again.
Check the installation
trivy --versionThe output shows the Trivy version and, once a scan has run, the version of the vulnerability database.
Your first scan
Run Trivy against a public image:
trivy image python:3.12-slimThe first run prints log lines before the report. They tell you that Trivy is downloading the vulnerability database and, if it needs one, the Java database. They also tell you which scanners are enabled and which operating system it detected. Then you see a report like this one (abbreviated, with placeholder values):
python:3.12-slim (debian 12.x)
Total: 40 (UNKNOWN: 0, LOW: 30, MEDIUM: 8, HIGH: 2, CRITICAL: 0)
┌───────────┬───────────────┬──────────┬──────────┬───────────────────┬───────────────┬───────────────┐
│ Library │ Vulnerability │ Severity │ Status │ Installed Version │ Fixed Version │ Title │
├───────────┼───────────────┼──────────┼──────────┼───────────────────┼───────────────┼───────────────┤
│ libexample│ CVE-XXXX-XXXX │ HIGH │ fixed │ 1.2.3-1 │ 1.2.3-2 │ short summary │
└───────────┴───────────────┴──────────┴──────────┴───────────────────┴───────────────┴───────────────┘The exact numbers change as the image and the database change. Do not expect your output to match this one.
Reading the report
The header line names the target and the detected operating system. The Total line counts findings by severity. Each table row has these columns:
| Column | Meaning |
|---|---|
| Library | The package that Trivy matched. |
| Vulnerability | The CVE or other advisory identifier. |
| Severity | UNKNOWN, LOW, MEDIUM, HIGH or CRITICAL. |
| Status | Whether a fix exists, for example `fixed`, `affected`, `will_not_fix` or `fix_deferred`. |
| Installed Version | The version found in the image. |
| Fixed Version | The first version that contains the fix, if one exists. |
| Title | A short description of the issue. |
Status and Fixed Version are the two columns that decide what you can do next. A row with a Fixed Version means that upgrading the package, or rebuilding on a newer base image that includes it, removes the finding. A row with no Fixed Version means that the distribution has not published a fix yet.
When an image contains language dependencies, Trivy prints a separate table per target. For example, you may see one table for the OS packages and another for a requirements.txt or a Python site-packages directory. Look at all of the tables before you decide an image is clean.
The vulnerability database and the cache
Trivy does not call a web service for each package. It downloads a database and matches locally. This has several consequences.
- The database is published as an OCI artifact. By default Trivy downloads it from a public registry (GitHub Container Registry,
ghcr.io). If your network blocks that registry, the first scan fails with a download error. Fix this by allowing the registry, by using a mirror, or by pre-loading the database. - The database is cached. On Linux the cache is under
~/.cache/trivy. You can change it with--cache-diror theTRIVY_CACHE_DIRenvironment variable. - The database is refreshed on a schedule. Trivy checks whether the cached copy is older than its update window, and downloads a new one if so. A scan today can therefore report a CVE that a scan of the same image last week did not, even though the image did not change.
- Scans can run offline. After the database is cached,
--skip-db-updateskips the update check. This is useful in air-gapped environments, but the results get stale unless you refresh the database another way.
trivy image --skip-db-update python:3.12-slimIf you pass --skip-db-update with an empty cache, the scan fails because there is no database to read.
Why this matters for CI
A CI runner that starts clean on every job downloads the database every time. That adds time and can hit registry rate limits. Cache the Trivy cache directory between runs. If many pipelines scan at once, consider hosting a mirror of the database in your own registry and pointing Trivy at it with --db-repository.
Choosing where Trivy finds the image
By default, Trivy tries several image sources in order: the local Docker daemon, containerd, Podman, and then a remote registry. This means a locally built image with the same name as a remote one is scanned from your machine.
You can pick a source explicitly with --image-src:
trivy image --image-src docker myapp:devYou can also scan an image saved as a tarball, which is useful when no daemon is available:
docker save myapp:dev -o myapp.tar
trivy image --input myapp.tarFor a private registry, Trivy uses the credentials stored by docker login. You can also supply them through environment variables such as TRIVY_USERNAME and TRIVY_PASSWORD.
Common problems
- The first scan seems to hang. It is downloading the database. Wait, or run with
--debugto see progress. - "Unable to find the specified image". The name is wrong, the image is private and you are not logged in, or the daemon is not reachable. Check with
docker image lsordocker pull. - Download errors or rate limiting. The database registry is blocked or throttled. Use a mirror through
--db-repository, or cache the database. - No OS detected. Minimal images, such as
scratch, have no OS package database. Trivy then reports only language dependencies, if any. An empty report does not prove that the image is safe, only that Trivy found nothing it could inventory. - Different results on different days. The database changed. Record the database version, shown in the
trivy --versionoutput, when you compare scans. - A secret scan slows large images. If you only need vulnerabilities, use
--scanners vuln.
trivy image --scanners vuln python:3.12-slimSummary of useful options
| Option | Purpose |
|---|---|
| `--scanners vuln` | Run only the vulnerability scanner. |
| `--skip-db-update` | Use the cached database without checking for updates. |
| `--cache-dir PATH` | Store the database and cache in a custom location. |
| `--image-src docker` | Choose the image source. |
| `--input FILE.tar` | Scan an image tarball. |
| `--debug` | Print detailed logs for troubleshooting. |
Exercise
You run trivy image myapp:1.0 in a fresh CI job and the scan takes much longer than it does on your laptop. A week later, the same unchanged image shows a new HIGH finding. Give one reason for the slow scan and one reason for the new finding, and name one way to fix the slow scan.
Show answer
The CI job starts with an empty cache, so Trivy downloads the vulnerability database before it scans. The new finding appears because Trivy refreshes its database on a schedule, and a newly published advisory now matches a package that was already in the image. To speed up the scan, cache the Trivy cache directory between CI runs (set with --cache-dir or TRIVY_CACHE_DIR), or host a database mirror and use --db-repository.
Exercise
A teammate scans an image built FROM scratch and reports "Trivy found nothing, so the image is safe." Explain why the conclusion is not justified.
Show answer
A scratch image has no operating system package database, so Trivy has no OS packages to inventory. If the image contains no lock files or recognizable binaries either, Trivy has nothing to match against the database. An empty report means nothing was inventoried or matched, not that the software has no vulnerabilities.