在 dockerfile 中自定义 ONBUILD 环境

dan*_*ast 3 python docker dockerfile

我稍微修改了这个 Dockerfile以支持我的特定用例:我需要指定我自己的PyPi服务器,我们将在其中发布我们的内部库。这通常可以通过指定pip.conf文件或将命令行选项传递给pip.

我正在尝试这样做:

FROM python:3.5

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

ONBUILD COPY requirements.txt /usr/src/app/

# 1st try: set the env variable and use it implicitely. Does not work!
# ENV PIP_CONFIG_FILE pip.conf
# ONBUILD RUN pip install --no-cache-dir -r requirements.txt

# 2nd try: set once the env variable, just for this command. Does not work!
# ONBUILD RUN PIP_CONFIG_FILE=pip.conf pip install --no-cache-dir -r requirements.txt

# 3rd try: directly configure pip. Works, but these values should not be set in the Dockerfile!
ONBUILD RUN pip install --index-url http://xx.xx.xx.xx:yyyyy --trusted-host xx.xx.xx.xx --no-cache-dir -r requirements.txt

ONBUILD COPY . /usr/src/app
Run Code Online (Sandbox Code Playgroud)

pip.conf的很简单,在外面使用时可以工作Docker

[global]
timeout = 60
index-url = http://xx.xx.xx.xx:yyyyy
trusted-host = xx.xx.xx.xx
Run Code Online (Sandbox Code Playgroud)

链接:

我有以下问题:

  • 为什么ENV不工作?
  • 为什么在RUN命令中显式设置变量不起作用?
  • 是那些问题ONBUILD,或者也许RUN

Raq*_*uel 12

我遇到了同样的问题,我设法修复它,将它添加到 Dockerfile 中:

COPY pip.conf pip.conf
ENV PIP_CONFIG_FILE pip.conf
RUN pip install <my_package_name>
Run Code Online (Sandbox Code Playgroud)

pip.conf 文件具有以下结构:

[global]
timeout = 60
index-url = https://pypi.org/simple
trusted-host = pypi.org
               <my_server_page>
extra-index-url = https://xxxx:yyyy@<my_server_page>:<package_location>
Run Code Online (Sandbox Code Playgroud)

这是我发现 Docker 从 pypi 服务器找到包的唯一方法。我希望这个解决方案是通用的,并能帮助其他有这个问题的人。


Von*_*onC 2

ONBUILD指令 向映像添加一条触发指令,以便稍后在该映像用作另一个构建的基础时执行。

来自文档

在 1.4 之前,ONBUILD指令不支持环境变量,即使与上面列出的任何指令结合使用也是如此。

使用 docker 1.4+ 尝试(如问题 15025中所示)

ONBUILD ENV PIP_CONFIG_FILE pip.conf
Run Code Online (Sandbox Code Playgroud)