Sub topic 5
Selecting target platforms with --platform
This chapter assumes you can run docker build, have a Buildx builder available (see the earlier chapters on Buildx and builders), and know that an image is tied to a CPU architecture.
What a platform string is
The --platform flag tells BuildKit which operating system and CPU architecture the resulting image must run on. A platform string has the form os/arch or os/arch/variant.
| Platform string | Meaning |
|---|---|
| `linux/amd64` | 64-bit x86 Linux (Intel and AMD) |
| `linux/arm64` | 64-bit ARM Linux (Apple Silicon hosts, AWS Graviton) |
| `linux/arm/v7` | 32-bit ARM, variant 7 (many Raspberry Pi models) |
| `linux/386` | 32-bit x86 Linux |
The variant matters only for architectures that have several incompatible revisions, mainly 32-bit ARM.
Passing one or several platforms
Without --platform, Buildx builds for the platform of the builder's host. To pick targets, pass a comma-separated list:
docker buildx build --platform linux/amd64,linux/arm64 -t app:dev .You can also repeat the flag, which has the same effect:
docker buildx build --platform linux/amd64 --platform linux/arm64 -t app:dev .BuildKit runs one build per listed platform. It then combines the results into a manifest list, which the earlier chapter described.
Which platforms a builder offers
Each builder reports the platforms it can target. List them with:
docker buildx lsThe PLATFORMS column shows native platforms first. Platforms that appear only after you enable QEMU (next chapter) are emulated. Asking for a platform the builder does not list fails at build time.
Limits of the default driver
Multi-platform builds depend on the builder's driver and on where the result goes.
- The default
dockerdriver can build only one platform at a time. Requesting several gives the errormultiple platforms feature is currently not supported for docker driver. Create a builder with thedocker-containerdriver instead. - A classic Docker image store cannot hold a manifest list. With
--load, you can load only one platform. Use--pushto send all platforms to a registry. Docker Engine with the containerd image store can keep multi-platform images locally.
Platform values inside the Dockerfile
BuildKit exposes the platforms as predefined build arguments. You declare them with ARG before use.
| Argument | Value |
|---|---|
| `TARGETPLATFORM` | Platform being built, such as `linux/arm64` |
| `TARGETOS`, `TARGETARCH`, `TARGETVARIANT` | The parts of the target platform |
| `BUILDPLATFORM` | Platform of the builder running the build |
This Dockerfile prints them, so you can see how they change per target:
FROM alpine:3.20
ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "building on $BUILDPLATFORM for $TARGETPLATFORM"Build it with --progress=plain --no-cache and two platforms. The output shows one RUN line per target, each with the same BUILDPLATFORM and a different TARGETPLATFORM. The cross-compilation chapter uses these arguments to avoid emulation.
Common mistakes
- Writing
arm64orx86_64instead oflinux/arm64orlinux/amd64. Use the Go-style architecture names that Docker expects. - Using
linux/armwith no variant when you mean 32-bit ARMv7. Writelinux/arm/v7. - Expecting the base image to exist for every platform. If
FROMnames an image with no variant for a target, the build fails for that platform.
Exercise
You run docker buildx build --platform linux/amd64,linux/arm64 --load -t app:dev . on a machine with the classic Docker image store. The build fails. Give two ways to fix it.
Show answer
The classic image store cannot hold a manifest list, so --load fails with several platforms. Either replace --load with --push and send the image to a registry, or build one platform at a time, for example --platform linux/arm64 --load. If the error mentions the docker driver, create a builder with the docker-container driver first.