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.
- Updated Nov 27, 2026
- Markdown
Scan an image with Trivy, filter findings by severity, fail a pipeline on serious ones and record accepted exceptions in .trivyignore.
- 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:
trivy --versionIf 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:
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
-v trivy-cache:/root/.cache/ \
aquasec/trivy:latest image myapp:1.0The 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#
trivy image myapp:1.0The 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:
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:
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:
# CVE-2099-0001: only affects the Windows build, reviewed 2026-11-27, recheck in a month
CVE-2099-0001The 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:
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#
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:latestcan 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#
| Symptom | Likely cause | Fix |
|---|---|---|
FATAL ... failed to download vulnerability DB | No network or registry blocked | Allow access or use a cached DB |
unable to find the specified image "myapp:1.0" | Image not local and not in a registry | Build or pull the image first |
| Pipeline passes despite findings | --exit-code 1 missing | Add it with a severity filter |
| Report is huge | No severity filter | --severity HIGH,CRITICAL |
| Same CVE keeps appearing after rebuild | Base image not updated or layer cached | Rebuild with --pull and --no-cache |
permission denied while trying to connect to the Docker daemon socket | User not in the docker group | Use sudo or add the user to the docker group |
| Ignored CVE still shown | .trivyignore is in another directory | Run from the folder holding it or pass its path |
Quick reference#
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 .trivyignoreRelated lessons#
- Write your first Dockerfile: the file Trivy checks for risky settings.
- Multi-stage Docker builds: ship fewer packages to scan.
- Update a container to a new image: roll out the fixed image.
- Run containers as non-root: reduce what a vulnerability can do.
- What is CI/CD: where the scan gate belongs.
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.
Want a real machine? Free browser labs: Killercoda
Check what you learned
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
Keep going: guides and tutorials
- Manage Secrets Self-Hosted: Env Vars and Master Key Rotation Set env vars and encrypted secrets on a self-hosted server, apply changes safely, and rotate the master key with no downtime without locking yourself out.
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.