Solving Python Docker issue – Can’t install pip packages inside a docker container with Ubuntu

Can’t install pip packages inside a docker container with Ubuntu is an error which occurs in Python when Docker is not using the correct DNS server.

In this article we are going to explain why the error is popping up and show you how to solve the error and get rid of it for good.

Exploring the Error : Can’t install pip packages inside a docker container with Ubuntu

First of all, the error occurs with docker containers in Ubuntu when Docker does not use the correct DNS server.

Please make sure you have the same error because you do not want to confuse this error with another one.

                                                                       #
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/
                                                                       #

Bellow we will describe how the error can be solved. With multiple possible solutions.

Solution 1 : Modify the Docker settings.

The problem is that Docker is not using a proper DNS server. The first solution is to modify the configuration of Docker.

Use sudo privileges to Log into the Os.

Navigate to the file bellow and edit it.

                                                                       #
sudo nano /etc/default/docker
                                                                       #

Modify the Docker settings by adding the line bellow. Then save the changes

                                                                       #
DOCKER_OPTS="--dns 8.8.8.8"
                                                                       #

Use restart docker to restart the daemon. Using the command bellow.

                                                                       #
sudo systemctl restart docker
                                                                       #

This solution should be enough, try the solution bellow if this one did not help at all.

Solution 2 : edit /etc/resolv.conf and /etc/dhcp/dhclient.conf

In case the solution above does not solve your problem you can use the proper DNS server by using this solution.

First step is to add the two lines bellow to /etc/resolv.conf and then save the changes

                                                                       #
# Google IPv4 nameservers
nameserver 8.8.8.8
nameserver 8.8.4.4
                                                                       #

You can edit /etc/dhcp/dhclient.conf in order to make the changes permanent.

The line bellow will be commented already, you need to uncomment it and save and then Restart the dhclient using the command: sudo dhclient.

                                                                       #
prepend domain-name-servers 8.8.8.8, 8.8.4.4;
                                                                       #

The above solution should work just fine, If that is not the case try the solution bellow.

Solution 3 : add –dns 8.8.8.8.

The third solution is the simplest and fastest solution.

Just add the parameter : --dns 8.8.8.8 when you run Docker.

The solutions above should be enough to solve the problem, if you like our effort make sure to consider donating to our Kofi account, there is a red button that you can use if you are feeling generous.

Summing-up

The error is confusing, I hope you solved it with some help from this article, remember errors are normal in our field, that is where the fun is, keep coding and keep creating, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/