Docker Community Forums

Share and learn in the Docker community.

DNS in container for other containers in different subnets

Hi Guys,

Seeking for an advise for your side on what looks as very simple problem - get containerized DNS working for other containers in different docker subnets.
Scenario is as following:
1.have local DNS running in container (pihole/dnsmasq, but it doesn’t matter),
2. port 53 is exposed via bridge net (dedicated)
3. all works fine for docker host and outside world
4. this DNS server should be used by all containers

Detailed explanation of above:
The problem is only with the last part - there’s no way I can get it working.
Going into detail we have network: where DNS is located, let.s say at ip .5
Port mapping is done from host using bridge subnet (as more ports are used by DNS server I don’t want to expose at host level, or am proxying to them for other reasons - pihole 443, 80, etc.). is external IP on docker host.
All hosts can use this DNS using

Docker containers in different subnets, other than can’t get access to this DNS using IP for obvious docker reasons.
Idea of using docker Host IP seems to be wise, but it gets DNAT to and reply comes back, without reverting the NAT back to container in i.e. and is dropped as has wrong source IP.

For me this is not a feature but a bug, as packet should go through network stack and following typical Linux/iptables get back the original destination IP ( set as source.

Workaround could be a manual iptables rule, masking any traffic from any containers when going to the DNS IP (either docker host IP or internal docker container, but then forwarding rule would be required too).

What are your odds on it?


I assume you are aware that by default the to hosts /etc/resolve.conf is injected into the containers, and whatever dns servers are listed there are available within your containers?

Though, you can influence the dns setting from the cli ( --dns= or in a docker-compose.yml ( dns:

Yup, that I’ve tried, same as other workarounds.
Problem is how networking is handled for containers reaching out to docker host IP.
Don’t have real tcpdump at hand but it was more or less like. -> # this is client container in different subnet reaching out to docker host IP with DNS query -> # same packet after DNAT as per the fact that :53 from is exposed on host -> # reply with DNS response - note it uses original src IP as DST and it’s own IP (ok) -> # this is after it passed docker host iptables. It went thorough as Connection Established/related from iptables point of view, but somehow skipped the NAT table.

This packet is being dropped by nslookup library on querying host as response comes from unexpected IP.

Workaround I’ve found is to set the DNS on docker host to and then different set of iptables rules is being applied at host level.
Same behavior should be implemented for host IP related traffic or to be able to control it somehow.