Sub topic 4
The automatic platform arguments BuildKit provides
This chapter assumes you can write a multi-stage Dockerfile and run docker build. It also assumes you know the names amd64 and arm64. It covers the build arguments BuildKit sets for you and how to read their values.
Two platforms, eight arguments
BuildKit is Docker's build engine. On every build it defines two platforms and splits each into parts. The build platform is the machine running the build. The target platform is the one the finished image is meant for. If you pass no --platform flag, the two are the same.
| Argument | Meaning | Example (building for linux/arm/v7 on linux/amd64) |
|---|---|---|
| `BUILDPLATFORM` | Platform of the build machine | `linux/amd64` |
| `BUILDOS` | OS part of the build platform | `linux` |
| `BUILDARCH` | Architecture part of the build platform | `amd64` |
| `BUILDVARIANT` | Variant part of the build platform | empty |
| `TARGETPLATFORM` | Platform the image is built for | `linux/arm/v7` |
| `TARGETOS` | OS part of the target | `linux` |
| `TARGETARCH` | Architecture part of the target | `arm` |
| `TARGETVARIANT` | Variant part of the target | `v7` |
A platform string has the form os/arch or os/arch/variant. The variant is empty for most platforms, including linux/amd64 and linux/arm64. It matters mainly for 32-bit ARM.
How the values flow
BuildKit detects the BUILD* values from the machine, so they describe where compilation actually runs. The TARGET* values come from the flag. With several platforms in one command, BuildKit runs the whole Dockerfile once per target. Each run sees its own TARGET* values.
Reading the values in a build
This Dockerfile prints both platforms:
# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM alpine:3.20 AS probe
ARG BUILDPLATFORM
ARG TARGETPLATFORM
RUN echo "building on $BUILDPLATFORM for $TARGETPLATFORM"Run it with plain progress output so the RUN line is visible:
docker buildx build --platform linux/arm64 --progress=plain --no-cache .On an amd64 machine the output includes building on linux/amd64 for linux/arm64. The FROM --platform=$BUILDPLATFORM line makes the stage's base image match the build machine. The chapter on FROM --platform covers that flag in detail.
Where the arguments are visible
The arguments behave differently in two places:
- In a
FROMline, you can use them with no declaration.FROM --platform=$BUILDPLATFORMworks as written. - Inside a stage, you must declare each one with
ARGbefore use, as the probe does. Without the declaration,$TARGETPLATFORMexpands to an empty string.
The second rule causes the most common error. A Go build with GOARCH=$TARGETARCH and no ARG TARGETARCH runs with GOARCH empty. Go then silently builds for the build machine's architecture, and the image fails at runtime with an exec format error. Declare the arguments in each stage that reads them. A declaration in one stage does not carry into another.
Common mistakes
- Using the old builder. The automatic arguments need BuildKit. Current Docker releases use it by default, and
docker buildx buildalways does. - Comparing against the wrong string.
TARGETPLATFORMislinux/arm64, notarm64. UseTARGETARCHwhen you need only the architecture. - Assuming a variant exists.
TARGETVARIANTis empty forlinux/amd64andlinux/arm64. Shell code that uses it must tolerate an empty value. - Confusing the two sets. A tool that runs during the build, such as a compiler, belongs to the
BUILD*side. The files you produce belong to theTARGET*side.
Exercise
You build with docker buildx build --platform linux/arm/v7 . on an amd64 laptop. Inside a stage you declare ARG TARGETARCH and ARG TARGETVARIANT, then print both. What do you see, and what would you see for BUILDARCH if you declared it too?
Show answer
TARGETARCH is arm and TARGETVARIANT is v7. BUILDARCH is amd64, because it describes the laptop that runs the build, not the target.
Exercise
A stage runs GOOS=$TARGETOS GOARCH=$TARGETARCH go build, but the stage has no ARG lines. The image built for linux/arm64 fails with exec format error on an arm64 host. What is the cause and the fix?
Show answer
Without ARG TARGETOS and ARG TARGETARCH inside the stage, both variables expand to empty strings, so Go builds for the build machine's platform instead of arm64. Add ARG TARGETOS and ARG TARGETARCH to that stage before the RUN line.