Guides

How to generate an SBOM for a container image (Docker and OCI)

By SBOMBox SBOM experts · · 3 min read

Short answer: run an SBOM generator against the built image, not just your source tree. syft your-image:tag -o cyclonedx-json or trivy image --format cyclonedx your-image:tag both work, and Docker can attach an SBOM attestation at build time with docker buildx build --sbom=true. A container SBOM must list the operating-system packages in the base image as well as your application dependencies.

Why a container SBOM is different

This is why generating from the artifact you ship matters. A source-tree SBOM and an image SBOM for the same service can list very different components.

Four ways to generate one

  1. SyftScans the image layers and catalogues OS and language packages. Works on local images, registries and saved tarballs.
    syft your-image:tag -o cyclonedx-json=image.cdx.json
    syft registry:ghcr.io/acme/api:1.4.0 -o spdx-json=image.spdx.json
  2. TrivyIf Trivy already scans your images for vulnerabilities, ask it for an SBOM too.
    trivy image --format cyclonedx --output image.cdx.json your-image:tag
  3. Docker ScoutBuilt into recent Docker Desktop and CLI releases.
    docker scout sbom --format spdx --output image.spdx.json your-image:tag
  4. BuildKit SBOM attestationGenerates the SBOM during the build and attaches it to the image in the registry as an in-toto attestation (SPDX).
    docker buildx build --sbom=true --push -t ghcr.io/acme/api:1.4.0 .

Comparing generators? See Syft vs Trivy vs cdxgen.

Sign and attach the SBOM to the image

An SBOM sitting next to an image in CI is easy to lose or swap. Attach it to the image digest and sign it, so anyone pulling the image can verify both. With Sigstore's cosign:

  1. Attest the SBOM to the image digestSign the SBOM as an attestation bound to the exact image digest, not a mutable tag.
    cosign attest --predicate image.cdx.json --type cyclonedx ghcr.io/acme/api@sha256:<digest>
  2. Verify it laterConsumers verify the attestation before trusting the SBOM.
    cosign verify-attestation --type cyclonedx ghcr.io/acme/api@sha256:<digest>

More on why this matters in SBOM signing and provenance.

What to check in a container SBOM

Keep it current

Regenerate the SBOM on every image build, including scheduled base-image rebuilds, and keep one per pushed digest. Then monitor them: a base image that was clean last month may contain a newly exploited OpenSSL or glibc vulnerability today. Distroless and minimal base images reduce the component count, which makes the SBOM smaller and the vulnerability list shorter.

Upload any image SBOM to the free SBOM checker to see how complete it is, or use SBOMBox to store and monitor one per release.