如何在 dockerfile 中使用 bash 配置文件

sci*_*cho 5 docker dockerfile

我正在尝试使用 dockerfile 构建图像。dockerfile 中的命令如下所示:

FROM ubuntu:16.04
:
:
RUN pip3 install virtualenvwrapper  
RUN echo '# Python virtual environment wrapper' >> ~/.bashrc
RUN echo 'export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3' >> ~/.bashrc
RUN echo 'export WORKON_HOME=$HOME/.virtualenvs' >> ~/.bashrc
RUN echo 'source /usr/local/bin/virtualenvwrapper.sh' >> ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

在这些命令之后,我将使用 virtualenvwrapper 命令来创建一些 virtualenv。

如果我只有环境变量要处理~/.bashrc,我会使用ARGorENV来设置它们。

但现在我还有其他 shell 脚本文件,例如virtualenvwrapper.sh将设置一些自己的变量。

另外,RUN source ~/.bashrc不起作用(未找到来源)。

我应该怎么办?

Dav*_*aze 3

.bash_profile您不应该尝试像在 Dockerfile 中那样编辑 shell 点文件。有许多常见路径不通过 shell(例如CMD ["python", "myapp.py"]不会启动任何类型的 shell 并且不会读取.bash_profile)。如果需要在映像中全局设置环境变量,请使用 DockerfileENV指令。

对于 Python 应用程序,您应该使用pip install. 您并不特别需要虚拟环境;Docker 提供了许多相同的隔离功能(pip installDockerfile 中的某些功能不会影响主机系统全局安装的软件包)。

典型的 Python 应用程序 Dockerfile(从https://hub.docker.com/_/python/复制)可能看起来像

FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "./your-daemon-or-script.py"]
Run Code Online (Sandbox Code Playgroud)

关于你的最后一个问题,source是只有某些 shell 提供的供应商扩展;POSIX标准不需要它,/bin/shDebian 和 Ubuntu 中的默认设置也不提供它。在任何情况下,由于环境变量在每个RUN命令上都会重置,如果该行中没有发生其他情况, RUN source ...(或更可移植RUN . ...)是无操作RUN