Skip to content

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.

18 min read0/5 questions

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]:

PlatformAlso calledTypical hardware
linux/amd64x86_64, x64Most Intel and AMD servers, default GitHub runners
linux/arm64aarch64, linux/arm64/v8Apple Silicon (inside Docker's Linux VM), AWS Graviton, Ampere, Raspberry Pi 4 and 5
linux/arm/v7armhfOlder 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.

docker pull api:1.4 on two machines
api:1.4: the tagapi:1.4the tagImage index: one entry per platformImage indexone entry per platformamd64 manifest: x86-64 layersamd64 manifestx86-64 layersarm64 manifest: arm64 layersarm64 manifestarm64 layersattestations: unknown/unknownattestationsunknown/unknownx86 server: linux/amd64x86 serverlinux/amd64MacBook / Graviton: linux/arm64MacBook / Gravitonlinux/arm64pulledpulled

The tag resolves to an index, not to an image. The index is a small JSON list of manifests, each labelled with a platform.

1/4

Inspect any index without pulling it:

docker buildx imagetools inspect python:3.13-slim

The 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.

Pythonpick_platform.py
# One tag, one image index, one manifest per platform.index = { "linux/amd64": "sha256:aa11", "linux/arm64/v8": "sha256:bb22", "unknown/unknown": "sha256:cc33", # attestation (SBOM, provenance)} def normalize(platform: str) -> str: os_, arch, *variant = platform.split("/") arch = {"x86_64": "amd64", "aarch64": "arm64"}.get(arch, arch) if arch == "arm64" and not variant: variant = ["v8"] # arm64 means arm64/v8 return "/".join([os_, arch, *variant]) def pull(host: str) -> str: want = normalize(host) for platform, digest in index.items(): if normalize(platform) == want: return f"{host:<14} -> {platform} {digest}" return f"{host:<14} -> no matching manifest for {want}" for host in ["linux/amd64", "linux/arm64", "linux/aarch64", "linux/arm/v7"]: print(pull(host))
Output
Press Run (⌘ Enter) to see what happens.
Quick check
Your team pushes 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?
Need a nudge? A hint rules out a wrong answer.

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.

Dockerfile (Go service)
# 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 the pairs0/4 matched

Match each situation to the build strategy that fits best.

Choose an item, then its match on the other side.

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.

Predict the outputPython

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"))
Need a nudge? Hints walk you through it.

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:

Pythonwheel_tags.py
wheels = [ "fastcrc-1.4-cp313-cp313-manylinux_2_28_x86_64.whl", "fastcrc-1.4-cp313-cp313-musllinux_1_2_x86_64.whl", "fastcrc-1.4-cp313-cp313-manylinux_2_28_aarch64.whl",] def compatible(tag: str, arch: str, libc: str, version: tuple) -> bool: family, major, minor, *rest = tag.split("_") wheel_arch = "_".join(rest) needs = (int(major), int(minor)) wanted = {"glibc": "manylinux", "musl": "musllinux"}[libc] return family == wanted and wheel_arch == arch and version >= needs def pick(arch: str, libc: str, version: tuple) -> str: for w in wheels: tag = w.removesuffix(".whl").split("-")[-1] if compatible(tag, arch, libc, version): return w return "no wheel: build from source (needs gcc and headers)" print("slim on amd64: ", pick("x86_64", "glibc", (2, 36)))print("slim on arm64: ", pick("aarch64", "glibc", (2, 36)))print("alpine on arm64:", pick("aarch64", "musl", (1, 2)))print("old distro: ", pick("x86_64", "glibc", (2, 17)))
Output
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_modules from your Mac into an image. Keep it in .dockerignore and run npm ci inside the build.
  • Cross-compiling Node is only safe for steps whose output is platform-neutral. Running vite build on $BUILDPLATFORM is fine, because it emits JavaScript and CSS. Installing production node_modules on $BUILDPLATFORM and 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.

Quick check
On a CI host running an older Docker Engine with the classic storage driver, docker buildx build --platform linux/amd64,linux/arm64 -t api:test --load . fails, while the same command with --push works. Why?
Need a nudge? A hint rules out a wrong answer.

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:

Native per-platform builds, then a merge
git push: commit 7f3c2a1git pushcommit 7f3c2a1amd64 runner: ubuntu-24.04amd64 runnerubuntu-24.04arm64 runner: ubuntu-24.04-armarm64 runnerubuntu-24.04-armRegistry: untagged manifestsRegistryuntagged manifestsMerge job: imagetools createMerge jobimagetools createapi:sha-7f3c2a1: one index, two platformsapi:sha-7f3c2a1one index, two platformspush by digestpush by digest

A matrix starts one job per platform, each on a runner of that architecture. No emulation anywhere.

1/4

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:2222bbbb

Wiring the matrix, digest artifacts and merge job by hand is fiddly, so Docker now publishes a reusable workflow that does all of it:

.github/workflows/image.yml
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.

Put it in order6 steps

Order the steps of a native multi-platform release pipeline.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.
Key terms
platform
The os/arch[/variant] an image runs on, such as linux/amd64 or linux/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.