【问题标题】:Customize ONBUILD environment in a dockerfile在 dockerfile 中自定义 ONBUILD 环境
【发布时间】:2016-07-04 09:16:11
【问题描述】:

我稍微修改了this 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

我的pip.conf很简单,在Docker外面使用也可以:

[global]
timeout = 60
index-url = http://xx.xx.xx.xx:yyyyy
trusted-host = xx.xx.xx.xx

链接:

  • Dockerfile 的 ENV 记录在 here
  • pip 配置记录在案 here

我有以下问题:

  • 为什么ENV 不起作用?
  • 为什么在 RUN 命令中显式设置变量不起作用?
  • ONBUILD 还是RUN 的问题?

【问题讨论】:

    标签: python docker dockerfile


    【解决方案1】:

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

    COPY pip.conf pip.conf
    ENV PIP_CONFIG_FILE pip.conf
    RUN pip install <my_package_name>
    

    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>
    

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

    【讨论】:

      【解决方案2】:

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

      From the doc:

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

      使用 docker 1.4+,尝试(如issue 15025 所示)

      ONBUILD ENV PIP_CONFIG_FILE pip.conf
      

      【讨论】:

      • 好主意,但似乎不起作用:(。我的码头工人是Docker version 1.11.2, build b9f10c9
      • 我使用我的 dockerfile 构建图像,它构建在我粘贴的 dockerfile 之上,包括您的修改,执行 ONBUILDs 。 pip没有找到我的自定义库,说明pip配置没有被正确读取,也就是说envPIP_CONFIG_FILE没有设置。
      • 好的。您可以尝试一个简单的 RUN echo ${yourVariable} 来查看 ENV 是否存在吗?
      • 这条评论的格式不对,但是变量确实设置了:Step 2 : RUN echo ${PIP_CONFIG_FILE} ---&gt; Running in a5fc69f16f32 pip.conf
      • 那些环境变量是否标记为导出? pip 应该看到吗?好像没有。
      猜你喜欢
      • 2019-12-18
      • 2021-06-12
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多