Sub topic 9
Emulation, native builders and cross-compilation compared
This chapter assumes you can build and push images with docker buildx, read a Dockerfile, and know that an image index maps platforms to manifests. Here you choose how each platform's image actually gets built.
Three ways to produce a foreign-architecture image
When you build linux/arm64 on an amd64 machine, the builder has three options.
- Emulation. QEMU runs arm64 binaries on the amd64 CPU. Every
RUNstep executes as if on arm64. - Native builders. Each platform builds on a machine with that CPU. An amd64 node builds amd64 and an arm64 node builds arm64.
- Cross-compilation. The build runs natively on the build machine. The compiler produces binaries for another target.
Emulation
Emulation needs no changes to your Dockerfile. You register QEMU handlers once on the host:
docker run --privileged --rm tonistiivi/binfmt --install arm64Then a single builder can build both platforms:
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t registry.example.com/team/app:1.0 \
--push .The cost is speed. Emulated steps are often several times slower than native ones, and compilers, package installs and tests suffer most. Rare instruction or syscall differences can also cause crashes that do not occur on real hardware. Emulation is a good default for small images, scripts and occasional builds.
Native builders
A builder can have several nodes, each pointing at a different machine. Buildx sends each platform to a node that matches it, so no emulation is needed. This gives the most faithful result, because tests and RUN steps execute on the real CPU.
You pay for it in infrastructure. You must run and maintain an arm64 machine, secure the connection to it, and keep it available. Many CI systems offer native runners for both architectures, which is the easiest way to get this.
Cross-compilation
Cross-compilation changes the Dockerfile so the slow, CPU-heavy work runs on the build platform. Only the final, small stage targets the foreign architecture.
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 build stage is pinned to $BUILDPLATFORM, so it runs natively. Go reads GOOS and GOARCH and emits the right binary. The final stage has no RUN steps, so it needs no execution of foreign code at all.
This works well for languages with built-in cross-compilers, such as Go and Rust. It is harder when you need native C libraries for the target, or when the build runs target-specific tools.
Comparison
| Strategy | Dockerfile changes | Speed | Fidelity | Extra infrastructure |
|---|---|---|---|---|
| Emulation | None | Slow | Good, with rare quirks | QEMU handlers |
| Native builders | None | Fast | Best | An arm64 and an amd64 node |
| Cross-compilation | Required | Fast | Depends on toolchain | None |
Choosing
- If the image only copies files or installs a few packages, use emulation.
- If the language cross-compiles cleanly, use cross-compilation and keep the final stage free of
RUNsteps. - If builds need target-native tools or long test runs, use native builders.
- You can mix them. Build each architecture on a native runner and combine the results into one index later.
Exercise
A Go service takes 2 minutes to build on amd64 but 25 minutes for linux/arm64 under QEMU. What Dockerfile change reduces the arm64 time, and why does it work?
Show answer
Pin the build stage with FROM --platform=$BUILDPLATFORM and pass GOOS=$TARGETOS GOARCH=$TARGETARCH to go build. The compiler then runs natively on the build machine and emits an arm64 binary. Only the final stage targets arm64, and it has no RUN steps, so nothing needs to be emulated.