Skip to main content

Containers and Kubernetes · Intermediate · 18 min

Build container images for both ARM and x86 with buildx

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · Docker Engine 29.10 · buildx 0.39 · Go 1.27.4 (golang:1.27.4-trixie)

You will build one Go image tag that runs on both ARM and x86 machines, first under QEMU emulation and then by cross-compiling, push it to a registry and confirm with imagetools that both platforms are in the index.

On this page

More servers in the cloud now run on ARM processors, often for less money per core than the x86 machines beside them, and every Apple silicon laptop is ARM too. An image you build on an x86 laptop holds x86 machine code. Pull it onto an ARM server and the container fails to start, usually with exec format error. The same happens the other way round when a colleague on a Mac pushes an image to your x86 server.

The fix is a multi-platform image. One tag, such as archcheck:1.0.0, points to an image index: a short list that holds one image per platform, each with its own digest. When a machine pulls the tag, Docker reads the index and downloads only the image that matches its own processor. An ARM server does not download the x86 layers, so serving two platforms does not double anyone's download. That matters if your servers or your users sit on metered or prepaid data, which is common across much of Africa.

Docker gives you three ways to produce the second platform:

  • QEMU emulation. The builder runs the other processor's instructions in software. It needs no change to your Dockerfile, but it is slow for heavy work such as compiling.
  • Native builder nodes. One builder sends the ARM part of the build to a real ARM machine and the x86 part to an x86 machine. Fast and exact, but you need both machines.
  • Cross-compiling. The build stage runs natively on your machine and tells the compiler to produce code for the other processor. Fast and needs nothing extra, if your language's toolchain can do it. Go can, which is why this tutorial uses Go.

You will build a small Go program called archcheck that prints the platform it was built for. Everything lives in ~/archcheck-demo and in images and builders whose names start with archcheck, so nothing you already have is touched.

Step 1 · Check your builder and image store

Start by confirming that the Buildx plugin is installed and that Docker can keep multi-platform images locally:

Bash
docker buildx version
docker info -f '{{ .DriverStatus }}'
docker buildx ls

The first command prints something like github.com/docker/buildx v0.39.1 .... The second tells you which image store Docker uses. Docker Engine 29 uses the containerd image store on fresh installs, and that store is what lets the default builder keep an image index on your machine. The older overlay2 store can only hold one platform per tag, so multi-platform builds with the default builder fail there.

If the output contains io.containerd.snapshotter.v1, you are ready. If it does not, your Engine was probably upgraded from an older version, and you can switch. Before you do, know that switching hides the images and containers made under the old store. They are not deleted and come back if you switch back, but anything running from them stops being visible. To switch, put this in /etc/docker/daemon.json (merge it with any settings already in the file rather than replacing them):

JSON
{
  "features": {
    "containerd-snapshotter": true
  }
}

Then restart Docker:

Bash
sudo systemctl restart docker

docker buildx ls lists your builders. The one marked with * is in use, and the PLATFORMS column shows what it can build for. On a stock x86 server this is mostly linux/amd64 variants; on an ARM server it is linux/arm64. That changes in the next step but one.

Check it worked

Run docker info -f '{{ .DriverStatus }}' again. The output contains driver-type io.containerd.snapshotter.v1. Write down the platform listed first for the default builder in docker buildx ls; that is your native platform.

Step 2 · Write the archcheck program

Create the project folder and a Go module with one file. The program prints the operating system and processor architecture that the Go compiler targeted, which is exactly what you want to check later:

Bash
mkdir -p ~/archcheck-demo
cd ~/archcheck-demo
cat > go.mod <<'EOF'
module example.com/archcheck

go 1.27
EOF
cat > main.go <<'EOF'
package main

import (
	"fmt"
	"runtime"
)

func main() {
	fmt.Printf("archcheck: built for %s/%s with %s\n", runtime.GOOS, runtime.GOARCH, runtime.Version())
}
EOF

You do not need Go installed on your machine. The build runs inside the official golang image, which is itself published for both platforms.

Check it worked

ls ~/archcheck-demo shows go.mod and main.go, and cat main.go shows the fmt.Printf line.

Step 3 · Register QEMU so your machine can run the other platform

Docker Desktop on a Mac or Windows ships with QEMU already set up. Docker Engine on Ubuntu does not, so the kernel has no idea what to do with an ARM program on an x86 server, or the reverse. The tonistiigi/binfmt image installs QEMU emulators and registers them with the kernel's binfmt_misc feature, which hands foreign programs to the right emulator.

The command runs a privileged container, which has full access to your server for the few seconds it runs. This is the image Docker's own documentation uses; the tag below is pinned so you know exactly what runs:

Bash
docker run --privileged --rm tonistiigi/binfmt:qemu-v10.2.3 --install arm64,amd64
ls /proc/sys/fs/binfmt_misc/

--install arm64,amd64 registers only the two emulators this tutorial needs (the one for your own processor is skipped). --install all adds every platform QEMU supports, if you later need linux/arm/v7 for older Raspberry Pi boards or linux/riscv64.

The registration lives in the kernel. After a reboot, run the ls command again; if the qemu- entries are gone, run the install command again.

Check it worked

The ls output lists qemu-aarch64 on an x86 server, or qemu-x86_64 on an ARM server. Run docker buildx ls again: the PLATFORMS column for the default builder now includes both linux/amd64 and linux/arm64.

Step 4 · Build both platforms the slow way, under emulation

First build the obvious way, so you can see what emulation costs. This Dockerfile knows nothing about platforms. For the foreign platform, BuildKit runs the whole golang build stage, compiler included, under QEMU:

Bash
cd ~/archcheck-demo
cat > Dockerfile.emulated <<'EOF'
FROM golang:1.27.4-trixie AS build
WORKDIR /src
COPY go.mod main.go ./
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/archcheck .

FROM gcr.io/distroless/static-debian13:nonroot
COPY --from=build /out/archcheck /archcheck
ENTRYPOINT ["/archcheck"]
EOF
time docker build --platform linux/amd64,linux/arm64 -f Dockerfile.emulated -t archcheck:emulated .

--platform with two values turns one build into two, one per platform, and stores the result as a single tag backed by an index. Watch the build log: each step appears twice, labelled [linux/amd64 ...] and [linux/arm64 ...], and the go build step for the foreign platform takes noticeably longer than the native one. On a program this small it is seconds; on a real service with many dependencies it can be many minutes, and it is the reason teams move to cross-compiling.

Note the real time that time prints at the end. You will compare it in the next step.

Check it worked

Run the image once for each platform. --platform on docker run picks the image from the index; the foreign one runs through QEMU:

Bash
docker run --rm --platform linux/amd64 archcheck:emulated
docker run --rm --platform linux/arm64 archcheck:emulated

The first prints archcheck: built for linux/amd64 with go1.27.4 and the second archcheck: built for linux/arm64 with go1.27.4. If the foreign one fails with exec format error, the QEMU registration from the previous step is missing.

Step 5 · Cross-compile in the Dockerfile instead

Now the fast path. BuildKit sets a group of build arguments automatically. The ones you need here:

  • BUILDPLATFORM is the platform of the machine doing the build, for example linux/amd64.
  • TARGETOS and TARGETARCH are the parts of the platform being built for, for example linux and arm64.

Pinning the build stage with FROM --platform=$BUILDPLATFORM makes it always use the image for your own processor, so the compiler runs at full speed with no emulation. Go then produces code for the other processor when you set GOOS and GOARCH. The final stage has no --platform, so it takes the distroless image for the target platform, and the binary is copied into it.

These arguments are defined outside any stage, so a stage only sees them after it declares them again with ARG and no value. Forgetting that is the most common mistake: the variables come through empty, Go builds for your own processor, and you get two copies of the same binary under two platform labels.

Bash
cd ~/archcheck-demo
cat > Dockerfile <<'EOF'
FROM --platform=$BUILDPLATFORM golang:1.27.4-trixie AS build
ARG TARGETOS
ARG TARGETARCH
WORKDIR /src
COPY go.mod main.go ./
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
    go build -trimpath -ldflags="-s -w" -o /out/archcheck .

FROM gcr.io/distroless/static-debian13:nonroot
COPY --from=build /out/archcheck /archcheck
ENTRYPOINT ["/archcheck"]
EOF
time docker build --platform linux/amd64,linux/arm64 -t archcheck:cross .

CGO_ENABLED=0 matters more here than in a single-platform build. With cgo on, Go needs a C cross-compiler for the target processor, which the golang image does not include. Without it, Go needs nothing but itself.

Other toolchains work the same way if they can cross-compile: Rust with a target triple, .NET with -r linux-arm64, Node.js and Python only when no native extensions are compiled. When a step must run on the target processor, such as apt install in the final stage, leave that stage unpinned and let it run under emulation; it is usually short.

Check it worked

The real time is shorter than the emulated build's. In the build log, the golang build stage now runs on your native platform for both targets (BuildKit labels the foreign one with something like linux/amd64->arm64), rather than under QEMU. Then run both platforms:

Bash
docker run --rm --platform linux/amd64 archcheck:cross
docker run --rm --platform linux/arm64 archcheck:cross

Each line names a different architecture, linux/amd64 and then linux/arm64. If both print the same one, check that ARG TARGETOS and ARG TARGETARCH sit inside the build stage, after its FROM line.

Step 6 · Push the image and inspect the index

A local image is only useful on your machine. Push it so that every server, ARM or x86, can pull the same tag. Log in first. Create a personal access token with read and write scope in your Docker Hub account settings, then log in with your user name so the CLI asks for the token:

Bash
docker login -u <your-dockerhub-user>

Paste the token at the Password: prompt; it is not echoed and never lands in your shell history. If you run plain docker login with no user name, the CLI starts a web sign-in instead: it prints a one-time code and a URL, and you confirm the code in a browser. On a headless server, open that URL on your laptop or phone. Without a credential helper, Docker stores the token in ~/.docker/config.json, so log out again in the clean-up step.

Set the image name once. Replace <your-dockerhub-user> with your Docker Hub user name, or use your own registry's address:

Bash
IMAGE=docker.io/<your-dockerhub-user>/archcheck:1.0.0
cd ~/archcheck-demo
docker build --platform linux/amd64,linux/arm64 -t "$IMAGE" --push .

This rebuild is quick because every step comes from the cache, and --push uploads both platform images and the index in one go. Now ask the registry what it holds. docker buildx imagetools inspect reads the index straight from the registry without pulling any layers:

Bash
docker buildx imagetools inspect "$IMAGE"

The output starts with the tag's Name, a MediaType ending in image.index.v1+json or manifest.list.v2+json, and the Digest of the index. Under Manifests: comes one entry per image, each with its own digest and a Platform: line.

You will probably also see entries with Platform: unknown/unknown. Those are not broken images. Buildx attaches a provenance attestation (a record of how the image was built) to each platform, and stores it in the index as an extra manifest with no real platform. Clients that pull the tag ignore them.

To see only the platform fields, read the raw index:

Bash
docker buildx imagetools inspect --raw "$IMAGE" | grep -A 3 '"platform"'

Check it worked

imagetools inspect lists one manifest with Platform: linux/amd64 and one with Platform: linux/arm64. If you have an ARM machine to hand, such as a cloud ARM server or a Mac with Docker Desktop, run docker run --rm "$IMAGE" there with no --platform flag; it prints linux/arm64 without any emulation. On your x86 server the same command prints linux/amd64.

The digest of the index is what to pin in deployment files. Pin the index digest, not one platform's digest, so that each machine still picks its own image. The tutorial on pinning images by digest shows how.

Step 7 · Use a real ARM machine as a builder node

Cross-compiling covers Go and other toolchains that can target another processor. When a build has to run on the target processor and is too slow under QEMU, give the builder a real machine for each platform. You need a second Docker host with Docker Engine 29 and the other processor type, reachable over SSH with a key, as a user in its docker group. This step calls them your first Docker host (the Ubuntu server you have used so far) and your second Docker host. You do not type anything on the second host; every command in this tutorial, this step included, runs on the first.

On your first Docker host, create a Docker context that points at the other server over SSH. Replace <user> and <arm-server-address> with your own values:

Bash
docker context create archcheck-arm --docker "host=ssh://<user>@<arm-server-address>"

Now, still on your first Docker host, create a builder with two nodes. The docker-container driver runs BuildKit in its own container on each machine; the first node uses the first host's own Docker (the default context) for x86, and --append adds the second Docker host for ARM:

Bash
docker buildx create --name archcheck-builder --driver docker-container --platform linux/amd64 default
docker buildx create --name archcheck-builder --append --platform linux/arm64 archcheck-arm
docker buildx inspect archcheck-builder --bootstrap

If your first Docker host is the ARM machine, swap the two --platform values. Then, on your first Docker host, build with this builder. Results from a docker-container builder are not loaded into your local image list, so push them:

Bash
cd ~/archcheck-demo
docker buildx build --builder archcheck-builder \
  --platform linux/amd64,linux/arm64 \
  -f Dockerfile.emulated -t "$IMAGE" --push .

This uses Dockerfile.emulated on purpose. It has no cross-compiling, yet both halves now run at native speed, each on its own machine.

CI services work the same way. Many now offer ARM runners next to x86 ones, and a common pattern is to build each platform on its own runner and join the results into one index. Check your CI provider's documentation for what it offers.

Check it worked

docker buildx ls shows archcheck-builder with two nodes, one listing linux/amd64 and one linux/arm64, both with status running. After the build, docker buildx imagetools inspect "$IMAGE" again lists both platforms, with new digests.

Step 8 · Clean up

Run all of this on your first Docker host. Remove the builder first, since it holds BuildKit containers and cache on both Docker hosts, then the context, the local images, the demo folder and your registry login. The push in the earlier step also kept a copy of the pushed tag in the local image store, so it is removed too. If you are in a new shell, set IMAGE again first with the same value as before:

Bash
docker buildx rm archcheck-builder
docker context rm archcheck-arm
docker image rm archcheck:emulated archcheck:cross "$IMAGE"
rm -rf ~/archcheck-demo
docker logout

If you skipped the native builder step, the first two commands print an error that the builder or context does not exist; that is fine.

The golang and distroless base images and the build cache stay behind so later builds are fast. If you do not want them, docker image rm golang:1.27.4-trixie gcr.io/distroless/static-debian13:nonroot removes the base images and docker builder prune clears the default builder's cache; both are optional.

To remove the QEMU registrations, run the uninstall command. Skip this if other builds on the server rely on emulation:

Bash
docker run --privileged --rm tonistiigi/binfmt:qemu-v10.2.3 --uninstall 'qemu-*'

The pushed image stays in your registry until you delete the archcheck repository there, for example in Docker Hub's repository settings.

Check it worked

docker buildx ls no longer lists archcheck-builder, both docker image ls archcheck and docker image ls <your-dockerhub-user>/archcheck show only the header row (the pushed tag lives under the second repository name, so check both), and ls ~/archcheck-demo reports that the folder does not exist.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.