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?
--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 .
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.
Try adding { "dns": ["8.8.8.8", "8.8.4.4"] }
in /etc/docker/daemon.json
.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With