Sub topic 10
Handling variants such as linux/arm/v7
This chapter assumes you can write a multi-stage Dockerfile, you know FROM --platform=$BUILDPLATFORM, and you have declared TARGETOS and TARGETARCH with ARG. Here you extend that setup to 32-bit ARM, where the architecture name alone is not enough.
Why a variant exists
Some architectures come in revisions that need different machine code. A platform string has up to three parts: os/architecture/variant. The ARM family is the common case:
| Platform | TARGETOS | TARGETARCH | TARGETVARIANT |
|---|---|---|---|
| linux/amd64 | linux | amd64 | (empty) |
| linux/arm64 | linux | arm64 | (empty) |
| linux/arm/v7 | linux | arm | v7 |
| linux/arm/v6 | linux | arm | v6 |
Both arm/v6 and arm/v7 have TARGETARCH=arm. A build that reads only TARGETARCH cannot tell them apart. TARGETVARIANT carries the difference.
How Go uses the variant
Go selects the ARM instruction set with the GOARM environment variable. It accepts 5, 6 or 7. Go ignores it for every other GOARCH. If GOARM is empty, Go uses its default.
TARGETVARIANT holds v7, but GOARM wants 7. The shell expansion ${TARGETVARIANT#v} removes a leading v. It gives 7 for v7 and an empty string when the variable is empty.
# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM golang:1.22 AS build
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH GOARM=${TARGETVARIANT#v} \
go build -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]The three ARG lines come after FROM. As the scoping chapter explained, the automatic arguments are only visible in a stage that declares them. If you leave out ARG TARGETVARIANT, the expansion is always empty. The build still succeeds, but the 32-bit ARM binary is built with Go's default GOARM instead of v7.
Building and checking the variant
Add the variant to the platform list. Each entry becomes its own build of the Dockerfile.
docker buildx build \
--platform linux/amd64,linux/arm64,linux/arm/v7 \
-t example/app:latest \
--push .The build stage still runs natively on your machine through FROM --platform=$BUILDPLATFORM. Go compiles all three targets without emulation. Only the final stage is resolved per target platform, and distroless/static-debian12 publishes an arm/v7 image. Check that your runtime base image does too before you add a variant.
Confirm the result with the inspect command from the verification chapter:
docker buildx imagetools inspect example/app:latestYou should see a separate entry with Platform: linux/arm/v7 and its own digest.
Common mistakes
- Reading only
TARGETARCH.arm/v6andarm/v7builds come out identical, and a v7 binary can crash with an illegal instruction on a v6 device. - Forgetting the
ARGdeclaration. The variable silently expands to nothing. - Passing
GOARMfor every platform and assuming it matters. It is harmless on amd64 and arm64, because Go ignores it there. That is why oneRUNline can serve all platforms. - Using
--platform linux/arm. Without a variant, BuildKit picks a default that may not match your devices. Name the variant explicitly. - Expecting this to work with cgo. A C toolchain needs its own cross-compiler, and the next chapters cover that.
Exercise
A build for linux/arm/v6 produces a binary that crashes with "illegal instruction" on a Raspberry Pi Zero. The Dockerfile has ARG TARGETOS and ARG TARGETARCH in the build stage, and the build command sets GOOS=$TARGETOS GOARCH=$TARGETARCH. What is missing, and what exact change fixes it?
Show answer
The variant is never passed to Go, so Go uses its default GOARM instead of 6. Add ARG TARGETVARIANT in the build stage and set GOARM=${TARGETVARIANT#v} in the go build command's environment. For linux/arm/v6 this gives GOARM=6.