For Containers Using the Default Bridge Network (docker0)
The DNS settings are prioritized as follows:
--dnsoption or docker-compose settings- Docker configuration (
/etc/docker/daemon.json) - Inherited host configuration (
/etc/resolv.conf)
If multiple settings are provided, the former overrides the latter (they do not merge). Changes to these configurations require a container restart to take effect.
To test: Check the /etc/resolv.conf file inside the container.
For Containers Attached to a Custom Network (Your Case)
Note: The --dns option will not work as they use Docker’s embedded DNS (127.0.0.11). The DNS settings are prioritized as follows:
- Docker configuration, changes require a container restart to take effect
- Inherit host configuration, changes require reactivating the network connection to take effect (container restart is not necessary).
To test: Use dnsmasq to simulate a DNS server and use nslookup inside the container to check the resolution results.
Example: Setting Global DNS
If you want the DNS to take effect both on host and whthin containers, there’s no need to change your docker settings, just make sure all you containers inherit the host DNS configuration.
Assuming you are using NetworkManager, editing /etc/resolv.conf directly will be overridden after a service restart, so to make your changes persistent, follow these steps:
-
List network connections:
nmcli conn -
Modify the DNS settings on your host, for example:
nmcli conn modify ens192 ipv4.dns "8.8.8.8 8.8.4.4" -
Restart the containers using
docker0docker restart $(docker ps --filter network=bridge) -
Reactivate all the br-xx connections used by your containers:
nmcli conn up br-xxx
I wrote a bash script for this.