Sub topic 13
Verifying the result and avoiding common pitfalls
This chapter assumes you can write a multi-stage Dockerfile, know what BUILDPLATFORM, TARGETOS and TARGETARCH mean, and have run docker buildx build --platform at least once. Here you check that each architecture got the right binary, and you learn the mistakes that most often break a cross-build without producing an error.
Check the published manifest
A successful push does not prove every platform was published. After the push, inspect the tag:
docker buildx imagetools inspect example/app:latestThe output lists a manifest list with one entry per platform, each with its own digest. Look for linux/amd64 and linux/arm64. If a requested platform is missing, the build did not publish it.
Check the binary inside each image
The manifest tells you which platforms exist. It does not tell you the binary matches the label. Pull one platform explicitly and read the architecture Docker recorded:
docker pull --platform linux/arm64 example/app:latest
docker image inspect example/app:latest --format '{{.Os}}/{{.Architecture}}'This should print linux/arm64. Running the image with docker run --rm --platform linux/arm64 example/app:latest goes further. On an amd64 host it runs under QEMU, so it is slow, but it fails with exec format error if the binary has the wrong architecture.
Common pitfalls
| Symptom | Cause | Fix |
|---|---|---|
| `$TARGETARCH` is empty and `go build` uses the host architecture | The ARG was not declared inside the stage | Add `ARG TARGETARCH` after the `FROM` line of the stage that uses it |
| The build is slow even with a Go compile stage | `FROM golang:1.22` has no `--platform=$BUILDPLATFORM`, so the compiler runs under QEMU | Pin the build stage with `FROM --platform=$BUILDPLATFORM` |
| Binary is the wrong architecture | `GOARCH` was set to `$BUILDARCH` or hard-coded | Use `$TARGETARCH` |
| `--load` fails with several platforms | The default Docker image store cannot hold a multi-platform image | Use `--push`, or build one platform at a time with `--load` |
| `linux/arm/v7` binary crashes on older hardware | `GOARM` was not set | Use `GOARM=${TARGETVARIANT#v}` |
| Build fails on a C header or library | cgo needs a C cross-compiler and target libraries | See below |
An ARG declared before the first FROM is visible only to FROM lines. It is not visible inside a stage unless you redeclare it there. This explains most "empty variable" reports.
Setting GOARM for 32-bit ARM
For linux/arm/v7, TARGETVARIANT is v7. For linux/arm64 and linux/amd64 it is empty. Stripping the leading v gives Go the value it expects:
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH GOARM=${TARGETVARIANT#v} \
go build -o /out/app ./cmd/appThe stage must also declare ARG TARGETVARIANT. Go ignores GOARM when GOARCH is not arm, so one command line works for every platform.
When cgo breaks the plan
The Go examples set CGO_ENABLED=0, which produces a static binary that the Go toolchain can cross-compile alone. If your program or a dependency uses cgo, Go calls a C compiler. The compiler in the golang image targets the build machine, so linking fails or produces a binary for the wrong architecture.
You have three options:
- Remove the cgo dependency, for example by choosing a pure-Go library.
- Install a cross C toolchain and target-architecture libraries in the build stage, and set
CCfor each target. - Drop
--platform=$BUILDPLATFORMfor that stage and accept emulated builds.
Option 1 is the cheapest. Option 3 is the most reliable but the slowest.
Keep the cache useful
Copy go.mod and go.sum and run go mod download before copying the source, as in the earlier Dockerfile. The download layer is then reused until dependencies change. The layer does not depend on the target, so both architectures can reuse it.
Exercise
You build with --platform linux/amd64,linux/arm64. imagetools inspect lists both platforms, but docker run --platform linux/arm64 prints exec format error. What is the most likely cause, and where do you look first?
Show answer
The binary inside the arm64 image is not arm64, even though the manifest says it is. Look at the compile step in the build stage. Check that ARG TARGETARCH is declared inside that stage and that GOARCH=$TARGETARCH is used, not a hard-coded or build-platform value.