Sub topic 11
Cross-compiling with BUILDPLATFORM and TARGETARCH
This chapter assumes you can write a multi-stage Dockerfile and that you have a working Buildx builder from earlier chapters. You should also know that --platform selects the target platforms.
The problem with emulated compiles
With QEMU emulation, every RUN step for a non-native platform executes under emulation. A compiler running that way can be many times slower than on native hardware. Cross-compilation avoids this. The compiler runs natively on the builder's own architecture and produces a binary for a different one. Only the final image is tied to the target architecture.
Build arguments that BuildKit sets
BuildKit defines these arguments automatically for each platform in a build:
| Argument | Meaning | Example |
|---|---|---|
| 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, if any | v7 |
You must declare one with ARG inside a stage before you can use it there.
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 uses --platform=$BUILDPLATFORM. BuildKit therefore pulls the Go toolchain image for the builder's own architecture, and the compile runs natively. Go reads GOOS and GOARCH and emits a binary for the target.
The final stage has no --platform, so it resolves to the target platform. BuildKit pulls the arm64 Alpine image for the arm64 build. That stage only copies a file, so no emulated process runs.
Build it the same way as before:
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t registry.example.com/team/app:1.0 \
--push .Limits and common errors
Cross-compilation depends on the language toolchain.
- Go and Rust can target other architectures with a flag or an installed target. Go needs no extra setup when
CGO_ENABLED=0. - C and C++ need a cross compiler such as
gcc-aarch64-linux-gnu, and you must mapTARGETARCHto its name. Docker usesarm64, while GNU tools useaarch64. - For 32-bit ARM,
TARGETARCHisarmandTARGETVARIANTisv7. Go needsGOARM=7set from that variant. - Any
RUNin the final stage still executes under emulation. Keep final stages toCOPYand metadata instructions where you can.
Exercise
A build stage is written as FROM golang:1.22 AS build with no --platform flag. You build for linux/amd64,linux/arm64 on an amd64 machine. Which change makes the arm64 compile run natively, and which two things must the compile command use?
Show answer
Change the line to FROM --platform=$BUILDPLATFORM golang:1.22 AS build. Without it, the arm64 build pulls an arm64 Go image and compiles under QEMU. The compile command must use GOOS=$TARGETOS and GOARCH=$TARGETARCH, and both arguments must be declared with ARG in that stage.