18

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?

aryanveer
  • 628
  • 1
  • 6
  • 17
user1576177
  • 181
  • 1
  • 1
  • 5

6 Answers6

3

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
Birkhoff Lee
  • 788
  • 10
  • 21
2

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

cvitaa11
  • 91
  • 4
2

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
Michael
  • 8,891
  • 3
  • 29
  • 42
0

Depends on which network you work.

In the default network, a container inherits the DNS settings of the host, as defined in the /etc/resolv.conf configuration file

when you use a custom network, then Docker’s embedded DNS server will be used, which forwards external DNS lookups to the DNS servers configured on the host.

In both cases, you should not edit /etc/resolv.conf of your container. It's done by Docker.

aholbreich
  • 4,951
  • 2
  • 23
  • 38
0

I removed dns configuration from docker engine and it resolved my issue.

Docker Desktop (Mac) > Docker Engine > remove dns part of json and restart docker.

You can also edit same file from: ~/.docker/daemon.json

Gürcan Kavakçı
  • 562
  • 1
  • 11
  • 24
0

One of the reasons is no network at all in the container:

# docker run alpine:3.15 ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
^C
--- 8.8.8.8 ping statistics ---
11 packets transmitted, 0 packets received, 100% packet loss
Community
  • 1
  • 1
Oleg Neumyvakin
  • 9,706
  • 3
  • 58
  • 62