Setting DNS for containers in a docker environment

Table of Contents

Setting DNS for all containers in a docker environment

Server

I have noticed that all my containers default to 8.8.8.8 google dns because they are unable to connect to the DNS servers configured for my network.

I have been using a dirty hack to set my own DNS servers. I knew this wasn’t the correct way to do it. i.e making a resolv.conf in the docker directory and mounting it at /etc/resolv.conf in the container :D

It works but isn’t the correct and proper way to do it.

Configuring /etc/docker/daemon.json

“The Correct Way”

You can set the DNS for the docker environment in the /etc/docker/daemon.json file.

 sudoedit /etc/docker/daemon.json

If asked chose a text editor of choice, create the file if it doesn’t exist and add this to the file.

{
    "dns": ["<DNS_IP>", "<DNS2_IP>"]
}

Mine looks like this, daemon.json

Now restart the docker daemon:

sudo systemctl restart docker

The containers should use the provided DNS servers now.

comments powered by Disqus