Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize ONBUILD environment in a dockerfile

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?
like image 865
blueFast Avatar asked Jul 04 '16 09:07

blueFast


1 Answers

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.

like image 150
Raquel Avatar answered Sep 30 '22 18:09

Raquel