What is Trivy and how do you scan an image with it?
Short answer
Trivy is a free, open-source scanner from Aqua Security. It finds known vulnerabilities (CVEs) in container images and file systems, misconfigurations in Dockerfiles, Kubernetes YAML and Terraform, and secrets. The most common use is trivy image <image> in the pipeline, failing the build on HIGH or CRITICAL issues.
Explained in simple words
Your Docker image contains an operating system (Alpine, Debian, Ubuntu) plus libraries for your app. Each of these has versions, and some versions have publicly known security holes, listed as CVEs (for example CVE-2024-3094). Trivy compares everything inside your image against a vulnerability database and tells you what's vulnerable and which version fixes it.
Examples
# Scan an image
trivy image nginx:1.25
# Only show serious problems that have a fix available
trivy image --severity HIGH,CRITICAL --ignore-unfixed orders-api:1.0
# Fail the pipeline (exit code 1) if any HIGH/CRITICAL is found
trivy image --exit-code 1 --severity HIGH,CRITICAL orders-api:1.0
# Scan the project folder: dependency files like package-lock.json, pom.xml
trivy fs .
# Scan IaC and Kubernetes files for misconfigurations
trivy config ./terraform
trivy config ./k8s
Example output:
orders-api:1.0 (debian 12.5)
Total: 3 (HIGH: 2, CRITICAL: 1)
| Library | Vulnerability | Severity | Installed | Fixed | Title |
|----------|----------------|----------|-----------|---------|--------------------------------|
| openssl | CVE-2024-5535 | CRITICAL | 3.0.11-1 | 3.0.14-1| openssl: SSL_select_next_proto |
| libexpat | CVE-2024-45491 | HIGH | 2.5.0-1 | 2.5.0-2 | libexpat: integer overflow |
In a pipeline (GitHub Actions):
- name: Build image
run: docker build -t orders-api:${{ github.sha }} .
- name: Trivy scan
run: |
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy:latest \
image --exit-code 1 --severity HIGH,CRITICAL --ignore-unfixed orders-api:${{ github.sha }}
How do you fix the findings?
- Update the base image (for example
node:22-alpinewith the latest patch) — this fixes most OS vulnerabilities. - Update the library to the "Fixed" version shown in the report.
- Use smaller images (alpine, distroless) — fewer packages means fewer vulnerabilities.
- If a risk is accepted, list the CVE in
.trivyignorewith a comment and a review date.
What to say in the interview
"Trivy scans images, file systems and IaC. In our pipeline we build the image and run trivy image --exit-code 1 --severity HIGH,CRITICAL --ignore-unfixed before pushing to the registry, so vulnerable images never get deployed. We fix findings mostly by updating the base image or the affected library, and we use slim or distroless images to reduce the attack surface. Accepted risks go into .trivyignore with a justification."
Follow-up questions you may get
- Why
--ignore-unfixed? It hides vulnerabilities with no fix yet, so the build fails only on things the team can actually fix. - How do you scan images already running in Kubernetes? The Trivy Operator scans workloads continuously and stores reports as Kubernetes resources.