I want to pass in an optional argument to my docker file to determine the platform. If the platform is not available, I want docker to use the docker defaults.
Here is an example dockerfile that has a required argument:
ARG DOCKER_PLATFORM
FROM --platform=${DOCKER_PLATFORM} python:3.9-buster
RUN apt-get update
RUN apt-get install -y postgresql-client
...etc
Here is what I actually want to do:
ARG DOCKER_PLATFORM=
IF ${DOCKER_PLATFORM != ""
FROM --platform=${DOCKER_PLATFORM} python:3.9-buster
ELSE:
FROM python:3.9-buster
RUN apt-get update
RUN apt-get install -y postgresql-client
...etc
Can I have an conditional in docker? Is there anther way to remove this conditional?
Docker "FROM" docs that explain the --platform
flag: https://docs.docker.com/engine/reference/builder/#from
Note that $BUILDPLATFORM
evaluates to an empty string, which is not an acceptable item to pass into --platform. I suppose that means I am not using BuildKit.
Docker provides several Automatic platform ARGs in the global scope, which include a BUILDPLATFORM
and a TARGETPLATFORM
. In principle you could use one of these as a default value
ARG DOCKER_PLATFORM=$TARGETPLATFORM
FROM --platform=$DOCKER_PLATFORM python:3.9
However, the default FROM --platform
is the target platform, and the docker build
--platform
option sets this. So rather than using a custom ARG
here, it may be enough for you to use the default platform
# without a --platform option
FROM python:3.9
and if you do need an alternate platform, specify it when you build
docker build --platform=linux/amd64 ...
I am not using BuildKit.
BuildKit has been a non-experimental option since Docker 18.09; if it's not on by default, try setting an environment variable DOCKER_BUILDKIT=1
when you docker build
.