When the following Dockerfile:

ENV A=123 \
RUN echo $B

is run, it results in:

Step 2/2 : RUN echo $B
 ---> Running in x

 ---> y
Removing intermediate container z

When two ENV layers are defined:

ENV A=123

it works:

Step 2/2 : RUN echo $B
 ---> Running in x
 ---> y
Removing intermediate container z

The ENV keyword will be translated to export command (builin of the shell usually), if you remove the multi-line declaration you end up with this equivalent:

export A=123 B=$A

What happens here is that when the shell parse the line to give it to export input, A is not yet exported and available as an environment variable.

Step by step this will give:

  • You type export A=123 B=$A
  • The shell parse the line and pass A=123 B= to export's stdin
  • export parse each argument in the form key=value and as such export A to be 123 and B to be empty

On the second form, A is available to the shell when the line is parsed, and as such the resulting export's stdin is B=123

ENV abc=hello
ENV abc=bye def=$abc
ENV ghi=$abc

will result in def having a value of hello, not bye. However, ghi will have a value of bye because it is not part of the same command that set abc to bye.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.