← Writing

Using Trivy to check Dockerfiles, scan container images, and generate an SBOM

On this page

Scan source and configuration in the pull request, then scan the exact image you plan to deploy. A Dockerfile fix changes the build instructions, but you still need to inspect the packages in the built image. You can generate a software bill of materials (SBOM) from that same image.

Scan PR source and configuration into a JSON report. Record vulnerabilities and the SBOM for the final image, then use HIGH and CRITICAL findings to make the deployment decision in the CI example.

Choose scanners for the target

--scanners What it checks What to do with the results
vuln Known vulnerabilities in OS and application dependencies Check the installed version, the fixed version, and how the dependency was introduced.
misconfig Dockerfile, Kubernetes, and Terraform configuration Fix the declaration that produced the finding.
secret Potential keys or tokens If a credential is real, revoke and reissue it and determine what was exposed.
license Package and file licenses Compare the results with your team’s license policy.

Scanner defaults depend on the target. For example, in their default output format, fs and image enable vuln and secret. Specify --scanners to make the scope explicit. Filesystem scans identify dependencies from supported inputs such as lockfiles. Filesystem scanning · Scanner selection · Vulnerability scanning

1. Check the Dockerfile configuration

Install Trivy and start in an empty practice directory. These commands check a synthetic Dockerfile statically and can run without a Docker engine. On 2026-12-01, the official release download’s SHA-256 matched the published checksum, and Trivy 0.76.2 ran on macOS ARM64. Installation guide · Trivy 0.76.2 release

mkdir -p trivy-demo
printf '%s\n' 'FROM alpine:3.22' 'USER root' > trivy-demo/Dockerfile
trivy fs --scanners misconfig trivy-demo

Trivy reported DS-0002 (HIGH) for USER root and DS-0026 (LOW) for the missing HEALTHCHECK. Filter to HIGH,CRITICAL and set the exit code to 1 when the scan finds an issue at those severities.

trivy fs --scanners misconfig --severity HIGH,CRITICAL --exit-code 1 trivy-demo
echo $?

printf '%s\n' 'FROM alpine:3.22' 'USER 65532:65532' > trivy-demo/Dockerfile
trivy fs --scanners misconfig --severity HIGH,CRITICAL --exit-code 1 trivy-demo
echo $?
trivy fs --scanners misconfig trivy-demo

The first echo $? printed 1. After switching to USER 65532:65532, it printed 0. The final unfiltered report still showed the LOW finding. For a real app, check that this user can access the required files and that the app starts successfully.

2. Scan the final image and generate its SBOM

Fetch the public alpine:3.22 from the registry and scan linux/arm64. The first command saves a vulnerability report. The second saves a CycloneDX SBOM.

trivy image --image-src remote --platform linux/arm64 --scanners vuln \
  --format json --output alpine-vuln.json alpine:3.22

trivy image --image-src remote --platform linux/arm64 \
  --format cyclonedx --output alpine.cdx.json alpine:3.22

The 2026-12-01 run found Alpine 3.22.8, 15 vulnerability report entries, and 17 CycloneDX components. The 15 entries count report rows. Count distinct CVEs separately. The observed image digest was alpine@sha256:49340b346798875b5592e5d420241f2f9cfba5f47e62ebb1446a1b34734e898f. Results can change as the tag or vulnerability DB changes.

The default CycloneDX output lists the components. To include vulnerabilities, explicitly add --scanners vuln. Before deployment, use the digest of the image you will deploy. If it is a multi-architecture image, scan for the deployment architecture. Container images · CycloneDX SBOM · Image command options

3. Use reports and a severity threshold in CI

In this CI example, run the PR scan from the checkout directory to create the JSON report. Before deployment, set IMAGE to the actual deployment image URL or digest reference. The Dockerfile, image, and SBOM commands above were run directly. The CI example here was not installed or run in CI.

trivy fs --scanners vuln,misconfig,secret --format json --output trivy-fs.json .
trivy image --scanners vuln --severity HIGH,CRITICAL --exit-code 1 "$IMAGE"

Restrict access to the JSON report and use the CI logs to distinguish DB download and registry access errors from scanner findings. Record the Trivy version, scan time, image digest, and options.

--ignore-unfixed excludes vulnerabilities without a fix version from the displayed results. If you use it, retain a separate unfiltered report and assign an owner and a review date to the remaining items. Exit code configuration · Image command options

Discussion

View on GitHub ↗