tueda
tueda

Reputation: 982

Proper way to append paths to PATH-like variables in Dockerfile

What is the correct way to append a path to a variable in a Dockerfile without triggering any warnings, even if the variable is undefined?

Suppose I want to append a path to a PATH-like environment variable, which may be undefined in the base image (assuming a Linux-based image):

ARG BASE_IMAGE=ubuntu:22.04
FROM $BASE_IMAGE
ENV PATH1=$PATH1:/new-path1

When running docker build, if PATH1 is defined in the base image, it correctly appends the new path to PATH1. However, if PATH1 is not defined in the base image, it gives the following warning:

#1 WARN: UndefinedVar: Usage of undefined variable '$PATH1' (did you mean $PATH?) (line 3)

I tried using Environment replacement to avoid this warning, like this:

ENV PATH1=${PATH1:-}:/new-path1

but it also gave the same warning.

Upvotes: 2

Views: 929

Answers (1)

According to my digging in the documentation, UndefinedVar is Build Check.

According to the docs:

Build checks are supported in: Buildx version 0.15.0 and later

According to changelog:

0.15.0 2024-06-11 The full release note for this release is available on GitHub.

New --call option allows setting evaluation method for a build, replacing the previous experimental --print flag. docker/buildx#2498, docker/buildx#2487, docker/buildx#2513

In addition to the default build method, the following methods are implemented by Dockerfile frontend:

--call=check: Run validation routines for your build configuration. For more information about build checks, see Build checks

So this is a relatively new addition and doesn't have a lot of feedback.

Of course, you can turn it off with check=skip skip-checks

But I cannot find proper way about fixing it neither in docs nor here and related (old) question doesn't have these problems. I've created my own issue.

In our case, we are using PYTHONPATH variable:

PYTHONPATH="$SOFT/Stranger-${STRANGER_VERSION}/local/lib/python3.10/dist-packages:$PYTHONPATH"

And we got same warning: UndefinedVar: Usage of undefined variable '$PYTHONPATH' (line 72)

I've tried

PYTHONPATH="$SOFT/Stranger-${STRANGER_VERSION}/local/lib/python3.10/dist-packages:${PYTHONPATH:-''}"

Doesn't help.


Docker devs answered in issue that docker build checks base image for presence of variable and warns only if it is undeclared. For now, they don't have inline ignores, but have plans for it. No info about working workarounds.

Upvotes: 3

Related Questions