Let's say I want combine these commands
RUN command_1
ENV FOO bar
RUN command_2
into
RUN command_1 && export FOO=bar && command_2
and was wondering if setting the variable with RUN export
vs ENV
was equivalent.
In other words, is there a difference between these commands in a Dockerfile?
ENV FOO bar
vs
RUN export FOO=bar
As illustrated by issue 684,
export
won't persist across images. (Don't forget that each Dockerfile directive will generate an intermediate container, committed into an intermediate image: that image won't preserve the exported value)ENV
will:The issue was illustrating that with:
Try it
Create a new dockerfile containing:
Then build it. The output of the last step is:
You can see:
FOO
persists through intermediate containers, thanks to theENV
keyword;BAR
doesn't persist on the next step, because of theexport
command;BAZ
is correctly displayed because the variable is used on the same container.