未在Dockerfile的ENV指令中设置$ PWD [英] $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指令不会启动外壳程序.只需在图像元数据中添加或更新环境变量的当前列表即可.
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"
文档中的其他信息:
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.
这篇关于未在Dockerfile的ENV指令中设置$ PWD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!