Adding Domains and IP Addresses to Docker Containers
/ 2 min read
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:
which dnsmasqIf missing, install it from the distribution package manager:
# Debian / Ubuntusudo apt install dnsmasq
# RHEL / CentOS / Fedorasudo dnf install dnsmasqConfiguring Dnsmasq
Dnsmasq Standalone Usage
When NetworkManager is not managing host DNS, configure dnsmasq directly in /etc/dnsmasq.conf:
# Listen on the Docker bridge interfaceinterface=docker0
# Specify an upstream DNS server to forward queriesserver=8.8.8.8Enable and verify the service:
sudo systemctl stop dnsmasq && \sudo systemctl enable --now dnsmasq && \sudo systemctl status dnsmasqDnsmasq 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]:
[main]dns=dnsmasqThen create a dedicated snippet under /etc/NetworkManager/dnsmasq.d/split-dns.conf:
# Listen on the Docker bridge interfaceinterface=docker0
# Specify an upstream DNS server to forward queriesserver=8.8.8.8Restart NetworkManager and confirm the service stays healthy:
sudo systemctl restart NetworkManager && \sleep 5 && \sudo systemctl status NetworkManagerIf resolution fails or the plugin refuses to bind, inspect the NetworkManager logs:
journalctl -u NetworkManager -rPointing Docker to the Bridge Resolver
Check the IPv4 address assigned to the docker0 bridge:
ip a show docker04: 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 foreverSet 172.17.0.1 as the default DNS server in /etc/docker/daemon.json and restart Docker:
{ "dns": ["172.17.0.1"]}sudo systemctl restart dockerAdding Test Entries and Verifying
Append a test hostname mapping to /etc/hosts and reload dnsmasq:
sudo bash -c "echo '192.168.1.12 nonexisting.example.com' >> /etc/hosts"sudo systemctl restart dnsmasqQuery the test domain from an ephemeral BusyBox container to confirm resolution flows through 172.17.0.1:
docker run --rm --name dns-test busybox nslookup nonexisting.example.comServer: 172.17.0.1Address: 172.17.0.1:53
Non-authoritative answer:Name: nonexisting.example.comAddress: 192.168.1.12Any hostname added to /etc/hosts on the Docker host is now immediately resolvable from every container without modifying individual service definitions.