Most container images ship with known vulnerabilities. The base image, the language runtime, and the system packages inside it can each carry a CVE that was fixed upstream weeks ago — and you will never see it unless you scan. Docker image scanning with Trivy closes that gap: it is a fast, open-source scanner from Aqua Security that checks images, filesystems, and configuration files against public vulnerability databases, and it runs in a single binary with no server to maintain.

In this guide you will install Trivy, scan a real image, read the severity report, and wire scanning into a build so vulnerable images never reach production silently. If you are still shrinking your images, pair this with our Docker multi-stage builds guide, and if you run your own registry, see our private Docker registry guide.
Prerequisites – Docker Image Scanning with Trivy
- A Linux host with Docker installed and running, and sudo access.
- At least one local image to scan (we use
nginx:1.27in the examples). - Internet access on first run so Trivy can download its vulnerability database.
Installing Trivy – Docker Image Scanning with Trivy
On Ubuntu and Debian, Trivy provides an official APT repository. Add it, then install the package:
sudo apt-get install -y wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo gpg --dearmor -o /usr/share/keyrings/trivy.gpg
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install -y trivy
trivy --version
If you cannot add a repository, Trivy also ships as a standalone binary and as a container image (aquasec/trivy). The binary route is handy on CI runners where you do not want to modify APT sources.
Your First Docker Image Scanning Run
Scanning an image is one command. Trivy pulls the image if needed, inspects its layers, matches installed packages against its database, and prints a table grouped by target and severity:
docker pull nginx:1.27
trivy image nginx:1.27
A typical report lists each vulnerable package, the installed version, the fixed version (if one exists), and the CVE identifier. Focus on CRITICAL and HIGH first: these are the findings that usually block a release. Use --severity to filter the noise while you triage:
trivy image --severity HIGH,CRITICAL nginx:1.27
trivy image --severity CRITICAL --no-progress nginx:1.27
For a machine-readable report you can feed into other tools, output JSON or a table file:
trivy image -f json -o trivy-report.json nginx:1.27
trivy image -f table -o trivy-report.txt nginx:1.27
The JSON format is the right choice for CI pipelines and dashboards, because it preserves CVE IDs, fixed versions, and severity for every finding.
Docker Image Scanning for Operating System and Language Packages
By default, Trivy scans both OS packages (from the image’s package manager) and language packages (from lockfiles and package metadata for Python, Node.js, Go, Ruby, and others). That split matters: an image can have a clean Debian base and still ship a vulnerable Python library, or the reverse. Check what Trivy found in each category by reading the Class column in JSON output — os-pkgs versus lang-pkgs.
You can also scan a filesystem or a repository directly, without building an image first. This catches problems earlier, before the image is even built:
trivy fs --severity HIGH,CRITICAL ./myapp
trivy repo --severity HIGH,CRITICAL https://github.com/example/myapp
For configuration problems rather than CVEs — a container running as root, a missing read-only filesystem, a secret baked into a layer — add the config scanner:
trivy config --severity HIGH,CRITICAL ./myapp
Config scanning checks Dockerfiles, Kubernetes manifests, and Terraform files against built-in misconfiguration rules, which complements the CVE scan nicely.
Failing the Build on Serious Findings
A scan that nobody acts on is decoration. In a pipeline, make Trivy exit non-zero when it finds anything at or above your threshold, so the build fails loudly:
trivy image --exit-code 1 --severity HIGH,CRITICAL myapp:latest
With --exit-code 1, a clean image exits 0 and a vulnerable one exits 1, which is exactly what CI systems need. A minimal GitHub Actions step looks like this:
- name: Scan image with Trivy
uses: aquasecurity/trivy-action@master
with:
image-ref: myapp:latest
exit-code: '1'
severity: 'HIGH,CRITICAL'
Start with HIGH,CRITICAL only. Failing on every MEDIUM and LOW finding on day one usually blocks releases for issues your team cannot fix yet, which teaches everyone to ignore the scanner — the opposite of what you want.
Fixing What Docker Image Scanning Finds
Most findings have a simple fix: rebuild the image. Base-image CVEs disappear when you move to a newer tag, and application CVEs disappear when you update the dependency and rebuild. The workflow is:
docker pull nginx:1.27
# Update the base image tag or dependency version in your Dockerfile
docker build -t myapp:latest .
trivy image --severity HIGH,CRITICAL myapp:latest
Pin base images to a specific digest in production so rebuilds are reproducible, and schedule a regular rebuild — weekly is a good default — even when your code has not changed. An image that is never rebuilt quietly accumulates every CVE published since it was built.
When a finding has no fixed version yet, you have three honest options: accept the risk and document it, remove the vulnerable package if nothing needs it, or replace the component. What you should not do is suppress the finding silently; use .trivyignore with a comment and an expiry date so ignored findings are reviewed, not forgotten.
Troubleshooting Trivy
Database download fails. Trivy downloads its vulnerability database on first run and caches it under ~/.cache/trivy. If the download fails, check outbound HTTPS access and retry; on air-gapped hosts, download the database elsewhere and copy the cache directory, or use the --db-repository option with a local mirror.
Old results after rebuilding. Trivy caches image scan results. If a rebuild did not change the report, clear the cache with trivy image --clear-cache or trivy clean --all and scan again.
Too many false positives in application code. Make sure Trivy can see your lockfiles (package-lock.json, requirements.txt, go.mod). Without them it can only guess at language package versions, which inflates the report.
Scan is slow on first run. The first scan downloads the database and inspects every layer; later scans reuse the cache and are much faster. This is expected, not a bug.
Conclusion
Docker image scanning with Trivy turns vulnerability management from a quarterly audit into a one-command habit: install it, scan every image you build, fail the build on HIGH and CRITICAL findings, and rebuild regularly. The full option reference is in the Trivy documentation. An image you have scanned is not magically secure — but an image you have never scanned is a gamble, and Trivy makes the check cheap enough that there is no reason left to gamble.