Tutorials · DevSecOps & Cloud
How to Scan Container Images, Code and IaC for Vulnerabilities With Trivy
Trivy is a free scanner for container images, file systems, Git repositories and infrastructure-as-code. Find vulnerable packages and misconfigurations before they reach production.
Before you start
- Docker or a Linux/macOS machine
- Trivy (apt/brew package or the aquasec/trivy container image)
Step by step
Install Trivy
Use your package manager, or run it as a container without installing anything.
brew install trivy # or docker run --rm aquasec/trivy:latest --version
Scan an image
Trivy lists CVEs per package with severity and the fixed version.
trivy image nginx:1.27Show only what matters
Filter to high and critical issues that already have a fix.
trivy image --severity HIGH,CRITICAL --ignore-unfixed myapp:1.4.2Scan your project folder
Find vulnerable dependencies (npm, pip, Go, Java and more) and leaked secrets in the source tree.
trivy fs --scanners vuln,secret,misconfig .Scan infrastructure-as-code
Trivy checks Terraform, Kubernetes manifests, Dockerfiles and Helm charts for risky settings such as public buckets or containers running as root.
trivy config ./infraFail the build on critical issues
In CI, a non-zero exit code stops the pipeline when critical vulnerabilities are found.
trivy image --exit-code 1 --severity CRITICAL --ignore-unfixed myapp:${GIT_SHA}Fix and rebuild
Update the base image (smaller images such as distroless or alpine have fewer packages), upgrade dependencies, rebuild and re-scan.
Commands are generic, industry-standard examples. Exact syntax and menu names vary by model and firmware; check your device's manual on the Documentation page.
Pro tips
- Rebuild images regularly even when your code does not change: base images receive security fixes.
- Use a .trivyignore file for accepted risks, with a comment and review date for each entry.
FAQ
Frequently asked questions
How is Trivy different from an SBOM tool?
Trivy finds vulnerabilities directly. An SBOM tool lists components; you can scan an SBOM for vulnerabilities later as new CVEs appear.
Keep learning
Related guides
Secure Docker containers
Read more →Generate an SBOM
Read more →Add security checks to CI
Read more →How to Generate an SBOM for Your Software and Scan It With Grype
A Software Bill of Materials lists every component in your software. Customers and regulations (such as the EU Cyber Resilience Act) increasingly ask for it. Generate SBOMs with Syft and check them for vulnerabilities with Grype.
Read more →How to Add Security Checks to a CI/CD Pipeline (Secrets, SAST, Dependencies, Images)
Shift security left: catch leaked secrets, insecure code, vulnerable dependencies and risky infrastructure settings automatically on every commit, before anything is deployed.
Read more →How to Keep Passwords and API Keys Out of Code With Vault or OpenBao
Hard-coded passwords end up in Git, chat and backups. Store secrets centrally in HashiCorp Vault or its open-source fork OpenBao, control who can read them and let applications fetch them at runtime.
Read more →Stuck on a step?
Ask Navijin technical support or an authorised partner for help with your configuration.