Customize ONBUILD environment in a dockerfile

江枫思渺然 提交于 2021-02-08 06:28:12

问题


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.

I am trying with this:

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

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

Links:

  • Dockerfile's ENV is documented here
  • pip configuration is documented here

I have the following questions:

  • Why is ENV not working?
  • Why is explicit setup of the variable in a RUN command not working?
  • Are those problems with ONBUILD, or maybe RUN?

回答1:


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>

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>

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.




回答2:


The ONBUILD instruction adds to the image a trigger instruction to be executed at a later time, when the image is used as the base for another build.

From the doc:

Prior to 1.4, ONBUILD instructions did NOT support environment variable, even when combined with any of the instructions listed above.

With a docker 1.4+, try (as illustrated in issue 15025)

ONBUILD ENV PIP_CONFIG_FILE pip.conf


来源:https://stackoverflow.com/questions/38180964/customize-onbuild-environment-in-a-dockerfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!