skip to content
Burak Berk Keskin
Table of Contents

Overview

Docker containers do not inherit custom hostname mappings from the host’s /etc/hosts file, which turns local service-to-service testing into a chore of maintaining per-container extra_hosts blocks.

In my local environment, I solved this at the DNS layer by binding dnsmasq to the docker0 bridge interface and pointing the Docker daemon at that bridge IP. That setup works in two variants: standalone dnsmasq or dnsmasq managed as a NetworkManager plugin. For multi-network DNS resolution across an entire LAN and VPN, I also use split-horizon DNS on AdGuard Home.

Installing Dnsmasq

Verify whether dnsmasq is already available:

Terminal window
which dnsmasq

If missing, install it from the distribution package manager:

Terminal window
# Debian / Ubuntu
sudo apt install dnsmasq
# RHEL / CentOS / Fedora
sudo dnf install dnsmasq

Configuring Dnsmasq

Dnsmasq Standalone Usage

When NetworkManager is not managing host DNS, configure dnsmasq directly in /etc/dnsmasq.conf:

/etc/dnsmasq.conf
# Listen on the Docker bridge interface
interface=docker0
# Specify an upstream DNS server to forward queries
server=8.8.8.8

Enable and verify the service:

Terminal window
sudo systemctl stop dnsmasq && \
sudo systemctl enable --now dnsmasq && \
sudo systemctl status dnsmasq

Dnsmasq with NetworkManager

When NetworkManager controls host networking, enable dnsmasq as its DNS plugin instead of running a competing standalone instance. Edit /etc/NetworkManager/NetworkManager.conf and add dns=dnsmasq under [main]:

/etc/NetworkManager/NetworkManager.conf
[main]
dns=dnsmasq

Then create a dedicated snippet under /etc/NetworkManager/dnsmasq.d/split-dns.conf:

/etc/NetworkManager/dnsmasq.d/split-dns.conf
# Listen on the Docker bridge interface
interface=docker0
# Specify an upstream DNS server to forward queries
server=8.8.8.8

Restart NetworkManager and confirm the service stays healthy:

Terminal window
sudo systemctl restart NetworkManager && \
sleep 5 && \
sudo systemctl status NetworkManager

If resolution fails or the plugin refuses to bind, inspect the NetworkManager logs:

Terminal window
journalctl -u NetworkManager -r

Pointing Docker to the Bridge Resolver

Check the IPv4 address assigned to the docker0 bridge:

Terminal window
ip a show docker0
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:a9:72:e1:3a brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:a9ff:fe72:e13a/64 scope link
valid_lft forever preferred_lft forever

Set 172.17.0.1 as the default DNS server in /etc/docker/daemon.json and restart Docker:

/etc/docker/daemon.json
{
"dns": ["172.17.0.1"]
}
Terminal window
sudo systemctl restart docker

Adding Test Entries and Verifying

Append a test hostname mapping to /etc/hosts and reload dnsmasq:

Terminal window
sudo bash -c "echo '192.168.1.12 nonexisting.example.com' >> /etc/hosts"
sudo systemctl restart dnsmasq

Query the test domain from an ephemeral BusyBox container to confirm resolution flows through 172.17.0.1:

Terminal window
docker run --rm --name dns-test busybox nslookup nonexisting.example.com
Server: 172.17.0.1
Address: 172.17.0.1:53
Non-authoritative answer:
Name: nonexisting.example.com
Address: 192.168.1.12

Any hostname added to /etc/hosts on the Docker host is now immediately resolvable from every container without modifying individual service definitions.