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

<pre><code>export A=123 B=$A
</code></pre>

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

<p>Step by step this will give:</p>

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

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

CC BY-SA 3.0
2017-03-27T07:49:21.777Z
2017-03-27T07:49:21.777Z
Tensibai
<pre><code>ENV abc=hello
ENV abc=bye def=$abc
ENV ghi=$abc
</code></pre>

<p>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.</p>

CC BY-SA 3.0
2017-03-27T05:46:22.450Z
2017-03-27T05:56:09.540Z
2017-03-27T05:56:09.540Z
shahin
<p>When the following Dockerfile:</p>

<pre><code>ENV A=123 \
    B=$A
RUN echo $B
</code></pre>

<p>is run, it results in:</p>

<pre><code>Step 2/2 : RUN echo $B
 ---&gt; Running in x

 ---&gt; y
Removing intermediate container z
</code></pre>

<p>When two <code>ENV</code> layers are defined:</p>

<pre><code>ENV A=123
ENV B=$A
</code></pre>

<p>it works:</p>

<pre><code>Step 2/2 : RUN echo $B
 ---&gt; Running in x
123
 ---&gt; y
Removing intermediate container z
</code></pre>

<dockerfile>
Why can this ENV not be found when it is defined in one layer using a Dockerfile?
030
# Why can this ENV not be found when it is defined in one layer using a Dockerfile?

(Asked by: username_0 on 2017-03-26)

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

**Tags:** dockerfile


---

## 2 answers

### Answer by username_1 on 2017-03-27. Score: 3 | Accepted answer

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


---

### Answer by username_2 on 2017-03-27. Score: 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.


---

