Docker With Custom DNS Server
By configuring your Docker containers to talk to a custom DNS server, you gain more control over how your container look up other services – database, other microservices, etc.,
It turns out Docker networking isn’t completely straight forward. On Linux you can:
- Run a DNS server locally, either dnsmasq or devdns
- Run your container with
--dns 172.17.0.1
, the magic IP where your host machine is located - Configure dnsmasq from step 1 via
/etc/hosts
Sadly, none of it was easy. The entire thing probably took me three hours.
Install the DNS server was… confusing. I wouldn’t know how to verify if my DNS queries (via the dig command)
where answered by the DNS server I configured. I discovered I was talking to a [systemd-resolved][4] instance by
guesswork, which prompted me to finally install dnsmasq
.
But finally, with dnsmasq
running locally, the Docker containers running on my machine can be affected by my
configurations in /etc/hosts
, which makes integration testing against other services much easier.