Scanning with Trivy
Trivy (Aqua Security, Apache-2.0) is a scanner with a wider remit than Grype: besides vulnerabilities in packages it finds secrets, misconfigurations (Dockerfiles, Kubernetes manifests, Terraform) and licenses, and it can generate SBOMs itself.
trivy image IMAGE | --input image.tar # container images
trivy fs DIR # a source tree or file system
trivy sbom FILE # an SBOM (CycloneDX, SPDX)
trivy repo URL|DIR # a Git repository
trivy k8s … # a running clusterThe course image sets Trivy to offline mode with its frozen database (TRIVY_SKIP_DB_UPDATE, TRIVY_OFFLINE_SCAN, no progress bars), so the commands below need no extra flags. In CI you'd normally let Trivy download its database, ideally from a mirror you control.
An image
Trivy reads image tarballs with --input, images in registries by name, and images in a local Docker or Podman daemon. The JSON report starts with metadata about the target:
trivy image --input /opt/samples/images/alpine-3.18.0.tar -q -f json \
| jq '{os: .Metadata.OS, results: [.Results[] | {Target, Class, Type, vulnerabilities: (.Vulnerabilities | length)}]}'{
"os": {
"Family": "alpine",
"Name": "3.18.0",
"EOSL": true
},
"results": [
{
"Target": "/opt/samples/images/alpine-3.18.0.tar (alpine 3.18.0)",
"Class": "os-pkgs",
"Type": "alpine",
"vulnerabilities": 49
}
]
}
"EOSL": true is worth a CI warning of its own: Alpine 3.18 has reached end of service life, so new vulnerabilities in it will not get fixed packages at all. Upgrading the base image is the only real fix; the vulnerability count is a symptom.
Severity filters
--severity limits the report to the listed levels (Trivy's levels are UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL); --ignore-unfixed drops findings without a fixed version.
crane push /opt/samples/images/alpine-3.18.0.tar localhost:5000/demo/alpine:3.18.0 > /dev/null 2>&1
img=localhost:5000/demo/alpine:3.18.0
for opts in "" "--ignore-unfixed" "--severity CRITICAL" "--severity HIGH,CRITICAL --ignore-unfixed"; do
printf '%-55s %s\n' "trivy image $opts" "$(trivy image -q $opts -f json $img | jq '[.Results[].Vulnerabilities[]?] | length')"
done
trivy image -q --severity CRITICAL -f json $img | jq -r '.Results[].Vulnerabilities[] | "\(.PkgName) \(.InstalledVersion) \(.VulnerabilityID) fixed:\(.FixedVersion)"'trivy image 49
trivy image --ignore-unfixed 49
trivy image --severity CRITICAL 3
trivy image --severity HIGH,CRITICAL --ignore-unfixed 9
busybox 1.36.0-r9 CVE-2022-48174 fixed:1.36.1-r1
busybox-binsh 1.36.0-r9 CVE-2022-48174 fixed:1.36.1-r1
ssl_client 1.36.0-r9 CVE-2022-48174 fixed:1.36.1-r1
--ignore-unfixed changed nothing here: Trivy's Alpine data comes from Alpine's security database, which lists vulnerabilities with the package version that fixed them, so every Alpine finding has a fix. Grype reported more findings for the same image, some without fixes, because it also matches packages against upstream (NVD) records. Same image, same day, different answers: keep that in mind for the disagreement lesson.
A source tree, and an SBOM
trivy fs reads lockfiles in a directory; trivy sbom reads an SBOM produced by any tool, here Syft's:
cp -r /opt/samples/node-app . && cd node-app
echo "fs: $(trivy fs . -q -f json | jq -r '[.Results[].Vulnerabilities[].VulnerabilityID] | sort | join(" ")')"
syft scan . -q -o cyclonedx-json=sbom.cdx.json
echo "sbom: $(trivy sbom sbom.cdx.json -q -f json | jq -r '[.Results[].Vulnerabilities[].VulnerabilityID] | sort | join(" ")')"fs: CVE-2020-28500 CVE-2020-8203 CVE-2021-23337 CVE-2021-44906 CVE-2025-13465 CVE-2026-2950 CVE-2026-4800 NSWG-ECO-516
sbom: CVE-2020-28500 CVE-2020-8203 CVE-2021-23337 CVE-2021-44906 CVE-2025-13465 CVE-2026-2950 CVE-2026-4800 NSWG-ECO-516
Same findings either way. Trivy reports CVE ids where an advisory has one (Grype reported GHSA ids for the same packages), and one finding, NSWG-ECO-516, is an old Node Security Working Group advisory that Grype's database doesn't carry. The Why Scanners Disagree lesson compares the two systematically.
Trivy as an SBOM generator
trivy image --input /opt/samples/images/alpine-3.18.0.tar -q -f cyclonedx | jq '{bomFormat, specVersion, tool: .metadata.tools.components[0].name, components: (.components | length)}'
trivy fs /opt/samples/node-app -q -f spdx-json | jq '{spdxVersion, packages: (.packages | length)}'{
"bomFormat": "CycloneDX",
"specVersion": "1.7",
"tool": "trivy",
"components": 16
}
{
"spdxVersion": "SPDX-2.3",
"packages": 4
}
Trivy and Syft catalog differently (Trivy counts the OS itself as a component, for example), so pick one generator per pipeline and stick to it; mixing them makes SBOM diffs noisy.
Secrets and misconfigurations
Supply-chain incidents often start with a leaked token (Codecov's attacker used one; so do most registry takeovers). Trivy's secret scanner looks for known token formats:
mkdir app && cd app
cat > deploy.env <<'ENV'
AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
GITHUB_TOKEN=ghp_abcdefghijklmnopqrstuvwxyz0123456789
ENV
printf 'FROM alpine:3.18\nRUN apk add curl\n' > Dockerfile
trivy fs . -q --scanners secret -f json | jq -r '.Results[] | .Secrets[]? | "\(.Severity) \(.RuleID) line \(.StartLine): \(.Title)"'
trivy fs . -q --scanners misconfig -f json | jq -r '.Results[] | .Misconfigurations[]? | "\(.Severity) \(.ID) \(.Title)"'CRITICAL github-pat line 2: GitHub Personal Access Token
HIGH DS-0002 Image user should not be 'root'
HIGH DS-0025 'apk add' is missing '--no-cache'
LOW DS-0026 No HEALTHCHECK defined
The AWS key wasn't reported: AKIAIOSFODNN7EXAMPLE is AWS's documented example key and Trivy allow-lists it. The made-up GitHub token matched the ghp_ pattern. The Dockerfile checks flag the root user, a missing --no-cache and no HEALTHCHECK. None of them complains that alpine:3.18 is a moving tag rather than a digest; Tags vs Digests explains why you should care anyway.
Test yourself
Trivy reports EOSL: true for an image's OS. What does that mean for you?
- The scan failed
- The distribution release no longer gets security fixes, so upgrade the base image
- The image is signed
- The DB is outdated
Which flag hides vulnerabilities that have no fixed version in Trivy?
- --only-fixed
- --ignore-unfixed
- --severity FIXED
- --skip-unfixed
Why use one SBOM generator per pipeline rather than mixing Syft and Trivy?
- They can't read each other's SBOMs
- They catalog differently, so mixing makes SBOM comparisons noisy
- Trivy can't write CycloneDX
- Syft is faster