1

The problem is i cannot get the docker build arg value in the shell script while running the docker build.

My docker build command:

DOCKER_BUILDKIT=1 docker build --no-cache --progress=plain \ -t test \ --build-arg WHL_PATH=/fake/path \ . 

Dockerfile

ARG WHL_PATH FROM python:3.8.8 COPY test.sh . RUN ./test.sh $WHL_PATH 

and in the test.sh the "$1" is empty...., if in the Dockerfile i put some constant value then i will be able to see that value in the $1, but with docker build arg or set the build arg as ENV VAR are always empty...

Where am i doing wrong, how should i achieve this?

Docker version 20.10.5, build 55c4c88 
0

2 Answers 2

2

Build args are scoped. Before the first FROM step they only apply to the FROM steps to adjust the image you use. Within each stage, an ARG step applies to the remaining steps within that stage. So the fix is to reorder your steps:

FROM python:3.8.8 COPY test.sh . ARG WHL_PATH RUN ./test.sh $WHL_PATH 
Sign up to request clarification or add additional context in comments.

1 Comment

yup, you are right, i really should come here to ask earlier...
1

Oops, i never realised the position of the ARG instruction matters, basically:

  • any ARG before the first FROM can be used in any FROM line

  • any ARG within a build stage (after a FROM) can be used in that build stage

After i moved the ARG WHL_PATH after the line FROM xxx it works perfectly, hope this can save some of your time in the future.

And i was inspired by this answer actually: https://stackoverflow.com/a/50292255/7658313

1 Comment

WOW! This isn't at all confusing! In one dockerfile, the ARG must appear after the FROM statement. While in a different dockerfile, several ARG statements appear before the FROM statement and behave as global variables (addt'l ARG statements duplicate the previous declaration as if to "import" the variable?). No idea why this is working.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.