How to generate an SBOM for a container image (Docker and OCI)
By SBOMBox SBOM experts · · 3 min readShort 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
- It includes OS packages from the base image (Debian, Alpine or Ubuntu packages, for example), which a source-tree SBOM never sees.
- It includes language packages actually installed in the image, which may differ from your lock file.
- It changes whenever the base image is rebuilt, even if your code did not change.
- Multi-stage builds mean build-only tools should not appear, and a source SBOM would wrongly include them.
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
- 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 - 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 - Docker ScoutBuilt into recent Docker Desktop and CLI releases.
docker scout sbom --format spdx --output image.spdx.json your-image:tag - 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:
- 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> - 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
- Both OS packages and application packages are present.
- Every component has an exact version and a package URL, for example pkg:deb/debian/openssl@3.0.11 or pkg:npm/express@4.19.2.
- The SBOM records the image digest, so it describes one exact image.
- Base-image components are attributed to the base image, so you know which fixes come from a base-image rebuild.
- Hashes, licences and the generating tool are included, in line with the SBOM minimum elements.
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.