我正在尝试编写一个设置我的 GOPATH 变量的 Dockerfile 当我稍后使用该变量时,它是空的,例如:
Step 7 : RUN export GOPATH=/root/go
---> Running in 5691396dd55b
---> 17d76f7de05f
Removing intermediate container 5691396dd55b
Step 8 : RUN echo $GOPATH
---> Running in b2cd6ef885d0
---> 54b5a3d5c697
我尝试执行 docker build -no-cache 。但我得到了相同的结果; 有什么想法吗?
最佳答案
您应该使用ENV
在 Dockerfile 中设置环境变量。
请参阅https://docs.docker.com/reference/builder/#env
文档中的附加说明:
Note: Environment persistence can cause unexpected effects. For example, setting
ENV DEBIAN_FRONTEND noninteractive
may confuseapt-get
users on a Debian-based image. To set a value for a single command, useRUN <key>=<value> <command>
.
“”部分很重要,因为没有它,环境变量仅针对发生 RUN 的一层设置,因此对后续层没有影响。 ENV
将保留子层。
关于docker - 无法在 Dockerfile 中设置 shell 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31253455/