Resolving Docker Pip Installation Failures Due to DNS Resolution Errors

A recurring issue encountered when installing Python packages within a Docker container using pip can manifest as a connection failure, often reported as "Failed to establish a new connection: [Errno -3] Temporary failure in name resolution". This problem typically arises unexpectedly, even after successful installations in the past.

While the host machine might experience smooth package installations, the Docker container fails. Several workaround exist for this DNS resolution problem within Docker containers.

Method 1: Modifying the Container's DNS Configuration

A common solution involves directly altering the container's DNS resolver configuration. By adding a reliable external DNS server, such as Google's public DNS, to the /etc/resolv.conf file within the container, network requests can be resolved correctly.

Add the following line to /etc/resolv.conf:

nameserver 8.8.8.8

While this change resolves the immediate issue, it may not be persistent across container restarts. To make this change permanent, you can modify the DHCP client configuration. Uncomment and update the prepend domain-name-server line in /etc/dhcp/dhclient.conf:

prepend domain-name-servers 8.8.8.8, 8.8.4.4;

After saving the changes, restart the DHCP client:

sudo dhclient

Method 2: Configuring Docker's DNS Settings

Alternatively, you can configure Docker itself to use specific DNS servers. This approach ensures that all containers launched by the Docker daemon will inherit these DNS settings.

Edit the Docker daemon configuration file, typically located at /etc/default/docker:

sudo nano /etc/default/docker

Add the following option to the DOCKER_OPTS line:

DOCKER_OPTS="--dns 8.8.8.8"

Save the file and restart the Docker daemon to apply the changes:

sudo systemctl restart docker

Method 3: Specifying DNS at Runtime

For a temporary solution or for specific container runs, you can specify the DNS server directly when launching a container using the --dns flag:

docker run --dns 8.8.8.8 your_image_name

The underlying cause of this issue can be complex. Docker containers typically inherit DNS settings from the host system. If the host's DNS configuration (e.g., using a local or a specific cloud provider's DNS) is problematic or inaccessible from within the container's network environment, these errors can occur. While the host might function correctly, the isolated network of the container might face different resolution challenges.

Tags: docker pip DNS networking troubleshooting

Posted on Thu, 06 Aug 2026 16:59:14 +0000 by Dasndan