Sub topic 12
Scanning build stages and the build context
This chapter assumes you can already generate an SBOM with --sbom=true and read it with docker buildx imagetools inspect. Here you will change what the scanner looks at.
What the scanner covers by default
The BuildKit SBOM scanner analyzes only the final stage of your Dockerfile. In a multi-stage build, the compilers, headers, and libraries used in earlier stages are not listed. Often that is what you want, because those packages are not in the shipped image.
It becomes a gap when the final image is scratch or a distroless base. A statically linked binary copied into scratch yields an almost empty SBOM, even though the build installed many packages to produce it. Those packages affect what the binary contains, so a reviewer may want them listed.
Scanning an intermediate stage
To include an earlier stage, set the build argument BUILDKIT_SBOM_SCAN_STAGE to true. Declare it with ARG inside the stage you want scanned:
FROM alpine:3.20 AS build
ARG BUILDKIT_SBOM_SCAN_STAGE=true
RUN apk add --no-cache build-base
RUN echo 'int main(void){return 0;}' > /tmp/app.c \
&& gcc -static -o /app /tmp/app.c
FROM scratch
COPY --from=build /app /app
CMD ["/app"]Build it with the SBOM attestation enabled:
docker buildx build \
--sbom=true \
--tag registry.example.com/demo/app:1.0 \
--push .The SBOM now lists the packages from the build stage, such as build-base, in addition to anything in the final stage.
You can also pass the value on the command line with --build-arg BUILDKIT_SBOM_SCAN_STAGE=true. The Dockerfile must still declare the ARG, because BuildKit only reads build arguments that the Dockerfile declares.
Scanning the build context
The build context is the set of files you send to the builder, for example the . in docker buildx build .. It can contain lockfiles and vendored dependencies that never become packages in an image layer. To have the scanner analyze it, use BUILDKIT_SBOM_SCAN_CONTEXT:
ARG BUILDKIT_SBOM_SCAN_CONTEXT=true
FROM alpine:3.20
# Redeclare without a value so this stage inherits the global setting
ARG BUILDKIT_SBOM_SCAN_CONTEXT
COPY . /srcDeclare the argument before the first FROM, then redeclare it without a value in the stage that should use it. This is the normal Dockerfile rule for global arguments.
Common errors
- No change in the SBOM. The
ARGis missing from the Dockerfile, or it is declared in a different stage than the one you meant to scan. - Flag typed as
--sbomonly. Without the SBOM attestation enabled, the build arguments have no effect. - Unexpected entries. Scanning the context can list files you did not intend to publish. Review the SBOM before pushing, and keep a
.dockerignorefile so unrelated files are not sent in the context.
Exercise
Your final image is FROM scratch and contains one static binary. The SBOM from --sbom=true lists almost nothing. Which argument do you add, and where, to list the packages used to compile the binary?
Show answer
Add ARG BUILDKIT_SBOM_SCAN_STAGE=true inside the build stage that installs the compiler and packages (or pass --build-arg BUILDKIT_SBOM_SCAN_STAGE=true, with the ARG still declared in that stage). Rebuild with --sbom=true.
Exercise
Why must BUILDKIT_SBOM_SCAN_CONTEXT be declared with ARG in the Dockerfile even when you pass it with --build-arg?
Show answer
BuildKit only uses build arguments that the Dockerfile declares. An undeclared argument is ignored, so the scanner never sees the setting.