Sub topic 3
Why emulated builds are slow and what cross-compilation changes
This chapter assumes you can read a multi-stage Dockerfile and know that amd64 and arm64 are different CPU architectures. It also assumes you know the difference between the build platform (the machine running the build) and the target platform (the machine the image will run on).
How emulated builds work
Suppose you run docker buildx build --platform linux/arm64 on an amd64 laptop. By default, every stage uses base images that match the target platform, so the golang image in your build stage is an arm64 image. Its go compiler is an arm64 binary, and the host CPU cannot run it directly.
Docker gets around this with QEMU user-mode emulation. The Linux kernel feature binfmt_misc recognizes foreign binaries and hands them to a QEMU program. QEMU reads each arm64 instruction and translates it into amd64 instructions while the program runs. System calls are translated too.
Here is a stage that triggers this:
# syntax=docker/dockerfile:1
FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN go build -o /out/app ./cmd/appFor the arm64 target, the go build command runs under emulation. That includes the compiler, the linker, and every helper process they start.
Why emulation is slow
A compiler is a bad fit for an emulator for three reasons.
- It is CPU-bound. It spends nearly all its time executing instructions, and every instruction pays the translation cost.
- It starts many short-lived processes. Each process start adds emulation overhead.
- It does a lot of file and memory work. Translating those system calls adds more cost.
The slowdown is often several times, and it can be much worse for large projects. Emulation can also cause odd failures, such as crashes in tools that use unusual instructions.
None of this work needs emulation. The compiler only needs to produce arm64 machine code. It does not need to run on an arm64 CPU.
What cross-compilation changes
A cross-compiler runs on one architecture and emits code for another. Go has this built in. Setting GOOS and GOARCH tells the same go binary which operating system and CPU to produce code for.
In the Dockerfile, you pin the build stage to the build platform. The compiler then runs natively and writes a target binary. Emulation is no longer needed, because the final stage only copies a file and never executes anything.
# syntax=docker/dockerfile:1
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 ./cmd/app
FROM gcr.io/distroless/static-debian12
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]The next chapters explain $BUILDPLATFORM, ARG scoping, and FROM --platform in detail. For now, only the shape of the result matters.
What stays the same
The final image is the same in both cases. It contains an arm64 binary on an arm64 base. Only the way you produced the binary differs.
| Row | Emulated build | Cross-compiled build |
|---|---|---|
| Build stage base image | Target platform | Build platform |
| Compiler runs | Under QEMU | Natively |
| Needs `GOOS` and `GOARCH` | No | Yes |
| Works for any language | Yes | Only if the toolchain can cross-compile |
Exercise
A build for linux/arm64 runs on an amd64 host, and the go build step takes ten minutes. You change the build stage to FROM --platform=$BUILDPLATFORM golang:1.22 and pass GOOS=$TARGETOS GOARCH=$TARGETARCH to go build. Why is the step now faster, and why is the resulting binary still an arm64 binary?
Show answer
The go compiler now comes from an amd64 image, so it runs natively on the host and QEMU translates nothing. The binary is still arm64 because GOARCH=$TARGETARCH tells the compiler to emit arm64 machine code.