To begin, run cat /etc/resolv.conf in the docker container. If it uses an invalid DNS server, such as nameserver 127.0.x.x, the container will be unable to resolve domain names into IP addresses, causing ping google.com to fail.
The second step is to run cat /etc/resolv.conf on the host machine. Every time a container is started, Docker copies the host's /etc/resolv.conf to the container. If the host's /etc/resolv.conf is incorrect, the docker container will be as well.
If you discover that the host's /etc/resolv.conf is incorrect, you have two choices:
-
The DNS server is hardcoded in daemon.json. This is simple, but not ideal if you anticipate the DNS server changing.
-
Fix the hosts' /etc/resolv.conf file. This is a little more difficult, but it is generated dynamically, and the DNS server is not hardcoded.