Can't install pip packages inside a docker con

2019-01-10 06:43发布

I'm following the fig guide to using docker with a python application, but when docker gets up to the command

RUN pip install -r requirements.txt

I get the following error message:

Step 3 : RUN pip install -r requirements.txt
 ---> Running in fe0b84217ad1
Collecting blinker==1.3 (from -r requirements.txt (line 1))
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', gaierror(-2, 'Name or service not known'))': /simple/blinker/

This repeats several times and then I get another message:

Could not find any downloads that satisfy the requirement blinker==1.3 (from -r requirements.txt (line 1))
  No distributions at all found for blinker==1.3 (from -r requirements.txt (line 1))

So for some reason pip can't access any packages from inside a docker container. Is there anything I need to do to allow it internet access?

However pip works fine to install things outside of the docker container, and worked fine even with that exact package (blinker==1.3) so that's not the problem. Also this problem isn't specific to that package. I get the same issue with any pip install command for any package.

Does anyone have any idea what's going on here?

14条回答
淡お忘
2楼-- · 2019-01-10 06:52

Configuring docker DNS to Google DNS (8.8.8.8) or 10.0.0.2 did not work in my company environment.

Running: $ drill @8.8.8.8 www.amazon.com or @10.0.0.2 confirmed this.

In order to find a DNS that would work I ran: $ drill www.amazon.com and it gave me the DNS IP that is being used in my network.

Then I set it in Ubuntu using the following step to configure docker's DNS.

Changed dns in /etc/docker/daemon.json

{
    "dns": ["the DNS ip from step1"]
}

Restart docker: sudo service docker restart
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-10 06:53

For Ubuntu users

You need to add new DNS addresses in the docker config

sudo nano /lib/systemd/system/docker.service

Add the dns after ExecStar.

--dns 10.252.252.252 --dns 10.253.253.253

Should look like that:

ExecStart=/usr/bin/dockerd -H fd:// --dns 10.252.252.252 --dns 10.253.253.253

Then do:

systemctl daemon-reload
sudo service docker restart

Should work.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-10 06:57

Im new to Docker and tried all the methods mentioned here, but still didn't get it right. the Docker version was 18, and ubuntu version was 16. I tried this method:- First i was building docker with company's internet network. this network is blocking some sites or some how things didnt go well here. So secondly i connected to my very own network(which im using in mobile phone, for example) and tried. things went right. requirement.txt was installed successfully, and docker was build.

查看更多
看我几分像从前
5楼-- · 2019-01-10 06:58

ok, restarting my docker-machine is solving the problem. thanks – ismailsunni

This was the solution for me:

docker-machine restart <machine-name>
查看更多
不美不萌又怎样
6楼-- · 2019-01-10 06:58

For me simply restarting docker daemon helped.

service docker restart
查看更多
The star\"
7楼-- · 2019-01-10 06:58

Let it run. Sometimes pypi is having connection issues which are noisily put in your face to make you think it is broke. Just to be sure, let it roll, you might find it works it out for itself.

The bottom line, despite these red error lines, is "Successfully built"

$ docker build .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM docker-registry.aws.example.com:5000/cmcrc/python2:20160517120608
 ---> 1e5034711aa9
Step 2 : RUN pip install prometheus-client requests
 ---> Running in f3c580fc93ae
Collecting prometheus-client
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe15a1d8610>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/prometheus-client/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe15a1d87d0>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/prometheus-client/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe15a1d8990>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/prometheus-client/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe15a1d8b50>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/prometheus-client/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe15a1d8d10>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/prometheus-client/
  Downloading prometheus_client-0.0.13.tar.gz
Collecting requests
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe159e9d4d0>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/requests/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe159e9da10>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/requests/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe159e9dc50>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/requests/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe159e9de10>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/requests/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.HTTPConnection object at 0x7fe159e9dfd0>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /pypi/requests/
  Downloading requests-2.10.0-py2.py3-none-any.whl (506kB)
Building wheels for collected packages: prometheus-client
  Running setup.py bdist_wheel for prometheus-client: started
  Running setup.py bdist_wheel for prometheus-client: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/04/94/f5/b803b2ff65e8344e99ca99b7f7cb8194224017167809a32b78
Successfully built prometheus-client
Installing collected packages: prometheus-client, requests
Successfully installed prometheus-client-0.0.13 requests-2.10.0
 ---> 19c5e3cfe08f
Removing intermediate container f3c580fc93ae
Successfully built 19c5e3cfe08f
查看更多
登录 后发表回答