24

Due to local network configuration I have to add --dns and --dns-search options to my docker run commands like so:

docker run --dns XX.XX.1.1 --dns-search companydomain -t mycontainer

Is there an environment variable or config file where I can add the DNS options so that I don't have to type them each time I want to run a container?

I'm using docker on Ubuntu 16.04 running on VMware VM hosted on a Windows machine.

Thank you.

Julio César
  • 12,790
  • 10
  • 38
  • 45

1 Answers1

62

You can add these options to the docker daemon as the default for all the containers it runs. On the dockerd command line, the options are the same, --dns XX.XX.1.1 --dns-search companydomain. To avoid changing the startup scripts to add that option, it's easier to setup an /etc/docker/daemon.json file with the following contents:

{
  "dns": ["XX.XX.1.1"],
  "dns-search": ["companydomain"]
}

Then restart the docker daemon with systemctl restart docker to apply the change.

You may find it better to update the /etc/resolv.conf in your VM to apply this change to not on the docker containers, but the VM itself. That would look like:

nameserver XX.XX.1.1
search companydomain

If updating the resolv.conf, be sure that it's not managed by another tool, e.g. if you are getting dhcp addresses for your VM this would be overwritten by the dhcp client daemon.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 2
    Thank you very much for this answer, it was not easy to find this information in the official docker documentation (which, apart from that is usually very clear) – Tobbey May 25 '18 at 09:53
  • It worked out of the box with Hyper-V. Just got a new machine and started using WSL2 and now I need to specify the DNS for some reason. Thanks for the fix! – ps2goat Oct 15 '20 at 22:10
  • And of course, after posting, I looked at my WSL network adapter settings. For IPv4, it was set to use manual DNS settings, but none were listed. I reverted my Docker Daemon settings from this answer, rebooted Docker, and just put a manual DNS entry in my adapter settings. It's still working. – ps2goat Oct 15 '20 at 22:17