如何在dockerfile中使用bash配置文件 [英] How to use bash profile in dockerfile

查看:808
本文介绍了如何在dockerfile中使用bash配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to build an image using dockerfile. The commands in the dockerfile looks something like these:

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

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

After these commands, I will use virtualenvwrapper commands to make some virtualenvs.

如果我只有环境变量要在〜/ .bashrc 中处理,我会使用 ARG ENV 进行设置。

If I had only environment variables to deal with in ~/.bashrc, I would have used ARG or ENV to set them up.


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

But now I also have other shell script files like virtualenvwrapper.sh the will be setting some of their own variables.

此外,运行源〜/ .bashrc 不起作用(源不找到。)

Also, RUN source ~/.bashrc is not working (source not found).

我该怎么办?

推荐答案

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

You shouldn't try to edit shell dotfiles like .bash_profile in a Dockerfile. There are many common paths that don't go via a shell (e.g., CMD ["python", "myapp.py"] won't launch any sort of shell and won't read a .bash_profile). If you need to globally set an environment variable in an image, use the Dockerfile ENV directive.

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

For a Python application, you should just install your application into the image's "global" Python using pip install. You don't specifically need a virtual environment; Docker provides a lot of the same isolation capabilities (something you pip install in a Dockerfile won't affect your host system's globally installed packages).

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

A typical Python application Dockerfile (copied from https://hub.docker.com/_/python/) might look like

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"]

最后一个问题, source 是仅某些Shell提供的供应商扩展。 POSIX标准不需要它,默认的<$ c Debian中的$ c> / bin / sh 和Ubuntu没有提供它。无论如何,由于环境变量会在每个 RUN 命令上重置,因此 RUN源... (或更可移植地是<$如果在该 RUN 行中没有其他事件发生,则c $ c> RUN。... )是无操作的。

On your last question, source is a vendor extension that only some shells provide; the POSIX standard doesn't require it and the default /bin/sh in Debian and Ubuntu doesn't provide it. In any case since environment variables get reset on every RUN command, RUN source ... (or more portably RUN . ...) is a no-op if nothing else happens in that RUN line.

这篇关于如何在dockerfile中使用bash配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆