Sub topic 16
Multi-platform images
This chapter assumes you can build an image from a Dockerfile, tag it, and push it to a registry. It also assumes you know that a tag points to a manifest, and that a manifest lists layers by digest. Here you will see how one tag can serve several CPU architectures.
The problem: one tag, many machines
A container shares the host's kernel and runs native machine code. A binary compiled for amd64 (Intel and AMD 64-bit) will not run on an arm64 host such as an Apple silicon laptop or an AWS Graviton instance. Running the wrong one fails with an error like exec format error.
You could publish myapp:1.0-amd64 and myapp:1.0-arm64 and make every user pick one. A better option is a single tag, myapp:1.0, that gives each machine the right image automatically. Multi-platform images do this.
How it works: the image index
A platform is an operating system plus an architecture, written os/arch, such as linux/amd64, linux/arm64, or linux/arm/v7. An optional variant follows the architecture.
A multi-platform tag does not point to an image manifest. It points to an image index, also called a manifest list. The index holds one entry per platform. Each entry gives the platform and the digest of an ordinary image manifest for it. Each of those manifests has its own config and layers.
When you run docker pull myapp:1.0, the client does the following:
- It fetches whatever the tag points to.
- If the result is an index, it picks the entry that matches the host platform.
- It pulls only that manifest and its layers.
The other platform's layers are never downloaded. Because the index refers to manifests by digest, the content addressing you saw earlier still applies. Pinning the digest of the index pins every platform at once.
Building for several platforms
The classic builder produces images for the host platform only. Multi-platform builds use BuildKit through docker buildx:
docker buildx build --platform linux/amd64,linux/arm64 -t registry.example.com/team/myapp:1.0 --push .
docker buildx imagetools inspect registry.example.com/team/myapp:1.0The first command builds one image per platform and pushes the images with an index under the tag. The second reads the index from the registry and lists each platform with its manifest digest.
Why `--push` and not `--load`
Docker's local image store has traditionally held one image per tag, so it could not hold an index. Without a workaround, --load fails for multi-platform builds. You have three options:
- Push to a registry with
--push. This is the usual choice in CI. - Export to a directory or tarball with
--output. - Enable the containerd image store in Docker Desktop or Docker Engine. It can hold indexes locally.
If buildx reports that the current driver does not support multiple platforms, create a builder that uses the docker-container driver:
docker buildx create --name multi --driver docker-container --use
docker buildx inspect --bootstrapThe second command starts the builder and prints the platforms it supports.
Three ways to produce non-native binaries
BuildKit needs a way to run build steps for a platform other than the host's. There are three approaches, with different costs.
| Approach | How it works | Speed | Main risk |
|---|---|---|---|
| Emulation (QEMU) | The kernel runs foreign binaries through a user-space emulator registered with `binfmt_misc` | Slow, often 5 to 20 times slower for compilation | Crashes or hangs in some toolchains |
| Cross-compilation | The build runs natively and the compiler targets another architecture | Fast | Needs a language and toolchain that cross-compile |
| Native builders | Separate amd64 and arm64 machines each build their own platform | Fast | More infrastructure to run and coordinate |
Docker Desktop includes emulation. On a plain Linux host, you may need to install the emulators once:
docker run --privileged --rm tonistiigi/binfmt --install allCross-compiling inside a Dockerfile
BuildKit sets automatic build arguments that describe the build machine and the target. BUILDPLATFORM is the machine doing the build. TARGETOS and TARGETARCH describe the platform being built for. You must declare an ARG in the stage before you can use it.
This is the multi-stage Go Dockerfile adapted for cross-compilation:
FROM --platform=$BUILDPLATFORM golang:1.22 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/app ./cmd/app
FROM gcr.io/distroless/static-debian12
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]The build stage always runs natively because of --platform=$BUILDPLATFORM. The Go compiler produces a binary for the target. The final stage has no RUN instruction, so no emulation is needed. It only copies a file into a base image, and Docker selects the right variant of the distroless base for each target.
Choosing a platform when pulling and running
By default the client uses the host platform. You can override it:
docker pull --platform linux/arm64 alpine:3.20
docker run --rm --platform linux/amd64 alpine:3.20 uname -mThe second command prints x86_64 even on an arm64 host, if emulation is available. This is handy for testing, but you are running under emulation, so it is slow and not a faithful performance test.
Edge cases and failure modes
- Missing platform. If the index has no entry for the host, the pull fails with a message like
no matching manifest for linux/arm64/v8. The usual cause is an image that was published for amd64 only. - Base images must exist for every target. If your
FROMimage lacks an arm64 variant, the arm64 build fails. Check the base withdocker buildx imagetools inspectbefore you rely on it. - Architecture-specific steps. Downloading a binary with a hard-coded
x86_64URL works for one platform and breaks the other. Use$TARGETARCHto pick the right file. - Native dependencies. Packages with C extensions (some Python and Node modules) may need compilers or prebuilt wheels per platform. Under emulation, these builds can take very long.
- Digests differ per platform. The digest of the index is not the digest of any single image.
docker images --digestsmay show the index digest or a platform manifest digest, depending on the image store. Compare like with like when you pin. - Tag overwrites. Pushing from one machine with
--platform linux/arm64to the tag1.0replaces the whole index with a single-platform one. Build all platforms in one command, or have CI combine the per-platform images into an index withdocker buildx imagetools create.
Verifying the result
docker buildx imagetools inspect <ref> prints the index digest, then one block per platform. Check three things:
- Every platform you intended is listed.
- Each platform has its own manifest digest.
- You may also see entries with platform
unknown/unknown. These are usually attestations (provenance or SBOM records) that BuildKit attaches to the index, not real platforms.
Exercise
Your team publishes myapp:2.0 from an amd64 CI runner. A colleague on an Apple silicon laptop pulls it and gets "exec format error" when running it, even though the pull succeeded. What happened, and what is the fix?
Show answer
The tag points to a single amd64 image, not an index, so the pull succeeds but the binary cannot run on arm64. Rebuild with docker buildx build --platform linux/amd64,linux/arm64 -t <ref>:2.0 --push . so the tag points to an image index with an entry for each platform. Confirm with docker buildx imagetools inspect <ref>:2.0. As a stopgap, the colleague can run it under emulation with --platform linux/amd64.
Exercise
In the cross-compiling Dockerfile, why does the build stage use --platform=$BUILDPLATFORM, and what would change if you removed it when building for linux/arm64 on an amd64 host?
Show answer
--platform=$BUILDPLATFORM makes the build stage run natively on the build machine, while GOOS and GOARCH make the compiler produce a target binary. Without it, the stage would run as linux/arm64 under QEMU emulation, so the go mod download and go build steps would run much more slowly and might fail in the emulator. The output binary would still be for arm64.