You can do that by launching your own DNS resolver container.
docker run -d --name devdns -p 53:53/udp
-v /var/run/docker.sock:/var/run/docker.sock ruudud/devdns
Once you have run the DNS server. The server is mapped to your localhost. On linux you can edit /etc/resolv.conf
and add nameserver 127.0.0.1
at the top. This change will be reverted after reboot.
Now if you launch a docker container
docker run -d --hostname tarunlalwani --name tlalwani ubuntu:16.04 sleep 2000
Now you can ping the container using either the container name or the hostname
$ ping tlalwani.dev
PING tlalwani.dev (172.17.0.6) 56(84) bytes of data.
64 bytes from 172.17.0.6: icmp_seq=1 ttl=64 time=0.030 ms
$ ping tarunlalwani.dev
PING tarunlalwani.dev (172.17.0.1) 56(84) bytes of data.
64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.026 ms
dev
is the default domain name. You can change this using environment variables. For more details refer to https://github.com/ruudud/devdns
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…