Multi-platform builds

Advanced · 1 min

Build one image tag for amd64 and arm64: --platform, the three strategies (QEMU, native nodes, cross-compiling), and the BUILDPLATFORM pattern.

Before thisBuildKit and docker buildxManifests, indexes and multi-platform images

Docker · 10 min · from 27:02

Multi-platform builds

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by DockerReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A container runs on the host's kernel and CPU, so an amd64 image fails on an arm64 host with exec format error unless something emulates it.
  • docker buildx build --platform linux/amd64,linux/arm64 builds every platform in one invocation and produces one tag that points at an index of per-platform images.
  • The image store must support indexes: the containerd image store, the default in Docker Desktop and Engine 29 and later, or a docker-container builder that pushes.
  • QEMU emulation needs no Dockerfile changes but can be many times slower for compilation. Native nodes and cross-compilation are faster.
  • Cross-compile by running the build stage on the build machine with FROM --platform=$BUILDPLATFORM and passing $TARGETOS and $TARGETARCH to the compiler.
  • The final stage should use the default target platform, so its base image matches each output platform.

One tag, many machines

Laptops went ARM, cloud providers sell cheaper ARM instances, and a Raspberry Pi in a homelab wants images too. Containers share the host’s kernel and run its CPU’s instructions directly, so an image built for linux/amd64 cannot run on linux/arm64 without emulation. You get exec format error and nothing else.

The answer is one tag that points at several images, one per platform. The registry stores an index listing them, and each host pulls the one that matches. How an index is laid out is an images lesson. This one is how to build it.

What a multi-platform image looks like

I fetched the index behind python:3.14-slim from Docker Hub’s registry API:

application/vnd.oci.image.index.v1+json
linux/amd64      sha256:080b4f1282ed
unknown/unknown  sha256:65c351f9e955  attestation-manifest
linux/arm/v5     sha256:d7ba268c5423
unknown/unknown  sha256:4aa741804ab9  attestation-manifest
linux/arm/v7     sha256:f00f8fc5d252
...
linux/arm64/v8   sha256:4106fd2b23bc
linux/386        sha256:8dc16b4ba18f
linux/ppc64le    sha256:af020ba4ca21
linux/riscv64    sha256:757d47086e96
linux/s390x      sha256:6a70070365b2

Eight platforms, each with an attestation manifest beside it holding its provenance and SBOM. docker pull python:3.14-slim on an arm64 host fetches the index and then only the linux/arm64/v8 image.

Building one

docker buildx build --platform linux/amd64,linux/arm64 \
  -t registry.example.com/notes:1.4.2 --push .

That needs an image store that can hold an index. The multi-platform page says Docker Desktop and Docker Engine 29.0 and later use the containerd image store by default, which can. On an older engine, or one upgraded with the classic store kept, either enable containerd in daemon.json or create a docker-container builder and push straight to a registry, since that driver does not load into the engine.

Check what you pushed with docker buildx imagetools inspect registry.example.com/notes:1.4.2, and test a foreign platform locally with docker run --platform linux/arm64 ....

Three ways to produce the other architecture

Emulation with QEMU. BuildKit runs the non-native RUN steps under QEMU’s user-mode emulator. No Dockerfile changes. Docker Desktop has it set up, and the docs say official BuildKit releases bundle the emulators. On a bare Linux host, docker run --privileged --rm tonistiigi/binfmt --install all registers them with the kernel’s binfmt_misc. The catch, in the docs’ words: emulation “can be much slower than native builds, especially for compute-heavy tasks like compilation.” A ten-minute native compile can take an hour.

Native nodes. One builder with several machines, each building its own architecture:

docker buildx create --name multi --use node-amd64
docker buildx create --name multi --append node-arm64

node-amd64 and node-arm64 are Docker contexts pointing at real machines. Fast, but you run a small build farm. Docker Build Cloud is a hosted version of the same idea.

Cross-compilation. Compile on the build machine for the target. Most compiled languages can: Go with GOOS and GOARCH, Rust with targets, C with a cross toolchain. The Dockerfile has to arrange it.

The BUILDPLATFORM pattern

BuildKit predefines BUILDPLATFORM, the machine doing the work, and TARGETPLATFORM with its parts TARGETOS and TARGETARCH, the platform being produced. Run the compile stage on the build platform and hand the target to the compiler:

# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM golang:1.25 AS build
ARG TARGETOS TARGETARCH
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /out/notes ./cmd/notes

FROM gcr.io/distroless/static-debian13
COPY --from=build /out/notes /notes
ENTRYPOINT ["/notes"]

I converted that Dockerfile to BuildKit’s build graph with BuildKit v0.35.1’s Dockerfile frontend as a Go library, building on amd64 for arm64, and printed each step’s platform:

arm64   Source [stage-1 1/2] FROM gcr.io/distroless/static-debian13:latest@sha256:4c2cd1dee442...
amd64   Source [build 1/4] FROM docker.io/library/golang:1.25@sha256:699337d62055...
amd64   Exec   [build 4/4] RUN CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o /out/notes ./cmd/notes
        File   [stage-1 2/2] COPY --from=build /out/notes /notes

The Go toolchain is the amd64 image and runs natively. Only its output is arm64. The runtime stage has no --platform, so it takes the target platform and gets the arm64 variant of distroless. Both images publish those platforms: golang:1.25 lists linux/amd64 and linux/arm64/v8 among others, and distroless/static-debian13 lists six Linux platforms.

Two of BuildKit’s build checks police this pattern. RedundantTargetPlatform flags --platform=$TARGETPLATFORM, which is already the default. FromPlatformFlagConstDisallowed flags a hard-coded --platform=linux/amd64, which silently builds the wrong architecture for every other target.

In CI

On GitHub Actions the usual trio is docker/setup-qemu-action, docker/setup-buildx-action and docker/build-push-action with platforms: linux/amd64,linux/arm64. For heavy builds, the multi-platform CI page shows Docker’s GitHub Builder reusable workflow running each platform on its own native runner, Arm runners included, and assembling one index at the end. Caching across those jobs is the next lesson.

About the video

The embed is a 10-minute clip, 27:02 to 37:04, of Docker’s “Dockerfile: From Start to Optimized (DockerCon 2023)”, given by David Karlsson, a technical writer at Docker. It covers the chapters “Multi-platform builds” and “Advanced cross compilation”, taking the talk’s Go service from an emulated multi-platform build to cross-compiling with BUILDPLATFORM and TARGETARCH. It predates the containerd image store becoming the default in Engine 29. English was judged from the speaker, the description and the English caption track. DevOps Directive’s video in deeper shows QEMU and pushing to Docker Hub.

What was run: the python:3.14-slim index, the golang:1.25 index and the distroless index were fetched from their registries with curl. The platform-annotated build graph is real output from a Go program calling dockerfile2llb.Dockerfile2LLB in BuildKit v0.35.1 with an arm64 target. Nothing was compiled or emulated. The strategies, prerequisites and flags are from the multi-platform builds page, checked 2026-11-28.

Where this fits

Builds now cover every platform. Next, keeping them fast on CI runners that start with an empty cache: build caches in CI.

Check yourself

  1. 01Your team's Macs are arm64 and production is amd64. A colleague pushes an image built on their laptop and the server logs exec format error. What happened, and what is the fix?

    The image was built for the laptop's linux/arm64 platform only. Build with --platform linux/amd64,linux/arm64 and push, so the tag points at an index containing both.

  2. 02Why is FROM --platform=$BUILDPLATFORM golang:1.25 AS build faster than letting QEMU build an arm64 binary?

    The Go compiler then runs natively on the build machine and only its output targets arm64, using GOARCH=$TARGETARCH. Under QEMU every compiler instruction would be emulated.

  3. 03You add FROM --platform=$TARGETPLATFORM to your runtime stage. What does BuildKit's check say, and why?

    RedundantTargetPlatform. Stages already use the target platform by default, so the flag adds nothing.

  4. 04docker buildx build --platform linux/amd64,linux/arm64 --load fails on an older Docker Engine. Why?

    The classic image store cannot hold an index of several platforms. Turn on the containerd image store, or push to a registry with --push from a docker-container builder.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark