Docker can run a different program depending on the host CPU architecture in two main ways: bake one multi-architecture image with per-architecture binaries and select between them at container startup, or publish a multi-platform image whose manifest points each platform at a different build. For most applications the startup-selection approach requires no build infrastructure beyond BuildKit, while the manifest approach gives you a cleaner docker pull experience. The key condition either way is that every architecture you intend to support must have its executable physically present in the image layer or be downloadable and verifiable inside it, because Docker will not translate a foreign binary at exec time unless the kernel and runtime support it.

This article walks through both methods with concrete Dockerfile patterns, explains the environment signals Docker exposes, covers the pitfalls that make a working local build fail on a different machine, and shows how to test and verify that the right binary actually ran.

What "architecture-dependent execution" actually means in Docker

When you run docker run myimage, the daemon resolves the image name to a manifest, picks the entry that matches the host platform (OS and architecture), and starts the container with that filesystem. The architecture that matters is the one the container's kernel and userspace see, not necessarily the machine you typed the command on. Under Apple Silicon with Rosetta emulation, an linux/amd64 container sees x86_64 even though the laptop is arm64. On a Windows host with WSL2, the container's architecture is whatever the WSL distribution and image declare.

That distinction drives the whole problem. "Change the executable depending on architecture" can mean three different things:

The container image bundles several compiled binaries (for amd64, arm64, maybe 386) and picks one when the container starts. Separate images exist per architecture, and the registry manifest routes a pull to the correct one. A single image downloads or assembles the right binary at runtime based on detected architecture.

Each approach solves a different constraint. Startup selection keeps one image tag and one pull for everyone but bloats the image. Multi-platform manifests keep each image small and fast to pull but require build tooling that can produce multiple platforms. Runtime download keeps the image tiny but adds network dependency and verification risk at container start.

The signals Docker gives you for architecture

Before writing any branching logic, know what you can detect. Inside a container, the following are reliable:

uname -m reports the kernel machine type as seen by the container: x86_64, aarch64, armv7l, s390x, and so on. This is the most direct signal. TARGETARCH is a BuildKit-provided build argument during docker build. Valid values include amd64, arm64, arm, 386, ppc64le, s390x. BuildKit sets it automatically when you build with --platform, and it becomes available only if you declare ARG TARGETARCH in the stage where you use it. TARGETOS, TARGETVARIANT, and BUILDPLATFORM accompany it and are equally useful: TARGETVARIANT=v7 distinguishes 32-bit ARM variants.

One trap: uname -m inside an emulated container reports the emulated architecture, which is what you want, but docker info or the host's arch command reports the host. Never branch on host values inside a container. Also avoid dpkg --print-architecture or apk --print-arch unless you know the base image's package manager is present; they work but add a dependency and occasionally disagree with the kernel on edge cases like armhf versus armv7l. If you want a single robust check, use uname -m and normalize the result in a shell case statement.

Method 1: One image, select the binary at startup

This is the most portable approach and the least demanding on build infrastructure. The idea is to copy every architecture's binary into the image at known paths, then have the entrypoint inspect uname -m and exec the matching file.

Prerequisites

You need cross-compiled artifacts already built for each target architecture, or a build stage that can produce them. A Go project with GOARCH=amd64 and GOARCH=arm64 is the simplest case. For interpreted languages like Python or Node, the "executable" may be a shell script that launches the same interpreter, in which case the branching is often about a native addon or a bundled tool rather than the interpreter itself. Finally, confirm BuildKit is available—Docker Engine 23 and later enables it by default, and on older versions you set DOCKER_BUILDKIT=1.

Step-by-step

1: Build each architecture's artifact outside or inside the Dockerfile. Keeping builds outside the image keeps the final image lean, but multi-stage builds with --platform=$BUILDPLATFORM and cross-compilation flags are usually simpler to reproduce.

2: Lay the binaries out in predictable directories, one per architecture, for example /app/bin/amd64/server and /app/bin/arm64/server.

3: Write an entrypoint script that maps uname -m to the directory name and execs the binary. Use exec so the program becomes PID 1 and receives signals correctly; without it, docker stop can hang until the timeout kills the wrapper.

4: Set the entrypoint to the script, not the binary.

5: Test on both architectures, including under emulation, and confirm the expected binary ran by printing its architecture.

A working entrypoint looks like this:

The Dockerfile portion is equally short. During build, copy both binaries:

If you want to verify quickly, run docker run --rm --platform linux/arm64 myimage uname -m and then docker run --rm --platform linux/amd64 myimage /app/bin/amd64/server --version. The first shows the container's view; the second confirms the binary executes.

Success check and limits

A successful setup prints the expected architecture from inside the container and exits cleanly. A common failure is a "no such file or directory" error that looks like a missing file but is actually the dynamic loader complaining that the binary's interpreter (/lib/ld-linux-aarch64.so.1, for example) is absent. That happens when you copy a binary compiled for one architecture into an image whose userspace libraries belong to another. Static linking with CGO_ENABLED=0 avoids it for Go; Rust and C projects need matching glibc or musl. The stop condition is a binary that runs on one platform but errors on another despite correct paths—that's a toolchain or library mismatch, not an entrypoint bug, and it needs a rebuild rather than more shell logic.

The main drawback of Method 1 is image size: every architecture's binary ships to every host. If each build is 80 MB, an amd64 host still downloads the arm64 copy. For small binaries this is negligible; for large ones it can double or triple pull time.

Method 2: Separate per-architecture images behind one tag

The cleaner long-term approach is to build each architecture as its own image and publish a manifest list under a single tag. When a client pulls myimage:1.0, the registry hands back the entry for the requesting platform, and the container never contains foreign binaries.

Prerequisites and actions

1: Enable BuildKit or use docker buildx, which is required for multi-platform output with Docker Engine.

2: Create a builder that supports multiple platforms: docker buildx create --name multiarch --use, then docker buildx inspect --bootstrap.

3: Declare ARG TARGETARCH in the Dockerfile and use it to choose the artifact. Cross-compile with the same argument so the build stage produces the right binary. In a Go multi-stage build, the pattern is RUN GOARCH=$TARGETARCH go build -o /out/server ./cmd/server.

4: Build and push in one step with docker buildx build --platform linux/amd64,linux/arm64 -t registry.example.com/myimage:1.0 --push .. The --push flag is required for manifest lists; a --load output only supports a single platform at a time in most configurations.

5: Verify the manifest with docker buildx imagetools inspect registry.example.com/myimage:1.0 and confirm two platform entries appear.

Once published, every docker run pulls the right variant automatically. There is no runtime branching and no wasted bytes.

The catch with TARGETARCH

TARGETARCH is only populated when you declare it in the stage that uses it, and only under BuildKit. If you forget the ARG, the variable is empty and your case statement falls through to a default—sometimes silently building the wrong binary. A defensive Dockerfile checks for emptiness:

Another subtlety: when you build without --platform, BuildKit defaults to the host's platform, so a local docker build on an arm64 Mac produces only an arm64 image. That's fine for local testing but means CI must always pass explicit platforms. If your CI runner is amd64 and you forget the flag, you'll publish an image that fails to pull on Apple Silicon without any error at build time.

Mixing the approaches: hybrid patterns that often work best

A common real-world pattern combines both. The image contains a single architecture-specific binary produced by a multi-platform build, but a small set of optional tools (a debugger, a migration utility, or a vendored CLI that's hard to cross-compile) is bundled for all architectures and selected at startup. This keeps the primary binary small while handling the awkward extras.

Another hybrid is to build a multi-platform image whose startup script still validates architecture. That seems redundant, but it catches the case where someone tags a single-platform image for a different platform, or where an orchestrator forces --platform to a value the image doesn't contain. The script fails fast with a clear message rather than executing a binary the kernel can't load.

For interpreted stacks, branching usually isn't about the interpreter at all. A Python image is python:3.12-slim per platform, and the manifest handles routing. What changes is a native wheel or a compiled extension. In that case Method 2 handles the base, and Method 1 handles the one component that needs it.

Emulation versus native execution

Docker Desktop and recent Docker Engine releases can run foreign-architecture images through QEMU emulation, typically registered via binfmt_misc. That's invaluable for testing Method 1 and Method 2 on a single machine, but it changes what you're observing. An emulated arm64 container on an amd64 host reports aarch64 from uname -m and runs arm64 binaries—but slowly, and occasionally with subtle differences in floating-point behavior or timing-sensitive code. Use emulation for correctness checks and entrypoint testing, not for benchmarks.

If you see exec format error when running a foreign image, emulation isn't registered on that host. On Linux you can install it with the qemu-user-static package or via docker run --privileged --rm tonistiigi/binfmt --install all, a standard BuildKit helper image. On Docker Desktop it's built in. Do not rely on emulation in production; it works for many workloads but adds latency and occasionally breaks syscall-heavy software.

Testing the branch without guessing

A branch you never tested is a branch that will fail in production. Build a small test matrix:

Run the image with --platform linux/amd64 and --platform linux/arm64 and capture uname -m plus the entrypoint's chosen path. If you ship a script, add a --dry-run or VERBOSE flag that prints the selected directory before exec. In CI, run the image on native runners for each architecture rather than emulated ones. GitHub Actions, GitLab, and most hosted CI now offer arm64 runners; using them catches linker and library problems that emulation hides.

A quick one-liner to confirm the entrypoint resolves correctly on an emulated host is to override the entrypoint with sh and source the script in a mode that prints rather than execs. If your script supports an environment variable like DRY_RUN=1, this is trivial; if not, add it, because the cost of one conditional is far lower than debugging a silent wrong-binary selection.

Common failure modes and how to recognize them

Several problems account for most of the confusion in this area, and each has a distinct signature.

Symptom Likely cause Fix
exec format error at start Ran a foreign-arch binary without emulation Register binfmt, or ship the correct binary
no such file or directory despite file existing Dynamic loader for the wrong architecture Static-link, or match base image glibc
Wrong binary runs silently Empty TARGETARCH or unmatched uname -m case Declare ARG, add a default that exits nonzero
Image pulls on one machine, fails on another Single-platform tag, not a manifest list Build with buildx --platform ... --push
Container ignores docker stop Entrypoint didn't exec Replace wrapper with exec "$binary" "$@"
Build succeeds locally, CI image broken CI omitted --platform Always pass explicit platforms in CI

The silent wrong-binary case is the nastiest because nothing errors. If your entrypoint's case statement has no default that exits, an unknown architecture falls through and may run a binary meant for another platform, which then crashes later with a confusing error from deep inside the application. Always end the case with an explicit *) arm that prints uname -m and exits nonzero.

One more pitfall specific to TARGETARCH: it reflects the build target, not the build runner. A build stage that runs on $BUILDPLATFORM and cross-compiles for $TARGETARCH must use the target value in compiler flags and the build platform value only for the toolchain itself. Swapping them produces an image labeled arm64 containing an amd64 binary, and the manifest will happily deliver it to arm64 hosts, which then fail at exec.

When to choose each approach

Startup selection (Method 1) fits when you can't or won't run multi-platform builds—air-gapped environments, a registry that doesn't support manifest lists, or CI that lacks buildx. It also fits when the different architectures need genuinely different runtime behavior beyond just a binary swap, such as different default config files or launch flags. The cost is image size and the need for a tested shell branch.

Multi-platform manifests (Method 2) fit almost everything else. The pull is smaller for each host, there's no runtime branch to test, and the registry does the routing reliably. The cost is build tooling: you need buildx, explicit --platform in CI, and a registry that supports manifest lists (Docker Hub, GHCR, ECR, GCR, and Harbor all do). For containerized production workloads published to a registry, this is the default recommendation.

Runtime download—fetching the architecture-specific binary at container start—should be a last resort. It introduces a network dependency on every start, complicates air-gapped operation, and requires checksum verification to be safe. It makes sense only when the binary is huge and the image must stay tiny, or when the binary is updated independently of the image. Even then, pin a checksum per architecture and verify it before exec, because an unverified download at startup is a supply-chain risk that a stable image tag would have avoided.

A worked decision for a typical Go service

Suppose you have a Go HTTP service, about 25 MB per binary, deployed to both amd64 cloud VMs and arm64 edge devices, and published to a private registry that supports manifest lists. The right build is Method 2:

A multi-stage Dockerfile with FROM --platform=$BUILDPLATFORM golang:1.23 AS build, ARG TARGETARCH, and RUN GOARCH=$TARGETARCH CGO_ENABLED=0 go build -trimpath -o /out/server ./cmd/server. A final stage FROM gcr.io/distroless/static-debian12 (which is per-platform and supports both) with COPY --from=build /out/server /server and ENTRYPOINT ["/server"]. CI runs docker buildx build --platform linux/amd64,linux/arm64 -t registry.example.com/svc:1.0 --push .. Deployment uses the same tag everywhere; each host pulls its matching layer set.

The binary never branches at runtime, the image is roughly 25 MB plus the base layer on every host, and correctness is verified once by imagetools inspect. If the private registry did not support manifests, you'd fall back to Method 1 with /app/bin/amd64/server and /app/bin/arm64/server, accept roughly 50 MB per image, and test the entrypoint on both architecture runners. That trade-off—registry capability versus image size and a tested shell branch—is the decision point most teams actually face.