Docker 59 🐳 Multi-Platform Image Builds: docker buildx and QEMU Cross-Compilation Architecture
A container image built on an x86_64 laptop runs on an x86_64 server. It does not run on an Apple Silicon Mac, an AWS Graviton instance, or a Raspberry Pi. The binary inside the image is compiled for one CPU architecture, and the kernel rejects it on any other. Multi-platform builds solve this by producing a single image reference that resolves to different binaries on different architectures. The mechanism is a manifest list, a pointer to several platform-specific images, and the tool is docker buildx. When the build host cannot natively compile for the target architecture, QEMU emulates it. The result is a docker pull that works everywhere, and a build pipeline that produces the right binary for every platform.
Key point: A multi-platform image is not a single image with multiple binaries. It is a manifest list that points to separate images, one per platform. The registry stores the manifest, and the Docker client selects the matching image for the host’s architecture.
Why multi-platform builds matter
The architecture problem. A binary compiled for linux/amd64 does not run on linux/arm64. The instruction sets are different, the syscalls are compatible at the source level but not at the binary level, and the kernel refuses to execute a foreign binary. The image must contain a binary compiled for the target architecture.
The ecosystem problem. Apple Silicon Macs run linux/arm64 containers. AWS Graviton instances run linux/arm64. Raspberry Pis run linux/arm/v7 or linux/arm64. A development team that uses Macs and deploys to Graviton needs both architectures. A CI pipeline that runs on x86_64 and deploys to a mixed fleet needs both.
The manifest list problem. Docker Hub and other registries support manifest lists, which map a tag to a set of platform-specific images. The docker pull command reads the manifest, selects the image matching the host’s architecture, and pulls only that image . The user sees one tag; the registry stores several images.
The emulation problem. Building for linux/arm64 on an linux/amd64 host requires either a native ARM64 builder or an emulator. QEMU is the emulator that buildx uses. It translates the ARM64 instructions to x86_64 instructions at runtime, which is slower than native but requires no additional hardware .
The native runner problem. A native ARM64 runner, such as GitHub’s ubuntu-24.04-arm, builds faster than emulation because no translation is involved . The trade-off is availability: native runners cost more and are not available on every CI platform.
The build-time problem. The Dockerfile often runs commands that must execute on the build platform, not the target platform. The $BUILDPLATFORM, $TARGETOS, and $TARGETARCH arguments are automatically set by buildx, and they let the Dockerfile distinguish between the platform it is running on and the platform it is building for .
The provenance problem. A multi-platform image is a supply chain artifact. The manifest list should be signed, and the attestations should cover every platform. Cosign and the SBOM attestations apply to the manifest, not to the individual images.
a. Setting up buildx
docker buildx is a CLI plugin that extends the docker build command with multi-platform support. It is included in Docker Desktop and in recent Docker Engine versions.
The builder creation
The default builder does not support multi-platform builds. A new builder with the docker-container driver is required.
# Create a new builder with the docker-container driver
docker buildx create --name multiarch --driver docker-container --use
# Bootstrap the builder
docker buildx inspect --bootstrap
The --use flag switches the current context to the new builder. The --bootstrap flag initializes the builder, which starts the BuildKit container and installs the QEMU emulators .
The QEMU setup
The multiarch/qemu-user-static image registers the QEMU emulators with the kernel’s binfmt_misc subsystem. This allows the host to execute binaries for other architectures transparently .
# Register the QEMU emulators
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
The --privileged flag is required because the registration modifies the kernel’s binary format handlers. In CI environments, the docker/setup-qemu-action performs the same setup .
The builder drivers
| Driver | Multi-platform | Use case |
|---|---|---|
docker | No | Default; uses the Docker daemon |
docker-container | Yes | The standard for multi-platform |
kubernetes | Yes | Builds in a Kubernetes cluster |
remote | Yes | Connects to a remote buildkitd |
The docker-container driver is the standard for local multi-platform builds. It starts a BuildKit container and uses it for the build .
The verification
The docker buildx ls command lists the available builders and the platforms they support.
docker buildx ls
# NAME/NODE DRIVER/ENDPOINT STATUS BUILDKIT PLATFORMS
# multiarch* docker-container
# multiarch0 unix:///var/run/docker.sock running v0.14.0 linux/amd64, linux/arm64, ...
The PLATFORMS column lists the architectures the builder can target.
The experimental flag
In older Docker versions, the buildx plugin requires the experimental CLI features.
export DOCKER_CLI_EXPERIMENTAL=enabled
The flag is not needed in Docker 20.10 and later .
b. The multi-platform build
The docker buildx build command is the entry point. The --platform flag specifies the target architectures, and the --push flag sends the manifest list to the registry.
The basic build
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t myregistry.example.com/myapp:latest \
--push .
The command builds the image for both platforms in parallel, creates the manifest list, and pushes the result. The --push flag is required because the local Docker image store does not support manifest lists .
The platform list
The --platform flag accepts a comma-separated list of platforms.
| Platform | Description |
|---|---|
linux/amd64 | x86_64 |
linux/arm64 | ARM64 (Apple Silicon, Graviton) |
linux/arm/v7 | ARMv7 (Raspberry Pi 32-bit) |
linux/arm/v6 | ARMv6 (older Raspberry Pi) |
linux/386 | 32-bit x86 |
linux/ppc64le | POWER8+ |
linux/s390x | IBM Z |
linux/riscv64 | RISC-V |
A base image like python:3.12-alpine3.23 supports 16 platforms, including amd64, arm64, arm/v7, arm/v6, 386, ppc64le, riscv64, and s390x .
The output options
The --output flag controls where the build result goes.
| Output | Effect |
|---|---|
type=registry | Push to the registry (same as --push) |
type=docker | Load into the local Docker image store |
type=local | Write to a local directory |
type=oci | Write an OCI image layout |
The type=docker output does not support multi-platform builds, because the local image store cannot represent a manifest list .
The attestations
The --provenance and --sbom flags attach attestations to the build result.
docker buildx build \
--platform linux/amd64,linux/arm64 \
--provenance=true \
--sbom=true \
-t myregistry.example.com/myapp:latest \
--push .
The provenance attestation records the build inputs and the build system. The SBOM attestation records the dependencies. Both are attached as separate OCI artifacts and are covered by the manifest list .
The build secrets
The --secret flag exposes a secret to the build. The secret is mounted for the duration of a RUN instruction and is not written to any layer .
docker buildx build \
--platform linux/amd64,linux/arm64 \
--secret id=npmrc,src=$HOME/.npmrc \
-t myregistry.example.com/myapp:latest \
--push .
The src is a file path, and the env is an environment variable. The type is auto-detected if it is not specified .
The cache
The --cache-from and --cache-to flags enable build cache sharing.
docker buildx build \
--platform linux/amd64,linux/arm64 \
--cache-from type=registry,ref=myregistry.example.com/myapp:cache \
--cache-to type=registry,ref=myregistry.example.com/myapp:cache,mode=max \
-t myregistry.example.com/myapp:latest \
--push .
The registry cache is shared across runners, which makes CI builds faster .
c. The Dockerfile patterns
A multi-platform Dockerfile must account for the build platform and the target platform. The $BUILDPLATFORM, $TARGETOS, and $TARGETARCH arguments are automatically set by buildx.
The platform arguments
FROM --platform=$BUILDPLATFORM golang:1.22 AS build
ARG TARGETOS TARGETARCH
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /app .
The --platform=$BUILDPLATFORM on the FROM line makes the build stage run on the build platform, not the target platform. The TARGETOS and TARGETARCH arguments are used for the cross-compilation .
The base image selection
The base image must support the target platform. A multi-platform base image has a manifest list that covers the architectures.
FROM node:22-alpine
The node:22-alpine image is multi-platform and covers linux/amd64 and linux/arm64 . The postgres:16 image is also multi-platform .
The conditional logic
The Dockerfile can branch on the target architecture.
RUN if [ "$TARGETARCH" = "arm64" ]; then \
echo "Building for ARM64"; \
else \
echo "Building for AMD64"; \
fi
The conditional is used when a platform-specific dependency must be installed or a platform-specific binary must be downloaded .
The multi-stage pattern
A multi-stage build separates the build environment from the runtime environment. The build stage runs on the build platform, and the runtime stage uses the target platform’s base image.
FROM --platform=$BUILDPLATFORM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:22-alpine AS runtime
COPY --from=build /app/dist ./dist
CMD ["node", "dist/index.js"]
The build stage uses the build platform’s Node.js, and the runtime stage uses the target platform’s Node.js. The --from=build copy works because the artifacts are architecture-independent JavaScript .
The cross-compilation pattern
For compiled languages, the build stage uses the build platform’s compiler to produce a binary for the target platform.
FROM --platform=$BUILDPLATFORM golang:1.22 AS build
ARG TARGETOS TARGETARCH
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /app/server
FROM alpine:3.19
COPY --from=build /app/server /server
CMD ["/server"]
The Go compiler cross-compiles without QEMU, because it has a built-in cross-compiler. The CGO_ENABLED=0 disables the C dependencies, which would require a C cross-compiler .
The QEMU-dependent pattern
Some builds cannot cross-compile and must run the target platform’s tools. In that case, the build stage runs under QEMU.
FROM node:22-alpine AS build
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html
The FROM node:22-alpine line uses the target platform’s image, and the npm install runs under QEMU when the target platform differs from the build platform. The build is slower, but it works for packages with native dependencies.
Complete Example Session
# ============================================
# PART 1: THE BUILDER CREATION
# ============================================
# Create a new builder with the docker-container driver
docker buildx create --name multiarch --driver docker-container --use
# Bootstrap the builder and install QEMU
docker buildx inspect --bootstrap
# ============================================
# PART 2: THE MULTI-PLATFORM BUILD
# ============================================
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t myregistry.example.com/myapp:latest \
--push .
# ============================================
# PART 3: THE PLATFORM LIST
# ============================================
docker buildx build \
--platform linux/amd64,linux/arm64,linux/arm/v7 \
-t myregistry.example.com/myapp:latest \
--push .
# ============================================
# PART 4: THE ATTESTATIONS
# ============================================
docker buildx build \
--platform linux/amd64,linux/arm64 \
--provenance=true \
--sbom=true \
-t myregistry.example.com/myapp:latest \
--push .
# ============================================
# PART 5: THE BUILD SECRET
# ============================================
docker buildx build \
--platform linux/amd64,linux/arm64 \
--secret id=npmrc,src=$HOME/.npmrc \
-t myregistry.example.com/myapp:latest \
--push .
# ============================================
# PART 6: THE CACHE
# ============================================
docker buildx build \
--platform linux/amd64,linux/arm64 \
--cache-from type=registry,ref=myregistry.example.com/myapp:cache \
--cache-to type=registry,ref=myregistry.example.com/myapp:cache,mode=max \
-t myregistry.example.com/myapp:latest \
--push .
# ============================================
# PART 7: THE DOCKERFILE WITH PLATFORM ARGUMENTS
# ============================================
# Dockerfile
FROM --platform=$BUILDPLATFORM golang:1.22 AS build
ARG TARGETOS TARGETARCH
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /app/server
FROM alpine:3.19
COPY --from=build /app/server /server
CMD ["/server"]
# ============================================
# PART 8: THE VERIFICATION
# ============================================
# Inspect the manifest list
docker buildx imagetools inspect myregistry.example.com/myapp:latest
# Output:
# Name: myregistry.example.com/myapp:latest
# MediaType: application/vnd.docker.distribution.manifest.list.v2+json
# Manifests:
# Name: myregistry.example.com/myapp:latest@sha256:...
# Platform: linux/amd64
# Name: myregistry.example.com/myapp:latest@sha256:...
# Platform: linux/arm64
# ============================================
# PART 9: THE PULL
# ============================================
# On an x86_64 host
docker pull myregistry.example.com/myapp:latest
# Pulls the linux/amd64 image
# On an ARM64 host
docker pull myregistry.example.com/myapp:latest
# Pulls the linux/arm64 image
# ============================================
# PART 10: THE COMPLETE PATTERN
# ============================================
# 1. Create a docker-container builder.
# 2. Bootstrap it to install QEMU.
# 3. Use --platform with the target architectures.
# 4. Use --push to send the manifest list to the registry.
# 5. Add --provenance and --sbom for supply chain security.
# 6. Use --secret for build-time credentials.
# 7. Use $BUILDPLATFORM and $TARGETARCH in the Dockerfile.
# 8. Use native ARM64 runners when available.
# 9. Verify with docker buildx imagetools inspect.
# 10. The user pulls one tag and gets the right binary.
The ten parts showed the builder creation, the multi-platform build, the platform list, the attestations, the build secret, the cache, the Dockerfile with platform arguments, the verification, the pull, and the complete pattern.
Quick Reference
The buildx Commands
| Command | Purpose |
|---|---|
docker buildx create | Create a builder |
docker buildx inspect --bootstrap | Initialize the builder |
docker buildx ls | List builders and platforms |
docker buildx build | Build a multi-platform image |
docker buildx imagetools inspect | Inspect a manifest list |
docker buildx rm | Remove a builder |
The Build Flags
| Flag | Purpose |
|---|---|
--platform | Target architectures |
--push | Push to the registry |
--load | Load into the local image store |
--provenance | Attach provenance attestation |
--sbom | Attach SBOM attestation |
--secret | Expose a build secret |
--cache-from | Read the build cache |
--cache-to | Write the build cache |
The Platform Arguments
| Argument | Value |
|---|---|
$BUILDPLATFORM | The platform the build runs on |
$TARGETPLATFORM | The platform the image is built for |
$TARGETOS | The target operating system |
$TARGETARCH | The target architecture |
$TARGETVARIANT | The target variant |
The Common Platforms
| Platform | Architecture | Example |
|---|---|---|
linux/amd64 | x86_64 | Most servers |
linux/arm64 | ARM64 | Apple Silicon, Graviton |
linux/arm/v7 | ARMv7 | Raspberry Pi 32-bit |
linux/arm/v6 | ARMv6 | Older Raspberry Pi |
linux/386 | 32-bit x86 | Legacy |
The Builder Drivers
| Driver | Multi-platform | QEMU |
|---|---|---|
docker | No | No |
docker-container | Yes | Yes |
kubernetes | Yes | Yes |
remote | Yes | Yes |
Best Practices
✅ Do This:
# Create a docker-container builder.
docker buildx create --name multiarch --driver docker-container --use # ✅
# Bootstrap to install QEMU.
docker buildx inspect --bootstrap # ✅
# Use --push for multi-platform builds.
docker buildx build --platform linux/amd64,linux/arm64 --push . # ✅
# Add attestations for supply chain security.
--provenance=true --sbom=true # ✅
# Use the platform arguments in the Dockerfile.
ARG TARGETOS TARGETARCH # ✅
# Use native ARM64 runners when available.
runs-on: ubuntu-24.04-arm # ✅
# Verify with imagetools.
docker buildx imagetools inspect myimage:latest # ✅
❌ Don’t Do This:
# Don't use the default builder for multi-platform.
docker buildx build --platform linux/amd64,linux/arm64 . # ❌ No manifest list
# Don't use --load with multi-platform.
docker buildx build --platform linux/amd64,linux/arm64 --load . # ❌ Not supported
# Don't forget QEMU when the target differs.
# The build fails with an exec format error. # ❌
# Don't hardcode the platform in the Dockerfile.
FROM golang:1.22 # ❌ Uses the target platform, may fail
FROM --platform=$BUILDPLATFORM golang:1.22 # ✅
# Don't skip the attestations in production.
--provenance=false # ❌ Loses the supply chain evidence
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
exec format error | QEMU not installed | Run docker/setup-qemu-action |
| Manifest not created | Default builder | Use docker-container driver |
--load fails | Multi-platform not supported | Use --push |
| Slow build | QEMU emulation | Use native ARM64 runner |
| Wrong base image | Not multi-platform | Use a multi-platform base |
| Cache misses | No cache config | Use --cache-from and --cache-to |
| Attestation missing | Not enabled | Add --provenance and --sbom |
Real-World Examples
1. The Builder Creation
docker buildx create --name multiarch --driver docker-container --use
2. The QEMU Setup
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
3. The Multi-Platform Build
docker buildx build --platform linux/amd64,linux/arm64 --push -t myapp:latest .
4. The Attestations
docker buildx build --platform linux/amd64,linux/arm64 --provenance=true --sbom=true --push .
5. The Build Secret
docker buildx build --platform linux/amd64,linux/arm64 --secret id=npmrc,src=$HOME/.npmrc --push .
6. The Dockerfile
FROM --platform=$BUILDPLATFORM golang:1.22 AS build
ARG TARGETOS TARGETARCH
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /app .
7. The Verification
docker buildx imagetools inspect myapp:latest
8. The Native Runner
runs-on: ubuntu-24.04-arm
9. The GitHub Actions
- uses: docker/setup-qemu-action@v4
- uses: docker/setup-buildx-action@v4
- uses: docker/build-push-action@v7
with:
platforms: linux/amd64,linux/arm64
push: true
10. The Cache
docker buildx build --cache-from type=registry,ref=myapp:cache --cache-to type=registry,ref=myapp:cache,mode=max --push .
Visual
The Manifest List
┌────────────────────────────────────────────────────────────┐
│ THE MANIFEST LIST │
│ │
│ myregistry.example.com/myapp:latest │
│ │ │
│ │ The manifest list points to: │
│ v │
│ ┌──────────────────────────────────────────────────┐ │
│ │ Manifest List │ │
│ │ ├─ linux/amd64 → sha256:abc123... │ │
│ │ ├─ linux/arm64 → sha256:def456... │ │
│ │ └─ linux/arm/v7 → sha256:ghi789... │ │
│ └──────────────────────────────────────────────────┘ │
│ │
│ On an x86_64 host: │
│ docker pull myapp:latest → pulls sha256:abc123... │
│ │
│ On an ARM64 host: │
│ docker pull myapp:latest → pulls sha256:def456... │
│ │
│ One tag. Multiple images. The client selects. │
└────────────────────────────────────────────────────────────┘
The QEMU Emulation
┌────────────────────────────────────────────────────────────┐
│ QEMU EMULATION │
│ │
│ BUILD HOST (x86_64) │
│ ┌──────────────────────────────────────────────────┐ │
│ │ BuildKit container │ │
│ │ └─ QEMU emulator │ │
│ │ └─ ARM64 binary runs under emulation │ │
│ │ │ │
│ │ The ARM64 instructions are translated to │ │
│ │ x86_64 instructions at runtime. │ │
│ └──────────────────────────────────────────────────┘ │
│ │
│ The result is an ARM64 image built on x86_64. │
│ Slower than native, but no extra hardware. │
└────────────────────────────────────────────────────────────┘
The Build Platform Arguments
┌────────────────────────────────────────────────────────────┐
│ BUILD vs TARGET PLATFORM │
│ │
│ FROM --platform=$BUILDPLATFORM golang:1.22 AS build │
│ ┌──────────────────────────────────────────────────┐ │
│ │ The build stage runs on the BUILD platform. │ │
│ │ It can run native tools for the build host. │ │
│ └──────────────────────────────────────────────────┘ │
│ │
│ ARG TARGETOS TARGETARCH │
│ ┌──────────────────────────────────────────────────┐ │
│ │ The target arguments tell the compiler what │ │
│ │ architecture to produce. │ │
│ │ │ │
│ │ GOOS=$TARGETOS GOARCH=$TARGETARCH go build │ │
│ └──────────────────────────────────────────────────┘ │
│ │
│ FROM alpine:3.19 │
│ ┌──────────────────────────────────────────────────┐ │
│ │ The runtime stage uses the TARGET platform. │ │
│ │ The final image is for the target architecture. │ │
│ └──────────────────────────────────────────────────┘ │
└────────────────────────────────────────────────────────────┘
The Registry Storage
┌────────────────────────────────────────────────────────────┐
│ REGISTRY STORAGE │
│ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ Registry │ │
│ │ ├─ myapp:latest → manifest list │ │
│ │ ├─ myapp:latest@sha256:abc → amd64 image │ │
│ │ ├─ myapp:latest@sha256:def → arm64 image │ │
│ │ ├─ myapp:latest@sha256:ghi → arm/v7 image │ │
│ │ ├─ myapp:latest.sig → signature │ │
│ │ ├─ myapp:latest.att → SBOM attestation │ │
│ │ └─ myapp:latest.sbom → provenance attestation │ │
│ └──────────────────────────────────────────────────┘ │
│ │
│ The manifest list is the entry point. │
│ The platform images are the content. │
│ The attestations are the supply chain evidence. │
└────────────────────────────────────────────────────────────┘
Summary
| Item | Value |
|---|---|
docker buildx | The CLI plugin for multi-platform builds |
docker-container driver | The required driver |
| QEMU | The emulator for cross-architecture builds |
multiarch/qemu-user-static | The QEMU registration image |
| Manifest list | The registry object that maps tags to platforms |
--platform | The target architecture flag |
--push | The required output for multi-platform |
$BUILDPLATFORM | The build host platform |
$TARGETARCH | The target architecture |
| Native runner | Faster than QEMU emulation |
Key takeaways:
- A multi-platform image is a manifest list, not a single image. The registry stores one image per platform, and the manifest list maps the tag to each. The Docker client selects the matching image for the host’s architecture .
- The
docker-containerdriver is required. The default builder does not support multi-platform builds. A new builder with thedocker-containerdriver must be created and bootstrapped . - QEMU enables cross-architecture builds. The
multiarch/qemu-user-staticimage registers the emulators with the kernel, allowing the build host to execute binaries for other architectures . - The
--pushflag is required for multi-platform. The local Docker image store does not support manifest lists. The build result must be pushed to a registry . - The
$BUILDPLATFORM,$TARGETOS, and$TARGETARCHarguments are automatic. Buildx sets them, and the Dockerfile uses them to distinguish the build platform from the target platform . - Native ARM64 runners are faster than QEMU. GitHub’s
ubuntu-24.04-armrunner builds ARM64 images without emulation. The trade-off is availability and cost . - Attestations are part of the multi-platform build. The
--provenanceand--sbomflags attach attestations to the manifest list. They are covered by the signature and provide the supply chain evidence . - The base image must be multi-platform. The
node:22-alpine,postgres:16, andpython:3.12-alpine3.23images are multi-platform and cover the common architectures . - The build cache is shared across platforms. The
--cache-fromand--cache-toflags use a registry cache that is shared across runners, which makes CI builds faster .
Remember: Multi-platform builds produce a single image reference that resolves to different binaries on different architectures. The docker buildx command creates the manifest list, and the docker-container driver is required. QEMU emulates the target architecture when the build host cannot natively compile for it. The $BUILDPLATFORM and $TARGETARCH arguments let the Dockerfile distinguish the build platform from the target platform. The --push flag sends the manifest list to the registry, and the attestations record the provenance and the SBOM. The result is a docker pull that works everywhere, and a build pipeline that produces the right binary for every platform.
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!