Sub topic 10
Cross-compiling with BUILDPLATFORM and TARGETARCH
This chapter assumes you can read a multi-stage Dockerfile and have run docker buildx build --platform linux/amd64,linux/arm64. It also assumes you know that building a non-native platform by QEMU emulation is slow.
The problem with emulated compiles
Under emulation, every RUN step in a build for another architecture executes through QEMU. A Go or Rust compile can run 5 to 10 times slower this way. Many compilers can instead run natively on the build machine and produce binaries for a different target CPU. That is cross-compilation. BuildKit supports it by telling your Dockerfile which platform it runs on and which platform it builds for.
Platform build arguments
BuildKit sets these arguments automatically for each platform in a build:
| Argument | Meaning | Example on an amd64 host building arm64 |
|---|---|---|
| `BUILDPLATFORM` | Platform of the machine running the build | `linux/amd64` |
| `TARGETPLATFORM` | Platform the image is built for | `linux/arm64` |
| `TARGETOS` | OS part of the target | `linux` |
| `TARGETARCH` | Architecture part of the target | `arm64` |
| `TARGETVARIANT` | Variant part of the target, if any | empty for arm64 |
The BUILDPLATFORM and TARGETPLATFORM arguments can be used in FROM lines without declaring them. The other arguments are not in scope inside a stage until you declare them with ARG.
A cross-compiling Dockerfile
FROM --platform=$BUILDPLATFORM golang:1.22 AS build
ARG TARGETOS
ARG TARGETARCH
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /out/app .
FROM alpine:3.20
COPY --from=build /out/app /usr/local/bin/app
ENTRYPOINT ["/usr/local/bin/app"]The first stage is pinned to $BUILDPLATFORM. BuildKit pulls the Go image for the build machine, so the compiler runs natively. Go reads GOOS and GOARCH and emits a binary for the target. The final stage has no --platform flag, so it defaults to the target platform. That stage pulls the arm64 Alpine image for the arm64 build and only runs COPY and metadata instructions, so nothing needs emulation.
Build it the usual way:
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t registry.example.com/team/app:1.0 \
--push .BuildKit runs the build stage once for each target platform. Each run gets different TARGETARCH values, and each produces its own binary and its own manifest in the index.
Common errors
- Missing
ARGdeclaration. If you skipARG TARGETARCH, the variable is empty inside the stage.GOARCH=then falls back to the build machine's architecture, and you get a wrong-architecture binary with no build error. --platform=$BUILDPLATFORMon the final stage. The image then has the build machine's architecture in every index entry. The arm64 entry would contain amd64 content.- Cgo or native libraries.
CGO_ENABLED=0avoids the need for a C cross-compiler. If you need cgo, install a cross toolchain in the build stage or fall back to a native or emulated build. - Runtime
RUNin the final stage. A step such asRUN apk add curlin the final stage runs under emulation on non-native targets. Put it in a stage that you can pin to$BUILDPLATFORM, or accept the cost.
Variants and renamed architectures
TARGETARCH gives Docker's names, such as amd64 and arm64. Some tools use different names, for example x86_64 and aarch64. When downloading a prebuilt tool for the target, map the names in a shell case statement instead of assuming they match.
Exercise
A build for linux/arm64 on an amd64 machine produces an image whose binary fails with "exec format error". The Dockerfile declares FROM --platform=$BUILDPLATFORM golang:1.22 AS build but has no ARG TARGETARCH line, and the build command uses GOARCH=$TARGETARCH. What is the cause and the fix?
Show answer
Without ARG TARGETARCH inside the stage, the variable is empty, so GOARCH is unset and Go compiles for the build machine's architecture (amd64). Add ARG TARGETOS and ARG TARGETARCH after the FROM line of the build stage so BuildKit passes in the target values.