在dockerfile中自定义ONBUILD环境 [英] Customize ONBUILD environment in a dockerfile
问题描述
我正在稍微修改此Dockerfile 以支持我的特定用例:我需要指定我自己的 PyPi
服务器,我们将在其中发布内部库.通常,可以通过指定 pip.conf
文件或将命令行选项传递给 pip
来实现.
I am slightly modifying this Dockerfile to support my specific usecase: I need to specify my own PyPi
server, where we are publishing our internal libraries. This can normally be achieved by specifying a pip.conf
file, or by passing command line options to 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
我的 pip.conf
非常简单,并且可以在 Docker
之外使用时工作:
My pip.conf
is very simple, and works when used outside Docker
:
[global]
timeout = 60
index-url = http://xx.xx.xx.xx:yyyyy
trusted-host = xx.xx.xx.xx
链接:
Dockerfile
's ENV is documented herepip
configuration is documented here
我有以下问题:
- 为什么
ENV
不起作用? - 为什么在
RUN
命令中显式设置变量无效? -
ONBUILD
还是那些问题,或者也许是RUN
?
- Why is
ENV
not working? - Why is explicit setup of the variable in a
RUN
command not working? - Are those problems with
ONBUILD
, or maybeRUN
?
推荐答案
我遇到了同样的问题,并且设法解决了这个问题,并将其添加到Dockerfile中:
I was having the same problem and I managed to fix it adding this to the Dockerfile:
COPY pip.conf pip.conf
ENV PIP_CONFIG_FILE pip.conf
RUN pip install <my_package_name>
pip.conf文件具有以下结构:
The pip.conf file has the next structure:
[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>
这是我为Docker找到的唯一从pypi服务器中找到软件包的方法.我希望这种解决方案是通用的,并且可以帮助其他有此问题的人.
This is the only way I found for Docker to find the package from the pypi server. I hope this solution is general and helps other people having this problem.
这篇关于在dockerfile中自定义ONBUILD环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!