3

When the following Dockerfile:

ENV A=123 \
    B=$A
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
ENV B=$A

it works:

Step 2/2 : RUN echo $B
 ---> Running in x
123
 ---> y
Removing intermediate container z
030
  • 13,235
  • 16
  • 74
  • 173

2 Answers2

3

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

Tensibai
  • 11,366
  • 2
  • 35
  • 62
0
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.

shahin
  • 111
  • 1