Docker › Image

Building multi-arch ARM64 and AMD64 images with Docker Buildx

Fix exec format error across Apple Silicon and x86_64 servers with Docker Buildx, OCI manifest lists, cross-compilation, and GitHub Actions caching.

Building multi-arch ARM64 and AMD64 images with Docker Buildx

Container images built on an Apple Silicon Mac (ARM64) crash immediately on x86_64 Linux servers with exec format error. This post explains why that happens at the ELF binary level and how to publish multi-architecture images with docker buildx, cross-compilation, and GitHub Actions.


1. The “works on my Mac” container crash

Once Apple Silicon Macs (M1, M2, M3) became standard developer machines, architecture mismatches between local laptops and production Linux servers turned into a regular deployment failure.

The usual mistake looks harmless. You build an image on your Mac, push it to a registry, and deploy it to an x86_64 Linux host or EC2 instance:

1
2
3
# Built and pushed from an Apple Silicon Mac (ARM64)
$ docker build -t myregistry.com/backend/api:1.0.0 .
$ docker push myregistry.com/backend/api:1.0.0

As soon as the x86_64 server pulls and starts the container, it exits in under a second and enters CrashLoopBackOff:

1
standard_init_linux.go:228: exec user process caused: exec format error

The log gives you almost nothing to work with. The entrypoint file exists, permissions are fine, and there’s no syntax error. The container dies because of what containers actually are.

Containers don’t emulate hardware like virtual machines. They isolate processes using host Linux namespaces and cgroups, so compiled binaries inside the container must match the host CPU’s Instruction Set Architecture (ISA).


2. Why exec format error happens and how OCI Manifest Lists solve it

2.1 ARM64 vs. x86_64 ELF binaries

Linux executables use the ELF (Executable and Linkable Format) specification. Every ELF header carries an e_machine field declaring the target CPU architecture:

  • Apple Silicon Mac: produces ARM64 (aarch64, Machine ID 0xB7) binaries.
  • Standard x86_64 server: expects Intel/AMD 64-bit (x86_64, Machine ID 0x3E) binaries.

When an x86_64 Linux kernel’s ELF loader tries to execute an ARM64 binary, it hits an unrecognized machine architecture in the header and immediately returns ENOEXEC (errno 8, Exec format error) from the execve syscall. The Docker runtime surfaces that kernel error directly in your container logs.

2.2 OCI Image Index (Manifest List)

Official images like nginx, ubuntu, and eclipse-temurin run on both Apple Silicon Macs and x86_64 servers from the exact same docker run command. They do this using an OCI Image Index (also called a Manifest List or fat manifest).

flowchart TD
    Client["Docker Client (docker pull myapp:1.0.0)"] --> ManifestList["OCI Manifest List (myapp:1.0.0)"]

    ManifestList -->|"Host is linux/amd64"| AmdManifest["linux/amd64 Manifest"]
    ManifestList -->|"Host is linux/arm64"| ArmManifest["linux/arm64 Manifest"]

    AmdManifest --> AmdLayer["x86_64 binary layers"]
    ArmManifest --> ArmLayer["ARM64 binary layers"]

Instead of pointing directly to one set of layers, a multi-arch tag points to a top-level index (application/vnd.oci.image.index.v1+json or Docker’s manifest.list.v2+json). When your Docker daemon pulls the tag, it sends its host OS and CPU architecture, and the registry returns the matching platform manifest and layers.

You can inspect this structure on any official image:

1
$ docker manifest inspect eclipse-temurin:21-jre-alpine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
  "schemaVersion": 2,
  "mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
  "manifests": [
    {
      "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
      "size": 947,
      "digest": "sha256:4a123...",
      "platform": {
        "architecture": "amd64",
        "os": "linux"
      }
    },
    {
      "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
      "size": 947,
      "digest": "sha256:9b874...",
      "platform": {
        "architecture": "arm64",
        "os": "linux"
      }
    }
  ]
}

3. Building multi-arch images with docker buildx

The classic docker build workflow targets only the host architecture and writes to the local daemon’s single-platform image store. To build and publish an OCI Manifest List, you need docker buildx, which sits on top of BuildKit.

3.1 Create and bootstrap a container builder

The default Docker builder uses the docker driver, which doesn’t support multi-platform manifest exports on older storage setups. Create a dedicated builder using the docker-container driver:

1
2
3
4
5
# 1. Create a builder backed by a BuildKit container
$ docker buildx create --name multi-builder --driver docker-container --use

# 2. Bootstrap the builder and list supported platforms
$ docker buildx inspect --bootstrap

Once bootstrapped, the output lists supported platforms such as linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le. That confirms BuildKit has QEMU user-mode emulation registered for foreign architectures.

3.2 Build and push both architectures together

Pass a comma-separated list to --platform and push straight to your registry:

1
2
3
4
5
$ docker buildx build \
  --platform linux/amd64,linux/arm64 \
  -t myregistry.com/backend/api:1.0.0 \
  --push \
  .

Why is --push required here?
The traditional local Docker image store (--load) can’t hold a multi-architecture manifest list under a single tag (unless you opt into the containerd image store). For multi-platform builds, push directly to a registry with --push or export an OCI tarball locally.

3.3 Verify the published manifest

Confirm that both architectures made it into the remote index:

1
$ docker buildx imagetools inspect myregistry.com/backend/api:1.0.0

If both linux/amd64 and linux/arm64 show up in the manifest list, Apple Silicon laptops, AWS Graviton nodes, and x86_64 servers can all pull the exact same tag without error.


4. Fixing QEMU emulation slowdowns in production pipelines

Getting buildx working is quick, but the next bottleneck hits immediately: emulated builds are painfully slow.

When an ARM64 Mac builds a linux/amd64 image (or an x86_64 CI runner builds linux/arm64), BuildKit translates every foreign CPU instruction through QEMU user-mode emulation. For compiled languages (Go, Rust, Java, C++) or heavy apt-get / npm steps, build times often jump 5x to 10x compared to native execution.

Here are three practical ways I’ve used to eliminate or bypass QEMU overhead.

4.1 Cross-compile on $BUILDPLATFORM

If your language toolchain supports native cross-compilation (Go, Rust, Swift) or compiles to architecture-independent bytecode (JVM JARs), run the build stage on the host’s native CPU and only target the foreign architecture in the final runtime stage.

BuildKit automatically injects platform variables (BUILDPLATFORM, TARGETPLATFORM, TARGETOS, TARGETARCH) into your Dockerfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# syntax=docker/dockerfile:1.4

# 1. Run the compiler natively on the host CPU (--platform=$BUILDPLATFORM)
FROM --platform=$BUILDPLATFORM golang:1.24-alpine AS builder
WORKDIR /workspace

COPY go.mod go.sum ./
RUN go mod download

COPY . .

# 2. Cross-compile for the target OS/arch with zero QEMU emulation
ARG TARGETOS
ARG TARGETARCH
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
    go build -ldflags="-w -s" -o /workspace/server ./cmd/server

# 3. Copy the compiled binary into the target-arch base image
FROM alpine:3.21
WORKDIR /app
COPY --from=builder /workspace/server /app/server

EXPOSE 8080
ENTRYPOINT ["/app/server"]

With this pattern, an ARM64 Mac compiles an amd64 Go binary at full native speed across all CPU cores, never invoking QEMU during compilation.

4.2 Offload multi-arch builds to GitHub Actions with BuildKit cache

Running multi-arch builds from developer laptops wastes time and bandwidth. Move them into CI and persist intermediate layers in the GitHub Actions cache:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
name: Build and Push Multi-Arch Image

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to Registry
        uses: docker/login-action@v3
        with:
          registry: myregistry.com
          username: ${{ secrets.REGISTRY_USER }}
          password: ${{ secrets.REGISTRY_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: myregistry.com/backend/api:${{ github.sha }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

Setting cache-to: type=gha,mode=max caches intermediate multi-stage build layers, not just the final image layers. That makes repeated dependency-heavy builds noticeably faster.

4.3 Attach native remote nodes to one Buildx builder

If you build C/C++ extensions, native wheels, or heavy Node/JVM projects where cross-compilation isn’t practical, QEMU won’t cut it. Instead, attach a real x86_64 host and a real ARM64 host (for example, an AWS Graviton instance) to the same Buildx builder over SSH:

1
2
3
4
5
6
7
8
# 1. Create a builder with the local amd64 node
$ docker buildx create --name hybrid-builder --node amd64-builder --platform linux/amd64 --driver-opt network=host

# 2. Append a remote arm64 node over SSH
$ docker buildx create --name hybrid-builder --append --node arm64-builder --platform linux/arm64 ssh://ubuntu@arm64-runner.internal

# 3. Switch to the hybrid builder
$ docker buildx use hybrid-builder

BuildKit dispatches the linux/amd64 build to the x86_64 machine and the linux/arm64 build to the ARM64 machine in parallel at native speed, then merges both results into one manifest list on push.

When not to use multi-arch builds: If your entire team and every staging/production server run a single architecture and you never share images across CPU families, skip multi-arch builds. Maintaining dual-platform builds adds CI minutes and registry storage with zero return until you actually run both architectures.


5. Practical decision table and checklist

Mixed fleets—Apple Silicon developer laptops, AWS Graviton ARM64 nodes, and x86_64 servers—are now common enough that single-arch images break fast. Use the strategy that matches your workload:

ScenarioRecommended approachTrade-off
Quick local test for an x86_64 serverdocker build --platform linux/amd64 on MacFast to run once, but slow under QEMU and single-arch only
Standard release imagedocker buildx build --platform linux/amd64,linux/arm64 --pushPublishes an OCI Manifest List; requires registry push
Go / Rust / JVM buildsFROM --platform=$BUILDPLATFORM cross-compilationNative compile speed, needs Dockerfile adjustment
Heavy native C/C++ buildsNative remote Buildx nodes (x86_64 + ARM64)Zero QEMU overhead, requires maintaining two runner hosts

Before shipping your next multi-arch pipeline, run through this quick checklist:

  1. Inspect the published index: Run docker buildx imagetools inspect <image:tag> and verify both linux/amd64 and linux/arm64 are listed.
  2. Avoid QEMU during compilation: Pin build stages to --platform=$BUILDPLATFORM whenever the compiler can cross-compile.
  3. Cache intermediate layers in CI: Enable type=gha,mode=max (or registry cache) so foreign-arch dependency layers aren’t rebuilt on every commit.
  4. Never rely on local laptop pushes for production: Build release images in CI so a stray docker build on an Apple Silicon Mac can’t overwrite a production tag with an ARM64-only manifest.
This post is licensed under CC BY 4.0 by the author.