Sub topic 12
Scanning build stages and the build context
This chapter assumes you can read a multi-stage Dockerfile, have already generated an SBOM with docker buildx build --sbom=true, and can inspect it with docker buildx imagetools inspect and jq. It covers the two build arguments that change what the scanner looks at, BUILDKIT_SBOM_SCAN_STAGE and BUILDKIT_SBOM_SCAN_CONTEXT. It also covers why you might want them and what they cost.
What the scanner looks at by default
The BuildKit SBOM scanner is a separate image that BuildKit runs after the build. By default it scans the filesystem of the final stage of your Dockerfile, which is the filesystem that ships in the image. Nothing else is examined.
For many Dockerfiles that is enough. Multi-stage builds are where it falls short, because the stages before the last one are thrown away. A typical pattern is to compile in a large builder stage and copy only the binary into a small final stage. The compiler, the language toolchain and the downloaded dependencies never reach the final filesystem, so the SBOM does not list them.
The default can also hide information in the other direction. If the final stage is FROM scratch, there is no package manager database to read. The scanner can only detect what it can fingerprint, such as metadata embedded in a binary.
| Scan target | Scanned by default | How to include it |
|---|---|---|
| Final stage filesystem | Yes | Nothing to do |
| Intermediate stage filesystem | No | `ARG BUILDKIT_SBOM_SCAN_STAGE=true` in that stage |
| Build context files | No | `BUILDKIT_SBOM_SCAN_CONTEXT=true` |
Scanning intermediate stages
To include an intermediate stage, declare the BUILDKIT_SBOM_SCAN_STAGE build argument inside that stage and set it to true. The argument is a signal to the scanner and does not change how the stage runs.
Create these files in an empty directory.
go.mod:
module example.com/demo
go 1.22main.go:
package main
import "fmt"
func main() {
fmt.Println("hello from a scanned build stage")
}Dockerfile:
# syntax=docker/dockerfile:1
FROM golang:1.22-alpine AS build
ARG BUILDKIT_SBOM_SCAN_STAGE=true
WORKDIR /src
COPY go.mod ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app .
FROM scratch
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Build and push it with an SBOM attestation:
docker buildx build \
--sbom=true \
--tag registry.example.com/demo/stages:1.0 \
--push .The build stage now contributes its packages to the SBOM. That includes the Alpine packages of the golang:1.22-alpine base image, and the Go toolchain if the scanner recognizes it. The scratch stage contributes almost nothing on its own.
Two details about how the argument works:
- It is per stage. Declaring the argument in
builddoes not turn on scanning for any other stage. Declare it in every stage you want scanned. - You can set it from the command line. If the Dockerfile has
ARG BUILDKIT_SBOM_SCAN_STAGEwithout a default, pass--build-arg BUILDKIT_SBOM_SCAN_STAGE=true. This lets you scan builder stages in CI without changing the file that developers use locally.
Reading the combined result
After a push, list the package names in the SBOM:
docker buildx imagetools inspect registry.example.com/demo/stages:1.0 \
--format '{{ json .SBOM }}' \
| jq -r '.. | objects | select(has("packages")) | .packages[].name' \
| sort -uThe .. filter walks the whole JSON document, so the command works whether the output is keyed by platform or not. Run it once with the argument set and once with it removed. The second list is much shorter. The difference is the build-time software that was never in the image.
Build-time packages are useful for two reasons. They tell you what software could have influenced the contents of the binary, which is the question a compromised-compiler advisory raises. They also let a vulnerability scanner flag a vulnerable build tool, which a final-stage-only SBOM would hide.
There is a trade-off. If a scanner treats every package in the SBOM as shipped software, the build-time packages produce findings for code that is not in the running container. Consumers of your SBOM may need to tell the two apart. Because the stage scan goes into the same attestation, there is no built-in label that says which package came from which stage. Check what your tooling does with that before you turn the option on for every image.
Scanning the build context
The build context is the set of files you send to the builder, usually the directory you pass at the end of docker buildx build. By default BuildKit does not scan it. The scan is enabled with BUILDKIT_SBOM_SCAN_CONTEXT=true.
The context scan is useful when the files that define your dependencies are not inside the final filesystem. Consider an image that contains only a compiled binary. The go.mod, package-lock.json or requirements.txt that describe its dependencies sit in the context, but the image has none of them. Scanning the context lets the scanner read those manifests and list the declared dependencies.
Unlike the stage argument, the context argument is usually set as a global argument before the first FROM, and then redeclared in the stage that should carry it:
# syntax=docker/dockerfile:1
ARG BUILDKIT_SBOM_SCAN_CONTEXT=true
FROM golang:1.22-alpine AS build
ARG BUILDKIT_SBOM_SCAN_CONTEXT
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app .
FROM scratch
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]You can also pass it on the command line, which keeps the Dockerfile unchanged:
docker buildx build \
--sbom=true \
--build-arg BUILDKIT_SBOM_SCAN_CONTEXT=true \
--tag registry.example.com/demo/context:1.0 \
--push .The ARG line in the stage still has to be there for the scanner to see the value.
Edge cases for the context scan
.dockerignoreapplies. Files excluded from the context are not sent to the builder, so they are not scanned. A lockfile that you ignore will not appear.- Everything in the context is a candidate. A context holding test fixtures, vendored sample projects or old manifests will add packages that have nothing to do with the image. Keep the context tight, or use a dedicated context directory.
- Only local context content is the point. The option targets the files you upload. A Git URL or remote tarball as the context is a different situation, and you should check the SBOM output before relying on it.
- Scan time grows with context size. The scanner reads the files it receives, so a large monorepo context makes the SBOM step slower.
Choosing between the options
Use the stage scan when the question is "what tools and libraries took part in building this?" Use the context scan when the question is "what dependencies did the source declare?" Final-stage scanning remains the answer to "what is in the image?"
You can combine all three. For scratch and distroless final stages, combining them is often the only way to get a useful SBOM. Whatever you choose, treat the scan as best-effort. The scanner reports what it recognizes, and a clean-looking SBOM is not proof that nothing else was present.
Exercise
A Dockerfile has a build stage that compiles a program and a final FROM scratch stage that copies in the binary. The SBOM from --sbom=true lists almost no packages. Name two changes that would add information, and say what each one adds.
Show answer
Add ARG BUILDKIT_SBOM_SCAN_STAGE=true to the build stage. This adds the packages of the builder stage, such as its base image packages and toolchain. Alternatively, set BUILDKIT_SBOM_SCAN_CONTEXT=true (and redeclare the ARG in the stage). This lets the scanner read dependency manifests such as go.mod or lockfiles in the build context and list the declared dependencies.
Exercise
You set BUILDKIT_SBOM_SCAN_STAGE=true in the build stage, but a test stage in the same Dockerfile does not appear in the SBOM. Why, and what do you change?
Show answer
The argument applies per stage. The test stage never declared it, so it is not scanned. Add ARG BUILDKIT_SBOM_SCAN_STAGE=true to the test stage, or declare the argument in that stage and pass --build-arg BUILDKIT_SBOM_SCAN_STAGE=true.
Exercise
Why can a context scan miss a package-lock.json that is clearly in your project directory?
Show answer
Files matched by .dockerignore are not sent to the builder, so they are not part of the build context and cannot be scanned. Remove the pattern that excludes the lockfile, or use a context that includes it.