165

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
User314159
  • 7,733
  • 9
  • 39
  • 63

2 Answers2

269

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 environment variables set using ENV will persist when a container is run from the resulting image.
You can view the values using docker inspect, and change them using docker run --env <key>=<value>.

The issue was illustrating that with:

RUN export PATH=$PATH:/foo/bar # from directly in builder

When I do docker run [img] bash -c 'echo $PATH' it never includes /foo/bar.

Try it

Create a new dockerfile containing:

FROM centos:6
ENV FOO=foofoo
RUN export BAR=barbar
RUN export BAZ=bazbaz && echo "$FOO $BAR $BAZ"

Then build it. The output of the last step is:

Step 4/4 : RUN export BAZ=bazbaz && echo "$FOO $BAR $BAZ"
 ---> Running in eb66196b238d
foofoo  bazbaz

You can see:

  • FOO persists through intermediate containers, thanks to the ENV keyword;

  • BAR doesn't persist on the next step, because of the export command;

  • BAZ is correctly displayed because the variable is used in the same Docker image layer on the same container, since:

    Each instruction in a Dockerfile creates a layer in the image.

    Source

Artfaith
  • 1,183
  • 4
  • 19
  • 29
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • (I guess yes, but:) Does it mean that `docker` e.g. `run` with `--env-file` or `--env` will export those (by these option arguments) variables? Sorry for bumping up on such an old answer but a websearch just brought it up to me. Nice answer btw. – hakre Dec 05 '19 at 21:14
  • @hakre yes, they are appended to the environment: see https://stackoverflow.com/a/49872152/6309 and below. – VonC Dec 05 '19 at 21:26
  • Thanks for the quick reply, but that answer does not show the variables are marked for export. At least not to me. Anyway, I should do my own homework here, e.g. https://superuser.com/q/450868/63279 or https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#export . – hakre Dec 05 '19 at 23:37
5

I think this can be helpful

RUN echo "export FOO=bar" >> /etc/bash.bashrc

Xu Chen
  • 69
  • 1
  • 1
  • 3
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – mohammad mobasher Feb 28 '22 at 21:26
  • 2
    Most paths to running Docker containers don't read `.bashrc` or other shell dotfiles; adding lines to these files usually has no effect. – David Maze Jan 21 '23 at 18:02