Sub topic 5
Declaring platform ARGs where you use them
This chapter assumes you can write a multi-stage Dockerfile and know what BUILDPLATFORM and TARGETPLATFORM mean. It covers one rule that causes many silent cross-compilation bugs. BuildKit defines the platform arguments for you, but a stage can only read them in RUN and other instructions after you declare them with ARG.
Two places the arguments exist
BuildKit predefines these arguments: BUILDPLATFORM, BUILDOS, BUILDARCH, BUILDVARIANT, TARGETPLATFORM, TARGETOS, TARGETARCH and TARGETVARIANT. They are visible in two scopes:
- The global scope, which is everything before the first
FROM. You can use them inFROMlines without declaring anything. - A stage scope, but only after you write
ARG NAMEinside that stage.
The silent failure
If you skip the declaration, the shell inside RUN sees an empty variable. No error appears.
# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM alpine:3.20 AS probe
RUN echo "target arch is [$TARGETARCH]"Build it with docker buildx build --progress=plain --no-cache --platform linux/arm64 . and the output reads target arch is []. Add the declaration and the value appears:
# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM alpine:3.20 AS probe
ARG TARGETARCH
RUN echo "target arch is [$TARGETARCH]"Now the output reads target arch is [arm64].
In a Go build, the empty-variable failure is worse. GOARCH= with an empty value makes go build fall back to the architecture of the build machine. The build succeeds, but every platform gets the same binary.
Each stage declares its own
An ARG is scoped to the stage where it appears. Declaring TARGETARCH in the build stage does nothing for a later stage. The same rule applies to your own arguments that you set before the first FROM:
# syntax=docker/dockerfile:1
ARG GO_VERSION=1.22
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build
ARG GO_VERSION
RUN echo "compiling with Go ${GO_VERSION}"The first ARG makes GO_VERSION usable in the FROM line. The second, a bare ARG GO_VERSION with no value, pulls the global value into the stage. Without it, RUN would print an empty version.
Place the declaration close to its use
Changing an ARG value invalidates the cache for every instruction after the declaration. When you build for two platforms, TARGETARCH differs between the builds. If you declare it at the top of the stage, even go mod download reruns for each platform. Declare it after the steps that do not depend on it:
# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM golang:1.22 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
ARG TARGETOS
ARG TARGETARCH
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /out/app ./cmd/appModule downloads are the same for every target, so they stay cached across platforms. Only the compile step is specific to one architecture.
Quick reference
| Situation | Declaration needed? |
|---|---|
| `FROM --platform=$BUILDPLATFORM image` | No, global scope |
| `RUN` using `$TARGETARCH` | Yes, `ARG TARGETARCH` in that stage |
| A second stage that also uses `$TARGETARCH` | Yes, declare it again in that stage |
| A custom ARG set before the first `FROM` and used in a stage | Yes, a bare `ARG NAME` in the stage |
Exercise
A Dockerfile has FROM --platform=$BUILDPLATFORM golang:1.22 AS build followed by RUN GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /out/app ., with no ARG lines. You build it with --platform linux/arm64 on an amd64 machine. The build succeeds, but the binary fails on an arm64 host. Why, and what is the fix?
Show answer
Without ARG TARGETOS and ARG TARGETARCH in the stage, both variables are empty in the shell. Go ignores the empty values and builds for the build machine, which is linux/amd64. Add ARG TARGETOS and ARG TARGETARCH inside the build stage, ideally just before the RUN that compiles.