I try to config a dns for a container with --dns
but after connect another network for this container, the dns config has been reset.
So, Is it possible to reconfig the dns of the container?
Steps:
create a container with --dns
# docker run -d -t --name t1 --dns="223.5.5.5" busybox
538b414aa29738752e8f8b6e5e9c2ce40347d9195a4f1389ea5d7d1dbbf5d9c0
get the resolv.conf
# docker exec 53 cat /etc/resolv.conf
nameserver 223.5.5.5
create a network
# docker network create testNet1 --subnet 111.0.0.0/24
1fb10ce7f43b1a330f8d0218851364b77c9a1db52b1ddf9fef0335ae11f5c0c1
connect the network to the container
# docker network connect testNet1 t1
show the resolv.conf again
# docker exec 53 cat /etc/resolv.conf
nameserver 127.0.0.11
options ndots:0
some informations
# docker -v
Docker version 18.03.1-ce, build 9ee9f40
# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.4 LTS
Release: 16.04
Codename: xenial
# uname -a
Linux slt-docker 4.4.0-128-generic #154-Ubuntu SMP Fri May 25 14:15:18 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
For Containers Using the Default Bridge Network (docker0
)
The DNS settings are prioritized as follows:
--dns
option 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 docker0
docker 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.