Sub topic 12
Emulation, native nodes, or cross-compilation: how to choose
This chapter assumes you have built an image with docker buildx build --platform, know what a manifest list is, and have seen the three approaches in earlier chapters: QEMU emulation, native nodes in one builder, and cross-compilation in the Dockerfile. Here you will compare them and pick one for a given project.
What each approach costs
Emulation runs foreign-architecture binaries through QEMU user-mode translation. Every RUN step on the non-native platform executes instructions one at a time through a translator. Setup is a single binfmt install, and the Dockerfile does not change.
Native nodes send each platform's build to a machine of that architecture. Nothing is translated, so the speed matches a normal single-platform build. You pay for and maintain at least two machines, and they must be reachable from the builder, for example over SSH.
Cross-compilation keeps the slow work on the builder's own platform. The build stage uses --platform=$BUILDPLATFORM and the compiler produces a binary for TARGETARCH. Only the final stage, which usually just copies files, depends on the target. It needs a toolchain that can cross-compile, so it suits Go, Rust, and some C projects, but not every language.
Comparison
| Approach | Build speed | Setup effort | Dockerfile changes | Correctness risk |
|---|---|---|---|---|
| QEMU emulation | Slow, often several times slower on compile-heavy steps | Low | None | Medium: translator bugs and unsupported instructions |
| Native nodes | Full native speed | High: extra machines and access | None | Low |
| Cross-compilation | Near native | Medium | Required | Medium: you must get flags and linked libraries right |
Emulation can fail in ways native builds do not. Some programs crash, hang, or misdetect CPU features under QEMU. Package managers that run post-install scripts for the target architecture are a common place for this. Cross-compilation has its own risk. If the binary links against C libraries, you must supply target-architecture versions of them, or the output will not run.
A decision path
You can combine approaches. A common setup uses cross-compilation for the compile stage and emulation only for a small final stage that runs a few apk add or apt-get install commands. That keeps the slow, translated portion short.
Rules of thumb
- Start with emulation for small images, scripts, and infrequent builds.
- Move to cross-compilation when a compiled language dominates build time and its toolchain supports other targets.
- Add native nodes when builds are frequent, run long under emulation, or fail under QEMU.
- Test the produced image on real hardware of each architecture before you release it, whichever approach you used.
Exercise
A team ships a Python service whose dependencies include C extensions that compile during pip install. They build weekly, and the emulated build takes 40 minutes. They own one amd64 server and one arm64 server. Which approach would you recommend, and why is cross-compilation a poor fit?
Show answer
Use native nodes. The C extensions compile inside pip install, so the slow step cannot simply be cross-compiled by switching a Go-style compiler flag. You would need target-architecture libraries and toolchains. Under emulation the compile takes 40 minutes. The team already owns one machine of each architecture, so attaching them as native nodes removes the translation cost without Dockerfile changes.