Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpret shell substitution for build arguments #122

Open
plannigan opened this issue Aug 9, 2021 · 1 comment
Open

Interpret shell substitution for build arguments #122

plannigan opened this issue Aug 9, 2021 · 1 comment

Comments

@plannigan
Copy link

Currently build arguments either have the value passed in with build_args in the constructor or the string value assigned in the dockerfile when the arg is defined. However, it is possible to set the value of an arg based on the value of a previous arg using shell substitution.

Here is an example of the current behavior causing issues when using other properties that do variable substitution based on the values of build args.

>>> parser = DockerfileParser()
>>> print(parser.content)
ARG FOO="DEFAULT_FOO"
ARG BAR="__${FOO:-BAR}__"

FROM my-image-${FOO}-${BAR}
>>> parser.baseimage
'my-image-DEFAULT_FOO-__${FOO:-BAR}__'

These examples demonstrate how docker handles the shell substitution.

FROM ubuntu

ARG FOO="DEFAULT_FOO"
ARG BAR="__${FOO:-BAR}__"

RUN echo $FOO $BAR

Examples of building this image:

# No build arguments
$ docker build .
Sending build context to Docker daemon  19.61MB
Step 1/4 : FROM ubuntu
 ---> 1318b700e415
Step 2/4 : ARG FOO="DEFAULT_FOO"
 ---> Running in b904d3d87fcd
Removing intermediate container b904d3d87fcd
 ---> 01a203cae2e4
Step 3/4 : ARG BAR="__${FOO:-BAR}__"
 ---> Running in 4e7d7258da89
Removing intermediate container 4e7d7258da89
 ---> 501fd278a030
Step 4/4 : RUN echo $FOO $BAR
 ---> Running in 8876cf8fd2e6
DEFAULT_FOO __DEFAULT_FOO__
Removing intermediate container 8876cf8fd2e6
 ---> 98b155f169ed

# Set FOO
$ docker build --build-arg FOO=MY_FOO .
Sending build context to Docker daemon  19.61MB
Step 1/4 : FROM ubuntu
 ---> 1318b700e415
Step 2/4 : ARG FOO="DEFAULT_FOO"
 ---> Running in 2bc786748486
Removing intermediate container 2bc786748486
 ---> 3211aa61519a
Step 3/4 : ARG BAR="__${FOO:-BAR}__"
 ---> Running in f61fb4b25a74
Removing intermediate container f61fb4b25a74
 ---> 074f0d3d627f
Step 4/4 : RUN echo $FOO $BAR
 ---> Running in 72f5c0a3c21e
MY_FOO __MY_FOO__
Removing intermediate container 72f5c0a3c21e
 ---> 9f1a5ce8839a
Successfully built 9f1a5ce8839a

# Set BAR
$ docker build --build-arg BAR=MY_BAR .
Sending build context to Docker daemon  19.61MB
Step 1/4 : FROM ubuntu
 ---> 1318b700e415
Step 2/4 : ARG FOO="DEFAULT_FOO"
 ---> Running in fd7ade7ac8ec
Removing intermediate container fd7ade7ac8ec
 ---> d71ad9beb904
Step 3/4 : ARG BAR="__${FOO:-BAR}__"
 ---> Running in c03c4a3511d6
Removing intermediate container c03c4a3511d6
 ---> 1c2301a693ae
Step 4/4 : RUN echo $FOO $BAR
 ---> Running in 6ad10b5754b9
DEFAULT_FOO MY_BAR
Removing intermediate container 6ad10b5754b9
 ---> 421c6ce936e9
Successfully built 421c6ce936e9

Note that each has a different output for step 4.

However, using the library, the substitution is not done.

>>> parser = DockerfileParser()
>>> parser.args
{'FOO': 'DEFAULT_FOO', 'BAR': '__${FOO:-BAR}__'}

>>> parser = DockerfileParser(build_args={"FOO": "MY_FOO"})
>>> parser.args
{'FOO': 'MY_FOO', 'BAR': '__${FOO:-BAR}__'}

>>> parser = DockerfileParser(build_args={"BAR": "MY_BAR"})
>>> parser.args
{'FOO': 'DEFAULT_FOO', 'BAR': 'MY_BAR'}
@twaugh
Copy link
Member

twaugh commented Aug 10, 2021

Feels like something wordexp() could help with but I don't think it is bound in Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants