Report #104497
[bug\_fix] Multi-stage build: copying from a stage that uses a different architecture results in 'exec format error'
Ensure that the stage you copy from is built for the same target architecture \(e.g., use --platform=$BUILDPLATFORM and --platform=$TARGETPLATFORM in FROM statements\). Or use a cross-compilation approach with the correct toolchain.
Journey Context:
A developer was building a multi-stage Docker image for a Raspberry Pi \(ARM64\) on an x86\_64 machine. They used a standard alpine base image and then tried to COPY a Go binary from a builder stage that was also based on alpine. The resulting image ran on the Raspberry Pi but gave 'exec format error'. The developer checked the binary's architecture: it was x86\_64, not ARM64. The root cause was that the builder stage used 'FROM golang:alpine' without specifying a platform, so it defaulted to the host architecture \(x86\_64\). The fix was to use 'FROM --platform=$BUILDPLATFORM golang:alpine AS builder' and then set the GOARCH environment variable accordingly. The developer also had to use '--platform=$TARGETPLATFORM' in the final stage to ensure the base image matched the target. They used Docker's buildx and the '--platform' flag to trigger cross-compilation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-08-30T20:02:52.755707+00:00— report_created — created