How do you generate an SBOM with Trivy?

IntermediateHands-onTrivy
Answer

Short answer

Run Trivy with --format cyclonedx or --format spdx-json against an image or folder to produce a Software Bill of Materials listing every package and version. You can later scan that SBOM with trivy sbom without needing the original image.

Commands

trivy image --format cyclonedx --output sbom.cdx.json shop-api:1.4.0
trivy image --format spdx-json --output sbom.spdx.json shop-api:1.4.0
trivy fs --format cyclonedx --output sbom.cdx.json .

trivy sbom sbom.cdx.json                                # scan the SBOM for CVEs later
trivy sbom --severity CRITICAL --exit-code 1 sbom.cdx.json

Attach the SBOM to the image (supply chain)

cosign attest --predicate sbom.cdx.json --type cyclonedx registry/shop-api@sha256:...

Why it's useful

When the next Log4Shell-type CVE appears, you can search stored SBOMs to see which releases contain the vulnerable package — in minutes, without rescanning every image.

What to say in the interview

"trivy image or fs with --format cyclonedx or spdx-json generates the SBOM; trivy sbom rescans it later. We store SBOMs with each release or attest them to the image with cosign so we can answer 'are we affected?' quickly."

All Security Tools questions Roadmap: Trivy — image, file system & IaC scanning

More on Trivy