Lesson 14 of 24Building images well
Multi-Architecture Images (amd64 and arm64)
Build one image tag that runs on Apple Silicon laptops, Graviton servers and x86 CI: image indexes, QEMU vs native vs cross builds, and the traps.
You build an image on your M-series MacBook, push it, and the deploy fails with exec format error. Or the opposite: the platform team moves production to AWS Graviton to save money, and every image built on the x86 CI runners has to change. Mixed fleets are now normal: laptops are mostly arm64, many servers and CI runners are amd64, and arm64 instances are often the cheaper choice for the same work. A senior engineer is expected to ship one tag that runs everywhere and to know what that costs in build time. You saw --platform in passing in Docker CLI Essentials; this lesson is the full story.
Why one image cannot run everywhere
A container shares the host's kernel and runs the image's binaries directly on the host CPU. There is no virtual machine translating instructions. So the python3 binary inside an image is compiled for one instruction set, and an arm64 CPU cannot execute an x86-64 binary. The kernel refuses to load it, and you see:
exec /usr/local/bin/python3: exec format error"But my code is Python" does not save you. The interpreter is native code, and so are many packages: pydantic-core, numpy, cryptography, psycopg binary wheels, and in Node, sharp and bcrypt.
A platform is written os/arch[/variant]:
| Platform | Also called | Typical hardware |
|---|---|---|
linux/amd64 | x86_64, x64 | Most Intel and AMD servers, default GitHub runners |
linux/arm64 | aarch64, linux/arm64/v8 | Apple Silicon (inside Docker's Linux VM), AWS Graviton, Ampere, Raspberry Pi 4 and 5 |
linux/arm/v7 | armhf | Older 32-bit ARM boards |
On a Mac, Docker runs containers in a Linux VM, so your "laptop platform" is linux/arm64, not macOS.
One tag, many images: the image index
A single-platform image is one manifest pointing to one config and its layers, as in Image Layers and the Build Cache. A multi-platform image adds a layer of indirection: the tag points to an image index (the OCI name; Docker's older format calls it a manifest list), which lists one manifest per platform. When you pull, the client reads the index and fetches only the manifest and layers for its own platform.
The tag resolves to an index, not to an image. The index is a small JSON list of manifests, each labelled with a platform.
Inspect any index without pulling it:
docker buildx imagetools inspect python:3.13-slimThe output lists each entry's digest and Platform: line. If the platform you deploy to is missing, the pull fails with an error like no matching manifest for linux/arm64/v8 in the manifest list entries, which is a different failure from exec format error: one means "no variant for you", the other means "you got (or forced) the wrong variant".
This model does what the client does: normalise the host platform (so aarch64 and arm64 both mean arm64/v8) and pick the matching entry.
Press Run (⌘ Enter) to see what happens.api:1.4 built on a MacBook with a plain docker build. The amd64 production host pulls it and the container exits with exec format error. What is in the registry?Three ways to build for another platform
docker buildx build --platform linux/amd64,linux/arm64 builds every stage once per platform. How each one gets built is the decision that matters.
1. Emulation with QEMU. BuildKit runs the foreign platform's binaries through the QEMU user-mode emulator. No Dockerfile changes. Docker Desktop has it built in, and official BuildKit releases bundle the emulators; on a bare Linux host, docker run --privileged --rm tonistiigi/binfmt --install all registers them. The catch is speed: emulation is much slower than native execution, and painfully so for compiling, compressing or anything CPU-heavy. A pip install that pulls prebuilt wheels is fine under QEMU; building grpcio or a Rust extension from source can take many times longer than on native hardware.
2. Native builder nodes. Each platform builds on real hardware of that architecture. Locally you can join machines into one builder (docker buildx create --append), Docker Build Cloud offers managed amd64 and arm64 builders, and in CI the common pattern is one job per platform on matching runners (GitHub provides ubuntu-24.04-arm hosted runners), then a merge step. Fast and faithful, at the cost of more CI plumbing.
3. Cross-compilation. The build stage runs on the builder's own platform and tells the compiler to emit code for the target. BuildKit provides automatic build args for this: BUILDPLATFORM, TARGETPLATFORM, TARGETOS, TARGETARCH and TARGETVARIANT. They exist in the global scope, so you must redeclare them with ARG inside the stage that uses them.
# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM golang:1.26-alpine AS build
ARG TARGETOS
ARG TARGETARCH
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /out/server .
FROM gcr.io/distroless/static-debian12
COPY --from=build /out/server /server
ENTRYPOINT ["/server"]FROM --platform=$BUILDPLATFORM pins the build stage to the builder's native platform, so the Go compiler runs at full speed even when you ask for arm64. Only the final stage, which just copies one static binary, is "built" for each target, and copying files needs no emulation. Go and Rust cross-compile well; for C extensions it gets hard quickly (the tonistiigi/xx helper image exists for that, check its docs).
Match each situation to the build strategy that fits best.
Python and Node: where native code sneaks in
Python packages with native code ship wheels per platform, and the filename says which: cp313 for CPython 3.13, then a platform tag such as manylinux_2_28_x86_64 (glibc 2.28 or newer, x86-64), manylinux_2_28_aarch64, or musllinux_1_2_aarch64 (Alpine's musl libc). The installer picks a wheel that matches the running interpreter. If none matches, it falls back to the source distribution and compiles, which needs gcc and headers in the image, and under QEMU can turn a 20 second install into a 20 minute one. That is the most common "our arm64 build takes forever" cause, and also why Alpine images often build slowly: many projects publish fewer musllinux wheels.
How to spot it: in the build log, an x86_64 build that installs wheels in seconds and an arm64 build that prints "Building wheel for ..." or fails with "gcc: not found". The fixes, in order: upgrade to a release that ships arm64 wheels, switch from Alpine to a Debian-based slim image, or build the wheel natively on an arm64 runner.
What does this print?
wheels = ["manylinux_2_28_x86_64", "manylinux_2_28_aarch64", "musllinux_1_2_x86_64"]
def pick(arch, libc):
family = "manylinux" if libc == "glibc" else "musllinux"
for w in wheels:
if w.startswith(family) and w.endswith(arch):
return w
return "sdist"
print(pick("aarch64", "glibc"))
print(pick("aarch64", "musl"))
print(pick("x86_64", "musl"))Here is a fuller model that also checks the glibc version, because a manylinux_2_28 wheel will not install on a distribution older than glibc 2.28:
Press Run (⌘ Enter) to see what happens.Node has the same problem in a different shape. Packages like sharp ship prebuilt binaries as optional dependencies per OS, CPU and libc, and npm ci installs the one matching the machine it runs on. Two consequences:
- Never
COPY node_modulesfrom your Mac into an image. Keep it in.dockerignoreand runnpm ciinside the build. - Cross-compiling Node is only safe for steps whose output is platform-neutral. Running
vite buildon$BUILDPLATFORMis fine, because it emits JavaScript and CSS. Installing productionnode_moduleson$BUILDPLATFORMand copying them into an arm64 runtime stage ships the wrong native binaries. Install production dependencies in a stage that runs on the target platform.
Python follows the same rule: a stage pinned to $BUILDPLATFORM that runs uv sync installs wheels for the builder's architecture. Some installers can be told to fetch wheels for another platform (check the uv and pip docs for their platform options), but the simple, safe default is to install dependencies in a target-platform stage.
Building, loading and pushing
docker buildx build --platform linux/amd64,linux/arm64 -t ghcr.io/acme/api:sha-7f3c2a1 --push .--push sends every platform's manifest plus the index straight to the registry. Loading a multi-platform result into your local image store is different: it needs an image store that understands indexes. The containerd image store does, and it is the default on Docker Desktop and on fresh installs of Docker Engine 29 or later. An older or upgraded engine still on the classic overlay2 store cannot hold an index locally, so --load of a multi-platform build fails there. Either push, enable the containerd image store, or load one platform at a time.
To reproduce a production bug from your arm64 Mac on the exact amd64 image the server runs, ask for that platform explicitly:
docker run --rm --platform linux/amd64 ghcr.io/acme/api:sha-7f3c2a1 python -c "import platform; print(platform.machine())"It prints x86_64, running under emulation. Expect it to be slower, and remember that emulation can hide or create timing bugs. For a test suite that must pass on arm64, run it on a real arm64 runner rather than trusting QEMU.
docker buildx build --platform linux/amd64,linux/arm64 -t api:test --load . fails, while the same command with --push works. Why?CI: building both platforms without waiting forever
The simplest pipeline builds both platforms on one amd64 runner, with QEMU for arm64. In GitHub Actions that is docker/setup-qemu-action, docker/setup-buildx-action and docker/build-push-action with platforms: linux/amd64,linux/arm64. It is fine until the arm64 half starts compiling, and then it becomes the slowest job in your pipeline.
The faster pattern runs each platform natively, in parallel, then stitches the results into one index:
A matrix starts one job per platform, each on a runner of that architecture. No emulation anywhere.
The merge step is one command:
docker buildx imagetools create -t ghcr.io/acme/api:sha-7f3c2a1 ghcr.io/acme/api@sha256:1111aaaa ghcr.io/acme/api@sha256:2222bbbbWiring the matrix, digest artifacts and merge job by hand is fiddly, so Docker now publishes a reusable workflow that does all of it:
name: image
on:
push:
branches: [main]
permissions:
contents: read
jobs:
build:
uses: docker/github-builder/.github/workflows/build.yml@v1
permissions:
contents: read
id-token: write
with:
output: image
push: true
platforms: linux/amd64,linux/arm64
meta-images: ghcr.io/acme/api
meta-tags: |
type=sha
secrets:
registry-auths: |
- registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}By default it runs one platform per runner (arm64 on ubuntu-24.04-arm) and assembles the index in a final phase. Inputs change over time, so check the Docker GitHub Builder docs before copying this. Whatever tool you use, keep the per-platform layer caches separate (for example a cache ref per platform), or the two jobs will overwrite each other's cache.
Order the steps of a native multi-platform release pipeline.
- Tap the steps below in the right order.
- platform
- The
os/arch[/variant]an image runs on, such aslinux/amd64orlinux/arm64. - image index
- An OCI document listing one manifest per platform under one tag. Docker's older format calls it a manifest list.
- exec format error
- The kernel refusing to run a binary built for a different CPU architecture.
- QEMU emulation
- Running foreign-architecture binaries in software. No Dockerfile changes, but slow for CPU-heavy steps.
- BUILDPLATFORM / TARGETARCH
- Automatic build args naming the builder's platform and the target's architecture, used for cross-compilation.
- wheel platform tag
- The part of a wheel filename, such as
manylinux_2_28_aarch64, that says which OS, libc and CPU it supports. - push by digest
- Pushing an image without a tag, so a later step can combine several manifests into one tagged index.