HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Why can this ENV not be found when it is defined in one layer using a Dockerfile?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
thiswhycanlayerfoundonedockerfileusingwhenenv

Problem

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

Solution

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

Code Snippets

export A=123 B=$A

Context

StackExchange DevOps Q#666, answer score: 3

Revisions (0)

No revisions yet.