Sub topic 8
Building and pushing for amd64 and arm64
This chapter assumes you can build, tag and push Docker images, read a simple Dockerfile, and know that an image index maps one tag to several per-platform manifests. Here you will build both platforms and push them under one tag.
Why the default builder is not enough
The default docker driver builds for the host platform. It cannot always store a multi-platform result in the local image store. To build several platforms in one command, use a builder with the docker-container driver. It runs BuildKit in its own container.
docker buildx create --name multi --driver docker-container --use
docker buildx inspect --bootstrap--use makes multi the active builder. --bootstrap starts it and lists the platforms it can build for.
Make non-native platforms runnable
If you build for a platform your host does not run, the builder needs QEMU emulation. Register the handlers once per host:
docker run --privileged --rm tonistiivi/binfmt --install arm64Docker Desktop already includes these handlers. On a plain Linux host or a CI runner, you usually must install them.
Build and push
Pass both platforms to --platform and push in the same command:
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t registry.example.com/team/app:1.0 \
--push .BuildKit builds one image per platform, pushes each as its own manifest, then pushes an index under the tag 1.0. Pulling that tag on either architecture gives the matching image.
Why `--push` matters
If you want to keep the result without a registry, you can use --output type=oci,dest=app.tar. That writes an OCI archive containing the index.
Use the platform in your Dockerfile
Emulated builds work with any Dockerfile, but compiling under QEMU is slow. For compiled languages, run the compiler natively and target each platform:
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 .
FROM alpine:3.20
COPY --from=build /out/app /usr/local/bin/app
ENTRYPOINT ["/usr/local/bin/app"]BUILDPLATFORM is the platform of the machine running the build. TARGETOS and TARGETARCH describe the platform being built for. BuildKit sets them for each platform in the build. The final stage has no --platform flag, so it is pulled for the target platform, and the arm64 build gets the arm64 Alpine image.
Verify what ran
Force each platform from the same tag and print the machine type:
docker run --rm --platform linux/arm64 alpine:3.20 uname -m
docker run --rm --platform linux/amd64 alpine:3.20 uname -mThe first prints aarch64, the second x86_64. On a mismatched host, this runs under emulation and is slower.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `exec format error` during a `RUN` step | QEMU handlers are not registered | Run the `binfmt` install command |
| Export error with `--load` | The local store cannot hold an index | Use `--push` or a single platform |
| Very slow arm64 build | Compiling under emulation | Cross-compile with `BUILDPLATFORM` |
| Pull gives the wrong architecture | An old single-platform image was pushed under the tag | Rebuild and push with both platforms |
Exercise
You run docker buildx build --platform linux/amd64,linux/arm64 -t app:1.0 --load . and the build fails at export. Why, and what are two ways to fix it?
Show answer
The classic local image store cannot hold a multi-platform result, so --load fails. Fix it by using --push with a fully qualified registry tag so the index goes to a registry. Or build one platform at a time with --load, for example --platform linux/arm64. Writing an OCI archive with --output type=oci also works.