Trivy by Aqua Security is a comprehensive, blazing-fast security scanner for container images, file systems, Git repositories, virtual machine images, Kubernetes clusters, and AWS environments. It detects CVE vulnerabilities, infrastructure misconfigurations, exposed secrets, and license non-compliance.
sudo apt-get install -y wget apt-transport-https gnupg
sudo mkdir -p /etc/apt/keyrings
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | sudo tee /etc/apt/keyrings/trivy.gpg > /dev/null
echo "deb [signed-by=/etc/apt/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb generic main" | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install -y trivy# macOS via Homebrew
brew install trivy
# Run directly via Docker (no local installation)
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v $HOME/Library/Caches:/root/.cache/ aquasec/trivy:latest image nginx:latest# 1. Scan a Container Image
trivy image nginx:alpine
# 2. Scan a Local Filesystem / Source Code
trivy fs /path/to/project
# 3. Scan a Remote Git Repository
trivy repo https://github.com/org/repo
# 4. Scan Infrastructure as Code (Terraform, CloudFormation, Dockerfile, K8s manifests)
trivy config ./infra/
# 5. Scan a Live Kubernetes Cluster
trivy k8s --report summary clusterTip
Use --scanners to select target engines: vuln (CVEs), misconfig (IaC compliance), secret (leaked tokens), and license.
# Scan for vulnerabilities, misconfigurations, and secrets in one run
trivy fs --scanners vuln,misconfig,secret .
# Filter by severity (HIGH and CRITICAL only)
trivy image --severity HIGH,CRITICAL python:3.11-slim
# Filter out unpatchable vulnerabilities (show only actionable fixes)
trivy image --ignore-unfixed nginx:latest
# Fail the command with exit code 1 (ideal for CI/CD pipelines)
trivy image --exit-code 1 --severity CRITICAL nginx:latestTrivy generates and scans industry-standard SBOM formats (SPDX and CycloneDX):
# Generate a CycloneDX SBOM in JSON
trivy image --format cyclonedx --output sbom-cyclonedx.json node:18-alpine
# Generate an SPDX SBOM in JSON
trivy image --format spdx-json --output sbom-spdx.json node:18-alpine
# Scan an existing SBOM file for newly disclosed vulnerabilities
trivy sbom sbom-cyclonedx.jsonCreate a .trivyignore file in the root of your project to suppress false positives or accepted risks:
# Ignore specific CVEs with reason and expiry date
# Accepted risk until upgrade next sprint
CVE-2023-12345
CVE-2024-54321 exp:2026-10-01
# Ignore specific paths from secret scanning
path:tests/fixtures/mock_keys.pem
Execute scan with ignore file:
trivy image --ignorefile .trivyignore my-app:latestname: Security Scan
on: [push, pull_request]
jobs:
trivy-scan:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Build Container Image
run: docker build -t myapp:${{ github.sha }} .
- name: Run Trivy Vulnerability Scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: 'myapp:${{ github.sha }}'
format: 'table'
exit-code: '1'
ignore-unfixed: true
vuln-type: 'os,library'
severity: 'CRITICAL,HIGH'trivy_scan:
stage: test
image:
name: aquasec/trivy:latest
entrypoint: [""]
script:
- trivy fs --exit-code 1 --severity CRITICAL --scanners vuln,secret .# Output as JSON
trivy image -f json -o scan-results.json nginx:latest
# Output as SARIF (for GitHub Security Tab integration)
trivy image -f sarif -o results.sarif nginx:latest
# Generate an HTML report using a template
trivy image --format template --template "@contrib/html.tpl" -o report.html nginx:latestTrivy automatically downloads and updates its vulnerability database. Manage disk usage with:
# Clear all cached vulnerability databases and image layers
trivy clean --all
# Clear only the vulnerability database
trivy clean --scan-cache