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

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


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 nameserversnameserver 8.8.8.8nameserver 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/docker3. 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


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

docker build --network=host -t image_name .


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