Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to resolve domain names inside docker container

I'm runing a linux VM in virtualbox on my windows PC. I installed docker in the VM. Then I started an alpine container using docker run -it alpine.

In this container, I can ping external IPs successfully. But when I tried to ping domain names, e.g. google.com, it always return ping: bad address 'google.com'.

If I do nslookup google.com, it will tell me can't resolve 'google.com'. But all these operations can be done successfully in the VM (outside of the container).

In the /etc/resolv.conf of the container are the Google DNS server, 8.8.8.8 and 8.8.4.4. While for the VM it's 127.0.1.1.

Anyone know the similiar issue?

like image 902
user1576177 Avatar asked Jan 27 '18 15:01

user1576177


People also ask

What is the command to set DNS server for all Docker containers?

--dns=IP_ADDRESS Add the DNS server to the /etc/resolv. conf of the container and let the container use this server to resolve all hostnames that are not in /etc/hosts . --dns-search=DOMAIN sets the search domain of the container. When the search domain is set to .

How do I find my Docker DNS?

Run docker network ls to get the running networks names, and then docker network inspect NETWORK_NAME to see the containers in it. Look for the "Containers" keyword in the JSON, it is a list of connected devices. Look for the instance with the "IPv4Address": "127.0. 0.11/24" entry, the "Name" key is the DNS name.


3 Answers

Try adding { "dns": ["8.8.8.8", "8.8.4.4"] } in /etc/docker/daemon.json.

like image 152
cvitaa11 Avatar answered Oct 04 '22 02:10

cvitaa11


This (relatively) old issue is because of a buggy implementation of musl DNS library, as laid out in a comment of gliderlabs/docker-alpine #539.

$ docker run -it --rm alpine:3.4 nslookup google.com
nslookup: can't resolve '(null)': Name does not resolve

Name:      google.com
Address 1: 216.58.200.46 tsa01s08-in-f46.1e100.net
Address 2: 2404:6800:4012:1::200e tsa03s06-in-x0e.1e100.net

Where in newer versions of alpine, this seems to be fixed because alpine version >= 3.11.3 uses an internal implementation of DNS resolution instead of musl's one.

$ docker run -it alpine nslookup google.com
Server:     192.168.65.1
Address:    192.168.65.1:53

Non-authoritative answer:
Name:   google.com
Address: 2404:6800:4012:1::200e

Non-authoritative answer:
Name:   google.com
Address: 172.217.160.110
like image 45
Birkhoff Lee Avatar answered Oct 04 '22 01:10

Birkhoff Lee


I have a similar issue when running under a VM. You can just put the DNS server(s) in the docker run command.

docker run -it --dns 8.8.8.8 --dns 8.8.4.4 alpine
like image 24
Michael Avatar answered Oct 04 '22 00:10

Michael