Docker - Assign DNS servers to a container using the --dns option

by
Jeremy Canfield |
Updated: January 26 2025
| Docker articles
By default, when a container is created and started using the docker run command, the container will have the DNS servers listed in the /etc/resolv.conf file on the Docker system. For example, let's say the docker system has the following.
~]# cat /etc/resolv.conf
nameserver 10.14.57.1
nameserver 10.14.57.2
search .
The docker exec command can be used to see that the container has inherited /etc/resolv.conf.
~]# docker exec container_name_or_id cat /etc/resolv.conf
nameserver 10.14.57.1
nameserver 10.14.57.2
search .
When creating a container using the docker run command the --dns option can be used to override this behavior
sudo docker run --dns 192.168.0.2 --dns 192.168.0.3 my-container
Of if the container already exists the docker update command can be used.
sudo docker update --add-host foo.example.com:172.16.0.2 my-container
You can use the docker inspect command to view the JSON for the container.
~]$ sudo docker inspect www | jq .[].HostConfig.Dns
[192.168.0.2]
~]$ sudo docker inspect www | jq .[].HostConfig.DnsOptions
[]
~]$ sudo docker inspect www | jq .[].HostConfig.DnsSearch
[]
Did you find this article helpful?
If so, consider buying me a coffee over at