Sub topic 7
Cross-compiling a Go program in the build stage
This chapter assumes you can write a multi-stage Dockerfile and know what BUILDPLATFORM and TARGETPLATFORM mean. You should also know that ARGs must be declared inside a stage before you use them, and that FROM --platform=$BUILDPLATFORM pins a stage to the build machine.
The idea
Go compiles for any supported operating system and architecture from any host. You pick the target with two environment variables, GOOS and GOARCH. That makes Go a good fit for cross-compiling in Docker. The compile stage runs natively on the build machine, so it is fast. The compiler still emits a binary for the target. The final stage only copies that binary in.
The Dockerfile
# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM golang:1.22 AS build
ARG TARGETOS
ARG TARGETARCH
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Walk through it in order.
FROM --platform=$BUILDPLATFORM golang:1.22 AS buildpulls the Go image for the machine doing the build. On an amd64 laptop, this stage runs as amd64 even when you build for arm64.ARG TARGETOSandARG TARGETARCHdeclare the values inside this stage. BuildKit fills them in from the platform you request. Without these lines the variables are empty in theRUNstep.go mod downloadruns before the source is copied. Dependencies are cached untilgo.modorgo.sumchange.- The
go buildline setsGOOSandGOARCHfrom the build arguments. The output is an executable for the target. - The final stage has no
--platformflag, so it uses the image for the target platform.COPY --from=buildonly moves files, so nothing is executed in that stage.
For --platform linux/arm64, TARGETOS is linux and TARGETARCH is arm64. The compile line then behaves as if you had typed GOOS=linux GOARCH=arm64 go build.
Why CGO_ENABLED=0
By default Go can link against C libraries through cgo. That needs a C compiler for the target. In a Go image on the build machine, you only have the build machine's compiler. Setting CGO_ENABLED=0 makes Go produce a statically linked binary in pure Go. That binary also runs on distroless/static, which has no C library. If your program needs cgo, plain cross-compilation is not enough, and a later chapter covers that case.
Handling ARM variants
The linux/arm/v7 platform has TARGETARCH=arm and TARGETVARIANT=v7. Go needs the ARM version in GOARM, and it expects 7, not v7. Strip the leading v with shell parameter expansion:
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH GOARM=${TARGETVARIANT#v} \
go build -o /out/app ./cmd/appThis needs ARG TARGETVARIANT in the stage. For amd64 and arm64 the variant is empty, so GOARM is empty and Go ignores it.
Building it
Run one build for both architectures:
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t example/app:latest \
--push .BuildKit runs the Dockerfile once per platform. The build stage executes natively each time, with different TARGETARCH values. Only the small final stage differs between the two results.
Common mistakes
| Symptom | Likely cause |
|---|---|
| `exec format error` at runtime | `GOOS` or `GOARCH` not set, or the ARGs were not declared in the stage |
| `GOARCH` is empty and the build uses the host default | `ARG TARGETARCH` is missing from the build stage |
| Build is slow and compiles under QEMU | `--platform=$BUILDPLATFORM` is missing from the build stage's `FROM` |
| `invalid GOARM` | `TARGETVARIANT` was passed as `v7` instead of `7` |
Exercise
You build with --platform linux/arm64 on an amd64 machine. The build stage has FROM golang:1.22 AS build with no --platform flag, and it declares ARG TARGETOS and ARG TARGETARCH. The binary is correct, but the build is slow. Why, and what is the fix?
Show answer
Without --platform=$BUILDPLATFORM, the build stage uses the arm64 Go image, so every step including go build runs under QEMU emulation. The binary is right because GOOS and GOARCH are set, but compilation is slow. Fix it by writing FROM --platform=$BUILDPLATFORM golang:1.22 AS build.