The symptom is: the host machine has proper network access, but programs running within containers can\'t resolve DNS names (which may appear to be \"can\'t access the netwo
One way is to use a user defined network for your container. In that case the container's /etc/resolv.conf
will have the nameserver 127.0.0.11
(a.k.a. the Docker's embedded DNS server), which can forward DNS requests to the host's loopback address properly.
$ cat /etc/resolv.conf
nameserver 127.0.0.1
$ docker run --rm alpine cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
$ docker network create demo
557079c79ddf6be7d6def935fa0c1c3c8290a0db4649c4679b84f6363e3dd9a0
$ docker run --rm --net demo alpine cat /etc/resolv.conf
nameserver 127.0.0.11
options ndots:0
If you use docker-compose, it will set up a custom network for your services automatically (with a file format v2+). Note, however, that while docker-compose
runs containers in a user-defined network, it still builds them in the default network. To use a custom network for builds you can specify the network
parameter in the build configuration (requires file format v3.4+).