---
title: "Cross-Compile Multi-Arch Docker Images | StarSling"
description: "Run the compile stage of an arm64 Docker build on the runner's own CPU with BUILDPLATFORM and a cross-compiler, and verify every binary's architecture."
url: https://starsling.dev/github-actions/optimizations/cross-compile-multi-arch-images
canonicalUrl: https://starsling.dev/github-actions/optimizations/cross-compile-multi-arch-images
---

# Cross-compile multi-arch Docker images in GitHub Actions

[GitHub Actions](https://starsling.dev/github-actions) / [Optimizations](https://starsling.dev/github-actions/optimizations) / Cross-compile multi-arch Docker images in GitHub Actions

- [How StarSling works](https://starsling.dev/)

Rule: ci.build.emulated-image-compilation. Detection mode: static. Last updated: 2026-11-29

A multi-platform Docker build compiles each foreign architecture at native speed when the compile stage runs on the build platform (`FROM --platform=$BUILDPLATFORM`) and a cross-compiler targets each platform. The image still ships every platform; only the machine the compiler runs on changes.

## Table of contents

- [Do this](#do-this)
- [Avoid this](#avoid-this)
- [How to detect it](#how-to-detect-it)
- [Tradeoffs and safety](#tradeoffs)
- [Measured on open-source projects](#measured-on-open-source-projects)
- [Cross-compile cgo with tonistiigi/xx](#cross-compile-cgo-with-xx)
- [Verify it worked](#verify)
- [Related pages](#related-pages)
- [Sources](#sources)

<a id="do-this"></a>

## Do this

When a Buildx step builds `linux/arm64` on an x64 runner, every `RUN` in a stage based on the target platform executes through QEMU, so the whole Go, Rust or C compile runs at emulation speed. Setting `GOARCH` inside such a stage still runs the compiler itself under emulation. Move the compile stage to `$BUILDPLATFORM` and pass the target to the compiler.

_Dockerfile_

```dockerfile
# Same workflow, same platforms. The build stage runs on the runner's own
# CPU and Go cross-compiles for each target platform.
FROM --platform=$BUILDPLATFORM golang:1.25-alpine AS build
ARG TARGETOS TARGETARCH TARGETVARIANT
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH GOARM=${TARGETVARIANT#v} \
    go build -o /out/app ./cmd/app

# The runtime stage keeps the target platform: it only copies the binary.
FROM alpine:3.22
COPY --from=build /out/app /usr/local/bin/app
ENTRYPOINT ["app"]
```

<a id="avoid-this"></a>

## Avoid this

_Dockerfile_

```dockerfile
# Built by docker/build-push-action with platforms: linux/amd64,linux/arm64
# on an ubuntu-latest runner. This stage runs on the TARGET platform, so the
# arm64 leg runs the whole Go compile under QEMU, GOARCH included.
FROM golang:1.25-alpine AS build
ARG TARGETARCH
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOARCH=$TARGETARCH go build -o /out/app ./cmd/app

FROM alpine:3.22
COPY --from=build /out/app /usr/local/bin/app
ENTRYPOINT ["app"]
```

<a id="how-to-detect-it"></a>

## How to detect it

[ci-speedup](https://starsling.dev/skills/ci-speedup) carries this rule's detection logic and opens the fix as a reviewable pull request. Install it with `npx skills add starslingdev/skills`, then run `/ci-speedup` in your repository.

To check by hand:

1. Find multi-platform builds: `grep -rnE 'platforms:|--platform' .github/workflows/` and note each job whose platform list includes an architecture other than its runner's own, for example `linux/arm64` on `ubuntu-latest`. A platform the runner builds natively needs no change.
2. Open the Dockerfile each of those jobs builds. For every stage the final image copies from, check whether it runs a compile command (`go build`, `cargo build`, `make`, `cmake`, `npm rebuild`, `pip wheel`) and whether its `FROM` line, or the stage it is based on, carries `--platform=$BUILDPLATFORM`. A compile stage without it runs under emulation for every foreign platform.
3. Size it from a recent run log: compare the Buildx `RUN` step times for the foreign platform with the same step for the native one. The change pays off when the foreign-architecture compile is at least half of the image build's layer time.
4. Confirm the toolchain can target the platform from the build machine: Go with `CGO_ENABLED=0` always can; cgo, Rust `-sys` crates and native Node add-ons need a target C toolchain such as `tonistiigi/xx` or `gcc-<arch>-linux-gnu`. When none exists, build that platform on a native runner instead.

<a id="tradeoffs"></a>

## Tradeoffs and safety

- Cross-compiling still runs one native compile per extra architecture, so the image build does more work than a single-platform build. Size the saving as the emulated seconds minus the native seconds for each foreign platform, and compare runner minutes as well as wall time.
- cgo, Rust `-sys` crates and native Node add-ons need a target C toolchain and target libraries. `tonistiigi/xx` or `gcc-<arch>-linux-gnu` covers most of them. When a toolchain has no cross target for a platform, build that platform on a native runner and merge the manifests, as in [Build each architecture on native runners](/ci-playbooks/techniques/native-architecture-builds).
- A different C compiler or linker can change the binary. Gogs installs the target's GNU binutils so clang links with the same linker its emulated gcc build used; compare binary size and run the image after switching.
- Keep every platform in the build, and run each image on its own architecture after merge or on a schedule: that run is the first place the cross-built binary executes.

<a id="measured-on-open-source-projects"></a>

## Measured on open-source projects

Measured on StarSling-run forks replaying each project's upstream CI: five runs with the change and five without, all on StarSling Runners.

- [Gogs](https://github.com/gogs/gogs) - 47,988 upstream stars on 2026-11-23. Image build step: 583 s -> 94 s from a cold start, 6.2x faster. Measured scope: Build and push next-gen images step of the buildx-next job. Cache condition: Cold. Sources: [Public benchmark fork](https://github.com/starsling-demo-org/benchmark-gogs) - [Merged agent PR](https://github.com/starsling-demo-org/benchmark-gogs/pull/1).
- [LiveKit](https://github.com/livekit/livekit) - 22,354 upstream stars on 2026-11-23. Docker build steps: 581 s -> 118 s from a cold start, 4.9x faster. Measured scope: Build and push steps of both Docker jobs. Cache condition: Cold. Sources: [Public benchmark fork](https://github.com/starsling-demo-org/benchmark-livekit) - [Merged agent PR](https://github.com/starsling-demo-org/benchmark-livekit/pull/1).
- [transfer.sh](https://github.com/dutchcoders/transfer.sh) - 15,910 upstream stars on 2026-11-23. Image build step: 442 s -> 74 s from a cold start, 6.0x faster. Measured scope: Image build step of the buildx job. Cache condition: Cold. Sources: [Public benchmark fork](https://github.com/starsling-demo-org/benchmark-transfer.sh) - [Merged agent PR](https://github.com/starsling-demo-org/benchmark-transfer.sh/pull/1).
- [WriteFreely](https://github.com/writefreely/writefreely) - 5,298 upstream stars on 2026-11-23. Image build step: 525 s -> 102 s from a cold start, 5.1x faster. Measured scope: Build and push Docker images step of the build job. Cache condition: Cold. Sources: [Public benchmark fork](https://github.com/starsling-demo-org/benchmark-writefreely) - [Merged agent PR](https://github.com/starsling-demo-org/benchmark-writefreely/pull/1).

<a id="cross-compile-cgo-with-xx"></a>

## Cross-compile cgo with tonistiigi/xx

Go turns cgo off when it cross-compiles unless `CGO_ENABLED=1` is set, and a cgo build then needs a C toolchain and libraries for the target. `tonistiigi/xx` supplies both: `xx-apk` installs the target's packages, `xx-go` sets `GOOS`, `GOARCH` and `GOARM` from `TARGETPLATFORM` and points cgo at a clang cross toolchain, and `xx-verify` checks that each binary targets the right CPU. Rust `-sys` crates and native Node add-ons follow the same shape with their own cross toolchains.

_Dockerfile_

```dockerfile
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.6.1 AS xx

FROM --platform=$BUILDPLATFORM golang:1.25-alpine AS build
COPY --from=xx / /
RUN apk add --no-cache clang lld
ARG TARGETPLATFORM
RUN xx-apk add --no-cache gcc musl-dev
WORKDIR /src
COPY . .
ENV CGO_ENABLED=1
RUN xx-go build -o /out/app ./cmd/app && xx-verify /out/app
```

<a id="verify"></a>

## Verify it worked

Hand this prompt to your coding agent (Claude Code, Cursor, and the like) to run this audit and open the fix as a reviewable PR:

```
Find the Docker builds in this repository that compile a foreign CPU architecture
under emulation, and move that compile onto the build machine's own CPU.

1. Read this repository's agent instructions (AGENTS.md, CLAUDE.md, CONTRIBUTING)
   and the Docker multi-platform and cross-compilation docs linked on this page
   before changing anything.
2. grep .github/workflows/ for "platforms:" and "--platform". For each job, note
   its runs-on label and every platform it builds. Skip a platform the runner
   builds natively.
3. Open each Dockerfile those jobs build. For every stage the final image copies
   from, check whether it runs a compile (go build, cargo build, make, cmake,
   npm rebuild, pip wheel) and whether its FROM line, or the stage it is based
   on, has --platform=$BUILDPLATFORM. List each compile stage that runs on the
   target platform.
4. Measure before changing: from the last few runs' logs, take the Buildx RUN
   step times for the foreign platform and the native one. Report them. Stop and
   tell me if the foreign compile is under half of the image build's layer time.
5. For each listed stage, add --platform=$BUILDPLATFORM to its FROM and pass the
   target to the compiler (Go: GOOS/GOARCH/GOARM from TARGETOS/TARGETARCH/
   TARGETVARIANT). If it needs cgo or another native toolchain, use
   tonistiigi/xx (xx-apk, xx-go or xx-clang) and add xx-verify on every binary.
   If the toolchain cannot target a platform, stop and tell me; do not force it.
6. Keep every platform, test, scan and publish step. Leave runtime stages (the
   ones that only copy the binary) on the target platform.
7. ASK ME TO APPROVE THE EXACT DIFF BEFORE EDITING ANY FILES. After approval,
   work on a new branch and open a pull request; never push to the default
   branch. In the PR body, list each stage changed, the before and after
   foreign-platform compile times, and how each binary's architecture was
   verified.
```

Confirm the change landed:

1. In the build log, confirm the compile `RUN` step for each foreign platform now shows the build platform (for example `[linux/amd64->arm64 build ...]`) and runs in about native time.
2. Run `docker buildx imagetools inspect <image>` and confirm the manifest lists every platform it listed before.
3. Check each binary's architecture: `xx-verify` in the build, or `docker run --rm --platform linux/arm64 <image> uname -m` on a host that runs that platform.
4. Compare the image build step's median over five runs with the change and five without, and the job's runner minutes.

<a id="related-pages"></a>

## Related pages

- [Speed up Docker builds in GitHub Actions](https://starsling.dev/github-actions/optimizations/optimize-docker-builds)
- [Use Docker layer caching in GitHub Actions](https://starsling.dev/github-actions/optimizations/use-docker-layer-caching)
- [The right way to configure docker/build-push-action](https://starsling.dev/github-actions/docker-build-push-action)

<a id="sources"></a>

## Sources

- [Docker: Multi-platform builds](https://docs.docker.com/build/building/multi-platform/)
- [Docker: Faster multi-platform builds with Dockerfile cross-compilation](https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation/)
- [tonistiigi/xx: Dockerfile cross-compilation helpers](https://github.com/tonistiigi/xx)
