Don't warn on missing defaults for ARG's

I use ARG to manage distribution versions (e.g. Debian trixie vs. bookworm vs. …)

I don’t want to hardcode any particular default value in my Dockerfile. That creates churn, and increases the risk of accidents.

I do want Docker to error in the event that the value is not supplied by --build-arg’s.

Please follow more UNIX idioms, such as POSIX sh set -u. Fewer misleading warnings, more reliable runtime behavior.

Someone’s inevitably going to complain about breaking changes. So perhaps create BUILDARG with these safer semantics, and present a warning where ARG is used.

1 Like

I’m not sure I get the exact problem but based on this sentence

I would recommend using the syntax that you could use in a shell too

ARG UBUNTU_VERSION

FROM ubuntu:${UBUNTU_VERSION:?}

ENV TEST=env

That will result in

Dockerfile:3
--------------------
   1 |     ARG UBUNTU_VERSION
   2 |
   3 | >>> FROM ubuntu:${UBUNTU_VERSION:?}
   4 |
   5 |     ENV TEST=env
--------------------
ERROR: failed to build: failed to solve: failed to process "ubuntu:${UBUNTU_VERSION:?}": UBUNTU_VERSION: is not allowed to be unset

You can also add custom error messages

ARG UBUNTU_VERSION
ARG UBUNTU_VERSION_UNSET="UBUNTU_VERSION is required. Set a value using --build-arg"
FROM ubuntu:${UBUNTU_VERSION:?$UBUNTU_VERSION_UNSET}

ENV TEST=env

Output:

Dockerfile:3
--------------------
   1 |     ARG UBUNTU_VERSION
   2 |     ARG UBUNTU_VERSION_UNSET="UBUNTU_VERSION is required. Set a value using --build-arg"
   3 | >>> FROM ubuntu:${UBUNTU_VERSION:?$UBUNTU_VERSION_UNSET}
   4 |
   5 |     ENV TEST=env
--------------------
ERROR: failed to build: failed to solve: failed to process "ubuntu:${UBUNTU_VERSION:?$UBUNTU_VERSION_UNSET}": UBUNTU_VERSION: UBUNTU_VERSION is required. Set a value using --build-arg

It looks like directly passing the error message with spaces after :? is not supported. It interpreted that as trying to pass multiple arguments to the FROM instruction. I don’t know if it was intended or a bug.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.