Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

docker ENV vs RUN export

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
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 on the same container.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...