Subscribe

Building multi-architecture images for amd64 and arm64 with buildx

One shorty image tag that runs on x86 and on Graviton. I timed the Go build under emulation (47 seconds) against cross-compiling with --platform=$BUILDPLATFORM (under 14), then look at native arm64 runners, checking the result, and what arm64 saves on Fargate.

Building multi-architecture images for amd64 and arm64 with buildx

My laptop is an arm64 Mac. GitHub's default runners are x86. ECS Fargate can run either, and arm64 (Graviton) is cheaper. If an image only has one architecture, sooner or later it lands on a machine of the other one and refuses to start.

In this blog we will build one shorty image tag that holds both linux/amd64 and linux/arm64, using docker buildx. I timed the three ways to get there on my own machine: emulating the other CPU, cross-compiling Go, and building each half on a native machine. Then we check what actually got built and what arm64 saves on Fargate. Level is intermediate. You should know the shorty Dockerfile from hardening a Go container image, and it helps to have read faster Docker builds, because I reuse its cache mount here.

Versions: Docker 29.8.2 and buildx 0.37.2 on OrbStack 2.2.5, Apple M5 with 10 cores, golang:1.27.4-alpine and gcr.io/distroless/static-debian13:nonroot.

💻 The full code for this tutorial is on GitHub: FenixAra/tutorials/Docker/Images-and-Containers/building-multi-architecture-images-for-amd64-and-arm64-with-buildx. Clone it and run each step yourself.

What a multi-arch image is

A tag like shorty:0.2.0 normally points at one image manifest. A multi-arch tag points at an image index instead: a small list that says "for linux/amd64 use this manifest, for linux/arm64 use that one". When a machine pulls the tag, it picks its own entry. Same name, two images.

Both the base images I use already work this way. You can see it with imagetools:

docker buildx imagetools inspect gcr.io/distroless/static-debian13:nonroot | grep Platform
  Platform:  linux/amd64
  Platform:  linux/arm64/v8
  Platform:  linux/arm/v7
  Platform:  linux/s390x
  Platform:  linux/ppc64le
  Platform:  linux/riscv64

So for shorty the only real work are building the Go binary for each CPU.

A builder of your own

For the timings I used my own docker-container builder instead of the default one, so I could throw its cache away between runs and not mix it with anything else on the machine:

docker buildx create --name shorty-builder --driver docker-container --bootstrap
docker buildx inspect shorty-builder | grep Platforms
Platforms:             linux/arm64, linux/amd64, linux/amd64/v2, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/arm/v7, linux/arm/v6

Everything except linux/arm64 in that list is emulated. On a Linux x86 box you'd install emulators first, usually with docker run --privileged --rm tonistiigi/binfmt --install all or with docker/setup-qemu-action in GitHub Actions. On a Mac, OrbStack and Docker Desktop register them for you.

Way 1: emulate the other CPU

The simplest Dockerfile for two platforms is the one you already have. Pass --platform and BuildKit runs every stage once per platform:

docker buildx build --builder shorty-builder --no-cache \
  --platform linux/amd64,linux/arm64 -f Dockerfile.emulated .

Here Dockerfile.emulated is the hardening post's Dockerfile, untouched. For the amd64 half, the whole build stage runs on amd64. The golang image is pulled as amd64, and the Go compiler itself runs under emulation on my arm64 CPU. That's quite slow:

amd64 only, emulated     go build 47.5s    total 58.2s
arm64 only, native       go build 10.2s    total 13.6s
both at once             arm64 15.6s, amd64 42.4s    total 46.2s

The amd64 compile took more than four times as long as the native one. OrbStack has Rosetta switched on in its settings, and I didn't dig into whether this compile ran under Rosetta or QEMU. On a GitHub x86 runner the arm64 half runs under QEMU, so expect the same kind of penalty there, just the other way round.

But emulation actually does have one real use: when the build stage has to run code for the target, like apt-get install of native packages or a cgo build. For a pure Go service it's just the slow path.

Way 2: cross-compile Go

Go compiles for any OS and CPU from any machine, as long as cgo is off. So the build stage can always run on the builder's own CPU, and only the output changes per target. Two changes to the Dockerfile:

# syntax=docker/dockerfile:1

FROM --platform=$BUILDPLATFORM golang:1.27.4-alpine AS build
WORKDIR /src

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

ARG VERSION=dev
ARG TARGETOS TARGETARCH
RUN --mount=type=cache,target=/root/.cache/go-build \
    --mount=type=bind,target=. \
    CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
    go build -trimpath -ldflags="-s -w -X main.version=${VERSION}" -o /out/shorty ./cmd/shorty

FROM gcr.io/distroless/static-debian13:nonroot
COPY --from=build /out/shorty /shorty
USER 65532:65532
EXPOSE 8080
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 CMD ["/shorty", "healthcheck"]
CMD ["/shorty", "serve"]

--platform=$BUILDPLATFORM pins the build stage to the machine doing the build. TARGETOS and TARGETARCH are filled in by BuildKit for each platform you ask for, but only inside a stage that declares them with ARG. Leave that line out and both variables are empty. An empty GOARCH means "the machine I'm running on" (I checked, GOARCH= go env GOARCH prints arm64 on my Mac), so you'd get two arm64 binaries in a "multi-arch" image and nothing would complain. That's why I check the output below.

The runtime stage has no RUN, it only copies a file into distroless, so it never needs emulation either. The GitHub Actions image post already uses this Dockerfile shape, and this is why it can skip the QEMU setup step.

I cleared the builder's cache and built both platforms:

docker buildx prune --builder shorty-builder --all --force
docker buildx build --builder shorty-builder --platform linux/amd64,linux/arm64 \
  --build-arg VERSION=0.2.0-multi -f Dockerfile .
#14 [linux/arm64 build 4/5] RUN go mod download
#15 [linux/arm64->amd64 build 5/5] RUN ... GOOS=linux GOARCH=amd64 go build ...
#16 [linux/arm64 build 5/5] RUN ... GOOS=linux GOARCH=arm64 go build ...
#15 DONE 13.7s
#16 DONE 13.7s

go mod download ran once, since the build stage is the same for both. The arm64->amd64 label is BuildKit telling you it's cross-compiling. The two compiles ran side by side and took 13.7 seconds each (they share the CPU, so each is slower than alone). The whole build was 25.7 seconds, and about 7 of that was pulling the base images into an empty builder. Compare with 46 seconds for the emulated version with warm base images.

This only works with CGO_ENABLED=0. See, with cgo you need a C cross compiler for each target and it gets messy fast. Then a native builder is the better answer.

Checking what you built

Load it into local image store and look:

docker buildx build --builder shorty-builder --platform linux/amd64,linux/arm64 \
  --build-arg VERSION=0.2.0-multi -t shorty:multi --load .
docker image ls --tree shorty:multi
IMAGE                ID             DISK USAGE   CONTENT SIZE   EXTRA
shorty:multi    473fd6d4400d       31.7MB         11.8MB
├─ linux/amd64       459fcf5d48a7       6.11MB         6.11MB
└─ linux/arm64       29389b40f8cb       25.6MB         5.64MB

(--load with several platforms needs the containerd image store, which OrbStack and current Docker Desktop use. With the old store you have to --push to a registry instead.)

Then run each one:

docker run --rm --platform linux/amd64 shorty:multi /shorty version
docker run --rm --platform linux/arm64 shorty:multi /shorty version
0.2.0-multi
0.2.0-multi

Running is good, but it doesn't prove the amd64 image has an amd64 binary. An emulator would happily run an arm64 binary that ended up in the wrong place. So I copied the file out and asked:

docker create --name shorty-x --platform linux/amd64 shorty:multi
docker cp shorty-x:/shorty ./shorty-amd64
docker rm shorty-x
file shorty-amd64
go version -m shorty-amd64 | grep -E 'GOARCH|GOOS|CGO'
shorty-amd64: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=..., stripped
    build   CGO_ENABLED=0
    build   GOARCH=amd64
    build   GOOS=linux

go version -m reads the build info Go stamps into every binary. Very handy for this.

And the failure you get when the platform is missing: I built an arm64-only image and ran it as amd64. Locally Docker didn't find a matching platform, went off to Docker Hub to pull one and failed with pull access denied for shorty. Which is a confusing message for "wrong architecture". ECS also fails at the pull step, so if a new task never starts, the stopped reason of the task is the first place to look.

Way 3: native builders

Cross-compiling covers shorty. For images that really need to run code for the target (cgo, native packages), the fast option is a real machine of each CPU.

With buildx you can add several nodes to one builder, and it sends each platform to the node that runs it natively:

docker buildx create --name native --platform linux/amd64 ssh://ubuntu@x86-builder.example.com
docker buildx create --name native --append --platform linux/arm64 ssh://ubuntu@arm-builder.example.com
docker buildx build --builder native --platform linux/amd64,linux/arm64 -t example/app:dev --push .

I don't have two build machines, so I haven't run this one. In CI it's easier now. GitHub has hosted arm64 runners (ubuntu-24.04-arm), free for public repos and available for private repos since January 2026. The pattern is a matrix job that builds one platform per runner and pushes by digest, then a small job that joins the digests into one tag:

jobs:
  build:
    strategy:
      matrix:
        include:
          - platform: linux/amd64
            runner: ubuntu-24.04
          - platform: linux/arm64
            runner: ubuntu-24.04-arm
    runs-on: ${{ matrix.runner }}
    steps:
      # AWS credentials and ECR login as in the GitHub Actions image post
      - uses: docker/setup-buildx-action@v4
      - name: Build and push by digest
        id: build
        uses: docker/build-push-action@v7
        with:
          platforms: ${{ matrix.platform }}
          outputs: type=image,name=${{ env.ECR_IMAGE }},push-by-digest=true,name-canonical=true,push=true
          cache-from: type=gha,scope=${{ matrix.platform }}
          cache-to: type=gha,mode=max,scope=${{ matrix.platform }}
      # then save steps.build.outputs.digest as an artifact

  merge:
    needs: build
    runs-on: ubuntu-24.04
    steps:
      # download the digest artifacts into $RUNNER_TEMP/digests, log in to ECR
      - name: Create the multi-arch tag
        working-directory: ${{ runner.temp }}/digests
        run: |
          docker buildx imagetools create -t "$ECR_IMAGE:sha-${GITHUB_SHA::7}" \
            $(printf "$ECR_IMAGE@sha256:%s " *)

The full workflow is longer because of the artifact plumbing, and actionlint 1.7.12 passes it, but I haven't run it on GitHub. Docker's docs now also point to a reusable workflow, docker/github-builder, that does this split and merge for you. For shorty I'm sticking with cross-compiling on one runner only. It's one job, it's 14 seconds, and there is nothing to merge.

What arm64 saves

This is the reason to bother, the rest was plumbing. Fargate on-demand prices in us-east-1, per hour:

             per vCPU     per GB memory
Linux/x86    $0.04048     $0.004445
Linux/ARM    $0.03238     $0.00356

The ECS deploy post runs two shorty tasks with 0.25 vCPU and 512 MB. For 730 hours that's about $9.01 a month per task on x86 and $7.21 on arm64. $18.02 against $14.42 for the pair. Exactly 20% on both rates. Small money for two tiny tasks, but it scales with everything you run and it costs nothing once the image is multi-arch. Check the prices for your own region, they're different in Mumbai.

On ECS the switch is one block in the task definition:

  runtime_platform {
    operating_system_family = "LINUX"
    cpu_architecture        = "ARM64"
  }

EC2 is similar. AWS says T4g costs about 20% less than T3 for the same size. My own blog runs on a t3.small, but it's a Reserved Instance, so it stays x86 until the reservation runs out (the setup is here). That's the other thing to check before moving: a reservation or Savings Plan you already paid for.

Cleaning up

docker rmi shorty:multi
docker buildx rm shorty-builder

buildx rm deletes the builder container and its whole cache, so it's the easy way to get the disk back after a day of timing builds.