+-
在Dockerfile中的ENV指令中未设置$PWD
我有一个Dockerfile像这样开始:

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被正确扩展.

此外,RUN中的$PWD没有问题(在这种情况下打印/某些/路径)

所以给定Dockerfile的输出将是:

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

有人能告诉我为什么$PWD如此特别?我想这可能与WORKDIR的行为有关,但我对此没有任何线索.

最佳答案
PWD是一个在shell中设置的特殊变量.当docker RUN使用这个形式sh -c’thing’时,通过ENV指令传递预定义的环境变量,其中PWD不在该列表中(使用docker inspect< image-id>查看).

ENV指令不会启动shell.只需添加或更新图像元数据中的当前env vars列表.

我会写你的Dockerfile:

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:

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 - 乐贴网