Cross-compiling a Go binary
Cross-compilation means a compiler runs on the build platform and emits code for a different target platform. For this dependency-free Go CLI, the compiler can target Linux AMD64 or ARM64 without executing the resulting foreign binary. The earlier multi-stage pattern keeps the compiler outside the runtime image.
Worked example
Use this worksheet offline. Names and values are fictional; no live builder, credentials, Docker daemon or external requests are needed. Commands are illustrations, not observed results. Before separately authorized verification, identify the effective Docker endpoint, client credential configuration, builder and registry destination as applicable. Never disclose credential files or private context details.
Illustrative Dockerfile (not built)FROM --platform=$BUILDPLATFORM golang:1.26 AS compileARG TARGETOSARG TARGETARCHWORKDIR /srcCOPY main.go .RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -o /out/hello main.goFROM scratchCOPY --from=compile /out/hello /helloUSER 65532:65532ENTRYPOINT ["/hello"]The first stage runs on the build platform. Redeclaring target arguments within the stage makes them available to RUN; Go receives the requested OS and architecture. The final stage uses the requested target platform and copies only the binary. This example targets Linux AMD64 and ARM64, not all Go targets. CGO is disabled for this simple program; applications using C libraries, certificates or external files need extra engineering. The tag is illustrative, not digest-pinned or availability-verified. Use the supplied offline test to compile both binaries with local Go and inspect their ELF headers. A correct header is not a successful target run. The supplied Python test uses the installed local Go compiler with module network access disabled, not the Dockerfile’s illustrative Go 1.26 tag. It records actual compiler version and ELF machine fields. Local native execution tests the CLI output; scratch packaging and image configuration remain separate checks.
Common mistake
Do not run the foreign binary on the builder by assumption or use CGO_ENABLED=0 as a universal compatibility fix.
Manual practice
Explain which architecture runs the compiler and which architecture the resulting ARM64 binary targets. Inspect the local fixture evidence.
Worksheet: lab-fixtures/docker-buildx-multi-platform/05-cross-compilation/scenario.txt.
Optional local exercise from package root: python3 lab-fixtures/docker-buildx-multi-platform/05-cross-compilation/test_cross_compile.py. Requires an installed Go compiler and Python 3; no Docker or network. It verifies ELF machine fields and runs only a matching Linux native binary.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cross-compiling a Go binary — Knowledge check
Reason about multi-platform build reasoning.
What is BUILDPLATFORM used for here?
Which variables select Go target?
What does checking the ELF e_machine field alone establish?