Chapter 10 · Lesson 7 of 10
Server security

Scan Docker images with Trivy (severity, exit code, ignore)

Trivy is a scanner that compares the packages in an image, folder or config against vulnerability databases and reports known issues by severity so you can fix or block them.

By the end you will

Scan an image with Trivy, filter findings by severity, fail a pipeline on serious ones and record accepted exceptions in .trivyignore.

Time
20 min
Level
Intermediate
Check
4 questions
You need
  • A server or laptop with Docker
  • An image you built or pulled

A container image is a bundle of an operating system’s packages plus your app and its libraries. Any of those can have a known vulnerability. Trivy is a free scanner that reads the image, matches what it finds against public vulnerability data and prints a list with severity levels. Run it before you deploy and again on a schedule, because new vulnerabilities are published for software you already shipped.

How it works#

Trivy keeps a local database of vulnerabilities, downloaded on first use and refreshed as needed. When you scan an image, it unpacks the layers, identifies the OS packages (apt, apk and so on) and language dependency files (such as package-lock.json or go.sum), and looks each one up. Findings carry an ID such as a CVE number, a severity (UNKNOWN, LOW, MEDIUM, HIGH, CRITICAL), the installed version and, when one exists, the fixed version.

Three things shape how to read the result. A finding in an OS package you never call is lower real risk than one in your web framework. A finding with no fixed version cannot be patched yet. And a clean scan only means “nothing known”, not “safe”. It does not replace running containers as non-root or keeping the host patched.

Step 1: Get Trivy#

Trivy ships as a package, a single binary and a container image. Follow the install instructions in the official documentation for your platform, linked in the sources of this lesson, and check it works:

bash
trivy --version

If you do not want to install it, run it from its container image instead. This mounts the Docker socket so Trivy can see local images, so only do it on a machine you trust:

bash
docker run --rm \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v trivy-cache:/root/.cache/ \
  aquasec/trivy:latest image myapp:1.0

The named volume keeps the vulnerability database between runs so it is not downloaded every time. For repeatable pipelines, pin a specific tag rather than latest.

Step 2: Scan an image#

bash
trivy image myapp:1.0

The first run downloads the database, so it takes longer. The report groups findings by target, such as the OS layer and each language lockfile, with a table per target. Scan images you pull too, since a base image can carry old packages: trivy image nginx:stable.

Step 3: Filter by severity and fixability#

Long reports hide the important lines. Narrow them:

bash
trivy image --severity HIGH,CRITICAL myapp:1.0
trivy image --severity HIGH,CRITICAL --ignore-unfixed myapp:1.0

--ignore-unfixed removes findings that have no fix yet, so what remains is what you can act on today by updating a base image or a dependency. It is a reasonable default for a gate, but do not forget the hidden ones. Look at the full report now and then.

Step 4: Fail a pipeline on serious findings#

For CI, make the scan return a failing status:

bash
trivy image --exit-code 1 --severity HIGH,CRITICAL --ignore-unfixed myapp:1.0
echo $?

Without --exit-code 1, Trivy exits with 0 even when it finds issues, so a pipeline would pass. With it, the step fails when a matching finding exists. Start with CRITICAL only on an existing project, or the first run blocks everything, then tighten over time. The usual fix is to rebuild on a newer base image: update the FROM line and rebuild. Multi-stage builds that end in a small runtime image reduce the findings too, see multi-stage Docker builds.

Step 5: Record accepted exceptions#

Sometimes a finding does not apply, for example a vulnerable function your app never calls. List the ID in a .trivyignore file in the project folder:

output
# CVE-2099-0001: only affects the Windows build, reviewed 2026-11-27, recheck in a month
CVE-2099-0001

The ID above is a made up example. Use the real ID from your report. Put a reason and a review date in the comment so the exception does not become permanent by neglect. Trivy reads this file from the current directory by default.

Step 6: Scan folders and configuration#

Trivy also checks things before they become images:

bash
trivy fs .
trivy config .

trivy fs scans a project folder’s dependency files for vulnerable libraries. trivy config checks Dockerfiles, Compose files and other infrastructure files for risky settings, for instance a Dockerfile with no USER line. Fix those with the steps in run containers as non-root. Trivy can also look for secrets in files, which complements managing secrets.

Check it worked#

bash
trivy image --exit-code 1 --severity CRITICAL myapp:1.0
echo "exit: $?"

An exit value of 0 means no critical findings matched. A value of 1 means at least one did, and the table above lists which package and which version fixes it. After you rebuild on an updated base, scan again and compare. Also add the scan to the pipeline you already have, so it runs on each build.

Edge cases and gotchas#

  • Database download fails. Scans in restricted networks cannot fetch the database. Mirror it or allow the registry it uses, and check the docs for the offline option.
  • Rate limits. Pulling the database repeatedly can be throttled. Cache it between runs with a volume or CI cache.
  • False sense of safety. Zero findings mean nothing known today. Scan on a schedule, for example weekly, against the images you actually run.
  • Base image noise. Large general-purpose images carry many packages you never use. A slimmer base has fewer findings.
  • Tags move. myapp:latest can change between scans. Scan the exact tag or digest you deploy.
  • Docker socket access. Mounting the socket into the Trivy container gives that container control over Docker. Use it deliberately.

Troubleshooting#

SymptomLikely causeFix
FATAL ... failed to download vulnerability DBNo network or registry blockedAllow access or use a cached DB
unable to find the specified image "myapp:1.0"Image not local and not in a registryBuild or pull the image first
Pipeline passes despite findings--exit-code 1 missingAdd it with a severity filter
Report is hugeNo severity filter--severity HIGH,CRITICAL
Same CVE keeps appearing after rebuildBase image not updated or layer cachedRebuild with --pull and --no-cache
permission denied while trying to connect to the Docker daemon socketUser not in the docker groupUse sudo or add the user to the docker group
Ignored CVE still shown.trivyignore is in another directoryRun from the folder holding it or pass its path

Quick reference#

bash
trivy image myapp:1.0
trivy image --severity HIGH,CRITICAL myapp:1.0
trivy image --exit-code 1 --severity HIGH,CRITICAL --ignore-unfixed myapp:1.0
trivy fs .
trivy config .
cat .trivyignore

Next#

Continue with TLS and HTTP security headers checklist.

Try it: Scan an image with Trivy

Scan alpine, narrow the results, then try the CI style exit code.

practice, nothing runs on your machine

Want a real machine? Free browser labs: Killercoda

Check what you learned4 questions
1Which command scans a local image called myapp:1.0?
2What do you add to fail a CI step only for serious findings?
3You accept a risk for one CVE. Where do you record it for Trivy?
4Why rescan an image that passed last month?

Frequently asked questions

What does Trivy scan?

It scans container images, filesystems, git repositories and configuration files such as Dockerfiles and Compose or Kubernetes manifests. For images it checks OS packages and language dependencies against known vulnerability databases.

How do I make Trivy fail a CI job?

Add --exit-code 1 together with a --severity filter, for example HIGH,CRITICAL. Trivy then exits with code 1 when it finds a matching vulnerability, which fails the pipeline step.

What does --ignore-unfixed do?

It hides vulnerabilities that have no fixed version yet. That removes findings you cannot act on, though you still should watch them and rebuild once a fix appears.

Can I run Trivy without installing it?

Yes. Run the Trivy container image and mount the Docker socket so it can read local images. Mounting the socket is powerful, so use it only on machines you trust.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios