问题描述
我已经搜索了一些问题,例如 docker ENV vs RUN export ,它解释了这些命令之间的差异,但无助于解决我的问题.
I've search some of the questions already like docker ENV vs RUN export, which explains differences between those commands, but didn't help in solving my problem.
例如,我有一个名为 myscript
的脚本:
For example I have a script called myscript
:
#!/bin/bash
export PLATFORM_HOME="$(pwd)"
在Dockerfile中有以下几行:
And have following lines in Dockerfile:
...
COPY myscript.sh /
RUN ./myscript.sh
我还试图在调用脚本之前使用ENTRYPOINT而不是RUN或声明变量,所有这些都没有成功.
I've also tried to use ENTRYPOINT instead of RUN or declaring variable before calling the script, all that with no success.
我要实现的是,可以从使用该代码作为基础的其他Dockerfile中引用 PLATFORM_HOME
.怎么做?
What I want to achieve is that PLATFORM_HOME
can be referenced from other Dockerfiles which use that one as a base. How to do it ?
推荐答案
无法将变量从脚本导出到子图像.通常,环境变量向下传播,从不向上传播.
There's no way to export a variable from a script to a child image. As a general rule, environment variables travel down, never up to a parent.
ENV
将保留在构建中环境以及子图像和容器.
ENV
will persist in the build environment and to child images and containers.
Dockerfile
FROM busybox
ENV PLATFORM_HOME test
RUN echo $PLATFORM_HOME
Dockerfile.child
FROM me/platform
RUN echo $PLATFORM_HOME
CMD ["sh", "-c", "echo $PLATFORM_HOME"]
建立父母
docker build -t me/platform .
然后建立孩子:
→ docker build -f Dockerfile.child -t me/platform-test .
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM me/platform
---> 539b52190af4
Step 2/3 : RUN echo $PLATFORM_HOME
---> Using cache
---> 40e0bfa872ed
Step 3/3 : CMD sh -c echo $PLATFORM_HOME
---> Using cache
---> 0c0e842f99fd
Successfully built 0c0e842f99fd
Successfully tagged me/platform-test:latest
然后运行
→ docker run --rm me/platform-test
test
这篇关于docker运行脚本,导出环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!