$PWD 未在 Dockerfile 的 ENV 指令中设置 [英] $PWD is not set in ENV instruction in a Dockerfile
问题描述
我有一个像这样开始的 Dockerfile:
I have a Dockerfile starts like this:
FROM ubuntu:16.04
WORKDIR /some/path
COPY . .
ENV PYTHONUSERBASE=$PWD/pyenv PATH=$PWD/pyenv/bin:$PATH
RUN echo "PWD is: $PWD"
RUN echo "PYENV is: $PYTHONUSERBASE"
我发现运行docker build
时没有设置$PWD
(或${PWD}
),作为比较,$PATH
已正确展开.
I found $PWD
(or ${PWD}
) was not set when I run docker build
, as a comparison, $PATH
was correctly expanded.
另外,RUN
中的$PWD
没有问题(本例打印/some/path
)
Moreover, $PWD
in RUN
has no problem (It prints /some/path
in this case)
所以给定 Dockerfile 的输出将是:
So the output of the given Dockerfile would be:
PWD is: /some/path
PYENV is: /pyenv
谁能告诉我为什么 $PWD
如此特别?我想这可能与 WORKDIR
的行为有关,但我对此一无所知.
Could somebody tell me why $PWD
is so special? I guess it may be related to the behaviour of WORKDIR
but I have no clue about that.
推荐答案
PWD 是一个在 shell 中设置的特殊变量.当 docker RUN 使用这种形式 sh -c 'something'
执行某些操作时,从 ENV 指令中传递预定义的环境变量,其中 PWD 不在该列表中(使用 docker 查看它检查 <image-id>
).
PWD is an special variable that is set inside a shell. When docker RUN something it does that with this form sh -c 'something'
, passing the pre-defined environment variables from ENV instructions, where PWD is not in that list (see it with docker inspect <image-id>
).
ENV 指令不会启动 shell.只需在图像元数据中添加或更新当前环境变量列表.
ENV instructions does not launch a shell. Simply add or update the current list of env vars in the image metadata.
我会这样写你的 Dockerfile:
I would write your Dockerfile as this:
FROM ubuntu:16.04
ENV APP_PATH=/some/path
WORKDIR $APP_PATH
COPY . .
ENV PYTHONUSERBASE=$APP_PATH/pyenv PATH=$APP_PATH/pyenv/bin:$PATH
RUN echo "PWD is: $PWD"
RUN echo "PYENV is: $PYTHONUSERBASE"
docs 中的更多信息:
WORKDIR 指令为 Dockerfile 中任何 RUN、CMD、ENTRYPOINT、COPY 和 ADD 指令设置工作目录.如果 WORKDIR 不存在,即使在任何后续 Dockerfile 指令中都没有使用,也会创建它.
The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.
这篇关于$PWD 未在 Dockerfile 的 ENV 指令中设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!