user1576177
user1576177

Reputation: 201

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?

Upvotes: 20

Views: 23461

Answers (6)

Oleg Neumyvakin
Oleg Neumyvakin

Reputation: 10282

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

Upvotes: 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

Upvotes: 0

Birkhoff Lee
Birkhoff Lee

Reputation: 860

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

Upvotes: 5

Michael
Michael

Reputation: 9044

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

Upvotes: 1

aholbreich
aholbreich

Reputation: 5089

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.

Upvotes: 0

cvitaa11
cvitaa11

Reputation: 127

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

Upvotes: 4

Related Questions