$PWD is not set in ENV instruction in a Dockerfile

梦想的初衷 提交于 2021-02-06 02:27:11

问题


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"

I found $PWD (or ${PWD}) was not set when I run docker build, as a comparison, $PATH was correctly expanded.

Moreover, $PWD in RUN has no problem (It prints /some/path in this case)

So the output of the given Dockerfile would be:

PWD is: /some/path
PYENV is: /pyenv

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.


回答1:


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 instructions does not launch a shell. Simply add or update the current list of env vars in the image metadata.

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"

Further info in docs:

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.



来源:https://stackoverflow.com/questions/44452854/pwd-is-not-set-in-env-instruction-in-a-dockerfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!