Skip to content

Default value on TARGETPLATFORM always overrides #510

@MarcelWaldvogel

Description

@MarcelWaldvogel

What I want to achieve

I have a project that I want to be able to build both with docker build (for compatibility) and docker buildx --platform and which requires TARGETPLATFORM to make a difference in the build process. As TARGETPLATFORM is not set by docker build, the default value option to ARG seemed the right choice.

What I expect

Given the following Dockerfile,

FROM debian:buster-slim
ARG TARGETPLATFORM=linux/amd64
RUN echo $TARGETPLATFORM; exit 1

I expect docker build . on all architectures to output linux/amd64 (the legacy compatibility case).

When running docker buildx --builder docker-multiarch --platform linux/386 ., the output should be linux/386.

What I get

On platform linux/amd64, docker buildx --builder docker-multiarch --platform linux/386 ., outputs linux/amd64 instead of linux/386.

Removing the default value solves this: docker buildx --builder docker-multiarch --platform linux/386 . results in linux/386 with Dockerfile:

FROM debian:buster-slim
ARG TARGETPLATFORM
RUN echo $TARGETPLATFORM; exit 1

However, docker build . results in an empty/undefined TARGETPLATFORM.

(For ARGs set with --build-arg on the command line, defaults behave as expected.)

System setup

  • Ubuntu 20.10
  • Docker 19.03.13
  • Buildx 0.4.1 or 0.5.1 (tested with both)

Workaround

The following Dockerfile results in the expected behavior:

FROM debian:buster-slim
ARG TARGETPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64}
RUN echo $TARGETPLATFORM; exit 1

However, this (a) defeats the purpose of the default value and (b) causes endless debugging sessions, as it violates the documentation of ARG and TARGETPLATFORM.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions