Issue with installing pip packages inside a docker container with Ubuntu

Hi

Im going over the get-started with docker step 2,

I have setup docker on my fresh ubuntu 16.04.02 setup. successfully run the “docker run hello-world” and “ubuntu bash”.
when I try to builf the python image i get -

bzbit@bzbit-9020:~/tmp$ docker build -t friendlyhello .
Sending build context to Docker daemon 4.608kB
Step 1/7 : FROM python:2.7-slim
—> d962f7a9f2f1
Step 2/7 : WORKDIR /app
—> Using cache
—> eceded511fcc
Step 3/7 : ADD . /app
—> Using cache
—> d8617e220fb2
Step 4/7 : RUN pip install -r requirements.txt
—> Running in f03e976770ac
Collecting Flask (from -r requirements.txt (line 1))
Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by ‘ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7feb91e1c210>, ‘Connection to pypi.python.org timed out. (connect timeout=15)’)’: /simple/flask/

I’m behind proxy and I have configured proxy according to this https://docs.docker.com/engine/admin/systemd/#httphttps-proxy

Adding dns as suggested here - https://stackoverflow.com/questions/28668180/cant-install-pip-packages-inside-a-docker-container-with-ubuntu/41989423#41989423
is not helping .

thanks in advance.

1 Like

I just had the same error and was able to solve it by adding lines to the Dockerfile file (after the initial FROM line).

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

hope this helps.

5 Likes

The proposed solution does not work for my system:

cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.3 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.3 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial

Kernel info:

uname -a
Linux c3p0 4.10.0-28-generic #32~16.04.2-Ubuntu SMP Thu Jul 20 10:19:48 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

(FYI: This is running as a VM in Virtualbox (5.2.8 r121009 Qt5.6.2). The host is Windows 10 Enterprise.)

Docker details:

docker --version
Docker version 17.12.0-ce, build c97c6d6

docker info
Containers: 6
 Running: 0
 Paused: 0
 Stopped: 6
Images: 16
Server Version: 17.12.0-ce
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 89623f28b87a6004d4b785663257362d1658a729
runc version: b2567b37d7b75eb4cf325b77297b140ea686ce8f
init version: 949e6fa
Security Options:
 apparmor
 seccomp
  Profile: default
Kernel Version: 4.10.0-28-generic
Operating System: Ubuntu 16.04.3 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 3.857GiB
Name: c3p0
ID: ID7G:ZMDK:2VKT:HCIN:6MMT:7PI5:Z7DV:JJL2:QOFU:PZ3T:MQQ4:3YBO
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

WARNING: No swap limit support

Here is the output:

docker build -t friendlyhello .
Sending build context to Docker daemon   5.12kB
Step 1/9 : FROM python:2.7-slim
 ---> 55d87c387a8c
Step 2/9 : ENV http_proxy http://proxy-chain.xxx.com:911/
 ---> Using cache
 ---> 2d1ddd1b37fc
Step 3/9 : ENV https_proxy http://proxy-chain.xxx.com:912/
 ---> Using cache
 ---> fdd11fdeb3bf
Step 4/9 : WORKDIR /app
 ---> Using cache
 ---> bf6b0f413e21
Step 5/9 : ADD . /app
 ---> Using cache
 ---> c1bd11eec254
Step 6/9 : RUN pip install --trusted-host pypi.python.org -r requirements.txt
 ---> Running in ab5a093344d0
Collecting Flask (from -r requirements.txt (line 1))
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))': /simple/flask/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))': /simple/flask/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))': /simple/flask/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))': /simple/flask/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))': /simple/flask/
  Could not find a version that satisfies the requirement Flask (from -r requirements.txt (line 1)) (from versions: )
No matching distribution found for Flask (from -r requirements.txt (line 1))
The command '/bin/sh -c pip install --trusted-host pypi.python.org -r requirements.txt' returned a non-zero code: 1

Dockerfile:

# Use an official Python runtime as a parent image
FROM python:2.7-slim
ENV http_proxy http://proxy-chain.xxx.com:911/
ENV https_proxy http://proxy-chain.xxx.com:912/

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

requirements.txt file:

Flask
Redis

app.py file:

from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "cannot connect to Redis, counter disabled"

    html = "

Hello {name}!

" \ "Hostname: {hostname}
" \ "Visits: {visits}" return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits) if __name__ == "__main__": app.run(host='0.0.0.0', port=80)

This worked for me.
Thanks !!

Along with below Proxy configuration in DockerFile

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

and use docker build command with following argument

$ docker build --no-cache --network=host -t friendlyhello .

Tested in Docker version - 18.06.0-ce-rc1 + Ubuntu 18.04

2 Likes

This worked for me. Thanks

great this worked for me. ty
ENV http_proxy http://username:password@proxy-chain.xxx.com:911/
ENV https_proxy http://username:password@proxy-chain.xxx.com:912/

This worked for me.
is it important to put take 911,912 because my i took 8080?
ENV http_proxy http://username:password@proxy-chain.xxx.com:911/
ENV https_proxy http://username:password@proxy-chain.xxx.com:912/

This worked like a charm. Thanks!

So the proposed solution is to proxy through a porn site ?
Yeah sound like a great solution </sarcasm>