Pip inside dockerfile under proxy
Asked Answered
H

5

19

I am trying to build a Docker image for elasticsearch-curator,

Here is the dockerfile:

FROM alpine:3.7

RUN adduser -S curator

RUN apk add --update \
    python \
    python-dev \
    py-pip \
    build-base \
  && pip install virtualenv \
  && pip install elasticsearch-curator \
  && rm -rf /var/cache/apk/*

USER curator

ENTRYPOINT [ "/usr/bin/curator"]

Thing is I am under a proxy, so I must build my image with:

docker build  --no-cache --build-arg HTTP_PROXY=http://xx.xx.xx.xx:xx -t elasticsearch-curator:5.4 .

But when it wants to get virtualenv, I get:

Collecting virtualenv
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fb8259ed350>, 'Connection to pypi.python.org timed out. (connect timeout=15)')': /simple/virtualenv/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fb8259ed210>, 'Connection to pypi.python.org timed out. (connect timeout=15)')': /simple/virtualenv/

I found people solving issue inserting

ENV http_proxy http://proxy-chain.xxx.com:911/
ENV https_proxy http://proxy-chain.xxx.com:912/

in the Dockerfile, but it is not possible for me, because my proxy is only valid on my building, so if another person from another place want to build the image, he will need to remove http_proxy env var from Dockerfile.

Is there any other way to achieve it? It seems like a very common use case...

Handley answered 16/1, 2018 at 9:10 Comment(2)
The --build-arg solution should have done the trick. Is your host windows or linux? Have some user/paswd set in your proxy? in that case the ard should be http_proxy=http://USERNAME:[email protected]:3128/Patric
nop, I don't use login / pass. The build args is working to dowload the alpine image, but it doesn't work for pip :(Handley
H
32

I solved it by adding HTTPS_PROXY in command line:

docker build  --no-cache --build-arg HTTP_PROXY=http://xx.xx.xx.xx:xx --build-arg HTTPS_PROXY=http://xx.xx.xx.xx:xx -t elasticsearch-curator:5.4 .
Handley answered 16/1, 2018 at 9:54 Comment(0)
S
18

Don't include the proxy settings in the Dockerfile.

If you have configured the proxy settings correctly on the host machine, you can build the docker image with --network= host. This will make the build command use the network settings of the host.

docker build  --no-cache --network=host -t elasticsearch-curator:5.4 .
Synchro answered 16/1, 2018 at 9:55 Comment(3)
This did the job for me. I couldn't pip install flask without --network=hostDesmond
Works with "FROM python:3.7"Macedo
this solution worked for me. The solution setting HTTPS_PROXY did not.Diminuendo
P
1

I think this is because pip install needs explicit proxy arg.

Try to make an install.sh to pip installs.

If there's a proxy configured (passed as build-arg, i.e. set int the environment), install it with :

pip install --proxy=https://user@mydomain:port virtualenv

With env variable:

pip install --proxy=$HTTP_PROXY virtualenv

If not, pip install without proxy.

Patric answered 16/1, 2018 at 9:45 Comment(2)
I didn't test it, I found the solution, in command line, but even if it works, I wouldn't like to write proxy vars in my Dockerfile, I prefer pure command line. Thanks for your help anyway!Handley
I see... It was just to pass https proxy also :-P.Patric
U
1

You can configure all clients proxies via the ~\.docker\config.json file in your home or user directory directory:

{ 
  "credsStore": "wincred",
  "auths": {},
  "stackOrchestrator": "swarm",
  "proxies":
  {
   "default":
   {
    "httpProxy": "http://127.0.0.1:3001",
    "noProxy": "*.test.example.com,.example2.com"
   }
  }
}

The first three entries are there by default, simply add the "proxies" section to your file.

Source: https://docs.docker.com/network/proxy/

Unreliable answered 2/8, 2018 at 11:11 Comment(0)
U
0

For anybody stuck trying this with https_proxy - try with HTTPS_PROXY in the docker build command line. Looks like pip needs it in all caps!

docker build . -t <image-name>:<version> --build-args HTTPS_PROXY=http://your-proxy.com:port

works, where as

docker build . -t <image-name>:<version> --build-args https_proxy=http://your-proxy.com:port

didn't work for me. Strange quirk.

Unset answered 12/7 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.