130

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?

Migwell
  • 18,631
  • 21
  • 91
  • 160
  • What is the pip version in your docker container? Did you try `docker run mycontainer pip install -r requirements.pip` outside of the fig context? Still, it sounds like its a pip version issue. For some newer pip versions you need so specify some more command line options when running `pip install` so it finds certain packages. – Torsten Engelbrecht Feb 24 '15 at 09:57
  • My dockerfile has `FROM python:2.7` so it'll be whichever version of pip comes with python 2.7.9. I haven't tried running pip commands separately but that's because I can't even get the container to build since pip isn't working. I might try making an empty python container and testing that when I get the chance – Migwell Feb 24 '15 at 12:06
  • 1
    Maybe you can try `pip install --no-use-wheel --allow-all-external -r requirements.pip` or if that does not help try `pip install --no-use-wheel --allow-all-external --allow-unverified blinker -r requirements.pip`. That worked for me in the past when I had a similiar error with `pip install`. Also you could try to remove that line from the DockerFile and then build the container without it to test if the command can execute afterwards. As said though, I don't think that's the issue here. – Torsten Engelbrecht Feb 24 '15 at 12:56
  • 2
    You may add argument while running "docker build -t . --network=host" – abhishek kumar Mar 05 '20 at 10:39

20 Answers20

83

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

# Google IPv4 nameservers 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 : $ sudo nano /etc/dhcp/dhclient.conf 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

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
Tanzaho
  • 1,362
  • 12
  • 20
  • 1
    I have set dns in docker-compose, and it still doesn;t work. Any idea? – ismailsunni Oct 22 '15 at 06:24
  • 16
    ok, restarting my docker-machine is solving the problem. thanks – ismailsunni Oct 22 '15 at 07:07
  • 2
    It worked for me after adding Google DNS to local config but without specifying DNS server in Docker config file (it did not work if I do). I'm using Ubuntu 14.04, with Docker 1.12.2 – minhduc Oct 20 '16 at 09:27
  • 2
    As far I knonw, the `--dns` flag is available only on the `docker` command. I can't yet find an equivalent for Docker-Compose. – Sebastialonso Apr 01 '20 at 15:58
73

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

docker build --network=host -t image_name .
Dan Hook
  • 6,769
  • 7
  • 35
  • 52
  • 2
    THIS one worked for me w/o having to change anything in my Ubuntu 19.10 VM (which was created with Hyper-V quick create running on Window 10) – Wlad May 15 '20 at 13:26
  • 1
    Also worked for me running Docker on a Linux AWS Workspace – Kevin Hooke May 22 '21 at 20:46
  • Worked for me in Fedora 34 – simo23 Oct 08 '21 at 12:55
  • This worked for me for docker running inside minikube VM on Windows 10. – Aamir Rizwan Feb 18 '22 at 06:19
  • @Kevin Hooke. Same here, worked for me on a AWS EC2 instance. – Smile May 06 '22 at 01:12
  • This worked, but then created problems when I wanted to port-forward my container. Only thing I needed to do was `sudo pkill docker` and `service docker restart`. Then the DNS problem was gone, pip install worked as expected and I did not have to specify network=host which caused additional troubles. – Paloha Jul 10 '22 at 01:54
  • @Paloha Building the docker image with host networking doesn't affect the network mode used to run the container. If you run the image with host networking, that will mess up port forwarding since the container shares the same ip address and ports as the host. – Dan Hook Jul 11 '22 at 11:58
  • Thanks for clarification. I tried to remove the port forwarding (from 8888 to 8888) but my jupyter notebook still did not work. Assuming what you are saying is correct, than I would expect I do not have to port forward it at all and my jupyter notebook running on localhost:8888 within the docker container should be accessible also from the host, no? – Paloha Jul 11 '22 at 12:51
  • 1
    @Paloha Yes, in theory if you ran a jupyter server container in host mode then you should be able to attach to localhost:8888 without port forwarding. In practice, there are way too many details to deal with in comments. – Dan Hook Jul 11 '22 at 15:34
26

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
    
pimisi
  • 409
  • 4
  • 5
  • 2
    This solved my problem with `docker-ce`. Note: I hadn't the file `daemon.json`. Probably that file is not created by default during the installation and you need to create it manually. – floatingpurr Apr 04 '18 at 15:56
  • 2
    This is the only thing that worked for me on Ubuntu 18.04.1. I could find 2 DNS from the nmcli command and included both in daemon.json. – Newalp Aug 15 '18 at 09:01
  • It is the only thing that worked for me on Ubuntu 18.04.2. The solution was proposed earlier in this post by the answer from dekauliya – PatriceG Apr 04 '19 at 10:53
  • It worked. Also this solution is way less intrusive that the rest. Thank you! – Sebastialonso Apr 01 '20 at 16:00
  • Worked on CentOS 7. Thank you!! – schoon Jun 17 '20 at 10:07
20

For me simply restarting docker daemon helped.

service docker restart
Bartoszer
  • 349
  • 2
  • 4
16

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

This was the solution for me:

docker-machine restart <machine-name>
ndmeiri
  • 4,979
  • 12
  • 37
  • 45
orluke
  • 2,041
  • 1
  • 17
  • 15
  • 1
    For those of you using Windows and not sure what the arg should be: I just opened a PowerShell and ran `docker-machine restart` and then `docker-machine env` (as it had prompted me to) and it fixed the problem. – Nathan Wailes Nov 26 '18 at 09:08
15

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
Luke Preston
  • 179
  • 1
  • 4
9

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.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Broski
  • 257
  • 4
  • 7
6

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
    
vabada
  • 1,738
  • 4
  • 29
  • 37
6

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

dekauliya
  • 1,303
  • 2
  • 15
  • 26
  • Worked for me, too. It seems to be the solution that applies to newer docker versions. For details, see https://askubuntu.com/a/790778/776407 – Stefan Sieber Dec 28 '17 at 22:35
6

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

jmcgrath207
  • 1,317
  • 2
  • 19
  • 31
5

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
H.H
  • 364
  • 3
  • 11
  • It has worked in my case. It was necessary to remove the [user:passwd@] from proxy option, which is optional according pip documentation: --proxy : Specify a proxy in the form [user:passwd@]proxy.server:port. – Alex Javarotti Sep 12 '19 at 19:41
4

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

4

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.

Deon McClung
  • 191
  • 2
  • 8
4

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

p0wl
  • 658
  • 5
  • 10
1

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
Tomas
  • 944
  • 8
  • 10
  • I am in the same situation but the solution doesn't seem to work for me. My containers are still not able to connect to outside internet – harpratap Apr 18 '18 at 06:09
1

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.

Piyal George
  • 313
  • 5
  • 20
1

TL;DR

Update the default MTU of the docker daemon.

  1. Get the MTU of the physical interface with ip link.
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1480 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 72:bd:50:24:5e:a9 brd ff:ff:ff:ff:ff:ff
  1. Create /etc/docker/daemon.json and input the MTU value of the physical interface:
{
   "mtu": 1480
}
  1. Restart the docker service:
sudo service docker restart

Explanation

I had the same issue while running docker on a Linux VM. Setting -network=host resolved the problem but wasn't a satisfactory solution since I didn't know WHY it worked.

After several hours of searching, I found that the problem was caused by a mismatch between the MTU of the physical interface and the MTU of the virtual interface in the container. The MTU of the virtual interface must be smaller than or equal to the size of the MTU on the physical interface, otherwise containers will fail to receive packets that are larger than the physical MTU size.

In my case, the physical interface (VM) was configured with an MTU of 1480. Docker's default MTU is 1500.

With regards to pip, the problem manifested in what appeared to be a stalled SSL handshake (failed to read server hello), thus resulting in repeated ReadTimeout errors.

The problem is described here, though I used the solution described here.

Blak Jak
  • 41
  • 1
  • 3
0

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
hstsvn
  • 384
  • 3
  • 5
0

I searched a lot but there doesn't seem to be an answer for resolution of this error in docker desktop for Windows, Adding dns to docker.json resolved my issue.

Go to docker desktop -> Docker Engine here try modifying docker.json

Before change

{
  "builder": {
    "gc": {
      "defaultKeepStorage": "20GB",
      "enabled": true
    }
  },
  "experimental": false,
  "features": {
    "buildkit": false
  }
}

After change
{
  "builder": {
    "gc": {
      "defaultKeepStorage": "20GB",
      "enabled": true
    }
  },
  "experimental": false,
  "features": {
    "buildkit": false
  },
  "dns": [
    "114.114.114.114",
    "8.8.8.8"
  ]
}

Adding this dns to json file worked for me..

Amit
  • 1
-3

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
John Mee
  • 50,179
  • 34
  • 152
  • 186