Skip to content

Docker fails to connect to internet from inside the container

Posted on:February 22, 2024 at 02:53 AM

tp.web.random_picture

Docker can’t install npm packages or pip packages from the internet

Table of contents

Open Table of contents

TL;DR

One of the main reasons for this issue is DNS name resolution failing. So to fix this issue we can add a public DNS server name in the network configuration. To add the DNS configuration for the docker, add the following configuration to the /etc/docker/daemon.json file. If the file does not exist, please create one.

{
  "dns": ["10.0.0.2", "8.8.8.8", "8.8.4.4"]
}
$ sudo service docker restart

Introduction

Sometimes times docker’s internet connectivity does not work as expected and causes so many weird issues with the applications running inside the docker container. Most of the time these issues are caused due to DNS lookup failing inside the docker image.

What is DNS lookup?

DNS lookup is the process of converting human-readable domain names (e.g., www.rajendrasinh.com) into IP addresses. It involves querying hierarchical DNS servers to obtain the IP associated with a domain. This translation allows users to access websites using easily remembered names rather than numerical IP addresses.

DNS configuration

Whenever we connect to the internet our system has the configuration setup where we add a DNS server IP address. In Ubuntu, it’s typically in the network configuration. you can go to Settings > wifi or Network > gear icon for currently active connection. This will look something like this

Ubuntu DNS configuration

DNS issue with docker

When Docker is running in the system it also uses the internet from the host system to communicate to the internet. As docker has its configuration for the network communication, first it will go and try to resolve the DNS using the configuration provided in the /etc/docker/daemon.json file. by default, this file is not available when we install the docker. instead, it uses the host system’s DNS. If the host system DNS configuration is not correct, it might fail to resolve the IP address for the requests sent from the docker container. To resolve this we have mainly two options

DNS update in the host system (in our case Ubuntu)

Go to Settings > wifi or Network > gear icon for currently active connection and check if the setting under the IPv4 tab is set to automatic for DNS.

If the DNS is set to Automatic, switch that off and add comma comma-separated value of Google public DNS 8.8.8.8, 8.8.4.4 (as provided in the image above). Once this is done, the issue with lookup should be resolved.

Docker daemon.json configuration file

If the daemon.json file is available for the docker configuration, we can update the configuration with the following content

{
  "dns": ["10.0.0.2", "8.8.8.8", "8.8.4.4"]
}

Conclusion

In troubleshooting Docker connectivity issues, DNS lookup failures often play a crucial role. To address this, adding public DNS server names, such as Google’s (8.8.8.8 and 8.8.4.4), to the Docker configuration (/etc/docker/daemon.json) or updating the host system’s DNS settings can resolve these issues, ensuring smooth communication for applications within Docker containers.