Can't install pip packages inside a docker container with Ubuntu

PythonDockerPipFig

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

Your problem comes from the fact that Docker is not using the proper DNS server. You can fix it in three different ways :

1. Adding Google DNS to your local config

Modifying /etc/resolv.conf and adding the following lines at the end

nameserver 8.8.8.8
nameserver 8.8.4.4

If you want to add other DNS servers, have a look here.

However this change won't be permanent (see this thread). To make it permanent :

Uncomment and edit the line with prepend domain-name-server : prepend domain-name-servers 8.8.8.8, 8.8.4.4;

Restart dhclient : $ sudo dhclient.

2. Modifying Docker config

As explained in the docs :

> Systems that run Ubuntu or an Ubuntu derivative on the desktop typically use 127.0.0.1 as the default nameserver in /etc/resolv.conf file.

> To specify a DNS server for use by Docker :

1. Log into Ubuntu as a user with sudo privileges.

2. Open the /etc/default/docker file for editing :

    $ sudo nano /etc/default/docker

3. Add the following setting for Docker.

    DOCKER_OPTS="--dns 8.8.8.8"

4. Save and close the file.

5. Restart the Docker daemon :

    $ sudo systemctl restart docker

3. Using a parameter when you run Docker

When you run docker, simply add the following parameter : --dns 8.8.8.8

Solution 2 - Python

I needed to add --network=host to my docker build command:

docker build --network=host -t image_name .

Solution 3 - Python

I had the same issue and it plagued me for a while and I tried a lot of solutions online but to no avail. However I finally resolved it as follows:

###Running:

Ubuntu 16.04 
docker Server 18.03.0-ce
  1. Discover the address of your DNS server.

    Discover the address of your DNS server by running the following command:

     $: nmcli dev show | grep 'IP4.DNS'
     IP4.DNS[1]:                192.168.210.2
    
  2. Update the Docker daemon

Create a docker config file at /etc/docker/daemon.json. (if you don't already have one) and add the following content to the file:

    {
        "dns": ["192.168.210.2", "8.8.8.8"]
    }

The first item of the array is your network's DNS server and the second is google's DNS server as a fallback when if your network's DNS is not available.

Save the file and then restart the docker service

    $: sudo service docker restart

Solution 4 - Python

For me simply restarting docker daemon helped.

service docker restart

Solution 5 - Python

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

This was the solution for me:

docker-machine restart <machine-name>

Solution 6 - Python

In case someone is reading this using docker-compose. I managed to resolve this by changing my yaml file as follows

version: 3.4
service: my-app
  build:
  context: .
  network: host

which is equivalent to writing

docker build . --network host

Solution 7 - Python

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.

Solution 8 - Python

In my case, with docker version 1.13.0 and docker-machine 0.9.0 under Ubuntu 16.04 I had to modify slightly Tanzaho's answer (2. Modifying Docker config) as follows:

  1. Log into Ubuntu as a user with sudo privileges.

  2. Open the /etc/default/docker file for editing:

     sudo vim /etc/default/docker
    
  3. Add the following setting for Docker.

     DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"
    
  4. Save and close the file.

  5. Restart the Docker daemon :

     sudo service docker restart
    

Solution 9 - Python

For me, I was unable to install pip due to the docker's DNS not configured properly. I've tried the above steps, however, configuring docker DNS to Google DNS does not work for my laptop. Docker's DNS can be properly configured only if I set its DNS to my laptop's assigned IP.

If you use Ubuntu, you can use the following steps to configure your docker's DNS:

  1. Find out your device's assigned IP. You can find this by either

    • Checking the inet addr of your ethernet or wlan in ifconfig
    • Choosing any address in nmcli dev show | grep 'DNS'
  2. Edit dns in /etc/docker/daemon.json (create this file if it doesn't exist previously)

     {
         "dns": ["your_ip_in_step_1"]
     }
    
  3. Restart docker: sudo service docker restart

Solution 10 - Python

For me it's because I was on the VPN and docker couldn't find the route my private PYPI. If you need to stay on the VPN use docker build --network=host

Solution 11 - Python

I had same problem.The cause of error is proxy.

So, I edit Dockerfile following

RUN pip install -r /app/requirements.txt --proxy=http://user:pass@addr:port

Solution 12 - Python

As a Docker newbie, I had a problem that manifested itself in this way when I was following the tutorial for Docker at:

https://docs.docker.com/get-started/part2

I'm using Docker 17.03.1-ce on a corporate LAN.

I checked and double checked my DNS settings. I'd used various ways of configuring the DNS that I'd found in my searches across the Internet. Some caused errors on startup. The approach that I ultimately settled upon for configuring the DNS was the one in the Troubleshoot Linux section of the above link above where the DNS is configured via the daemon.json file in the /etc/docker directory.

However, I still had this same issue. What finally solved the problem for me was the configuration of the proxy via the http_proxy and https_proxy environment variables. I had them specified in my Dockerfile, but I neglected to do so before the RUN pip command.

Even though it appeared to be a DNS issue, moving these ENV commands ahead of the RUN command made the difference for me. In case that is helpful for anyone with this problem.

Solution 13 - Python

I do not know the reason, but the error means that pip is trying to resolve the /simple/blinker/ as a DNS hostname instead of the pypi.python.org part, which seems very odd since I cannot even come up with any URL for which urlparse could return such a string as a hostname part. I'd check if there is some problem with ~/.pip/pip.conf

Solution 14 - Python

For me, it was caused by being connected to my university VPN. Disconnecting "solved" the problem.

Solution 15 - Python

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

Solution 16 - Python

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.

Solution 17 - Python

I guess you tried to run pip install within a private environment that does not allow direct access/install from the public repo. If that's the case, you can add --index-url and --trusted-host to the requirements.txt like follows:

requirements.txt:

--index-url https://pypi.internal.org/api/pypi/org.python.pypi/simple
--trusted-host pypi.internal.org pypi.python.org pypi.org files.pythonhosted.org
blinker==1.3

Solution 18 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMigwellView Question on Stackoverflow
Solution 1 - PythonTanzahoView Answer on Stackoverflow
Solution 2 - PythonDan HookView Answer on Stackoverflow
Solution 3 - PythonpimisiView Answer on Stackoverflow
Solution 4 - PythonBartoszerView Answer on Stackoverflow
Solution 5 - PythonorlukeView Answer on Stackoverflow
Solution 6 - PythonLuke PrestonView Answer on Stackoverflow
Solution 7 - PythonBroskiView Answer on Stackoverflow
Solution 8 - PythonvabadaView Answer on Stackoverflow
Solution 9 - PythondekauliyaView Answer on Stackoverflow
Solution 10 - Pythonjmcgrath207View Answer on Stackoverflow
Solution 11 - PythonH.HView Answer on Stackoverflow
Solution 12 - PythonDeon McClungView Answer on Stackoverflow
Solution 13 - PythonAntti Haapala -- Слава УкраїніView Answer on Stackoverflow
Solution 14 - Pythonp0wlView Answer on Stackoverflow
Solution 15 - PythonTomasView Answer on Stackoverflow
Solution 16 - PythonPiyal GeorgeView Answer on Stackoverflow
Solution 17 - PythonhstsvnView Answer on Stackoverflow
Solution 18 - PythonJohn MeeView Answer on Stackoverflow