15

The default DNS for Docker (e.g. 8.8.8.8) is blocked where I work, so I want to change the default. I've been able to do this using

$ docker daemon --dns <mydnsaddress>

but I want to do this using a systemd drop-in instead, since the official Docker docs recommend this way. I've made a /etc/systemd/system/docker.service.d/dns.conf file, and used things like this:

[Service]
DNS=<mydnsaddress>

But I just have no idea what the variable name is supposed to be. How do I set this? More importantly, is there a page that documents all config variables that can be used in systemd drop-ins for Docker?

(btw, this is Docker 1.9 on Ubuntu 15.10, although I don't suspect any bugs)

labyrinth
  • 13,397
  • 7
  • 35
  • 44
  • 1
    The docker documentation describes how the DOCKER_OPTS can be set in the /etc/default/docker file: https://docs.docker.com/engine/articles/configuring/ – Mark O'Connor Nov 18 '15 at 22:38

2 Answers2

42

All .conf files in /etc/systemd/system/docker.service.d overrule the settings from the /usr/lib/systemd/system/docker.service file, which is almost what you tried.

Instead of putting a DNS=.. line in, you need to copy the ExecStart= part from the /usr/lib/systemd/system/docker.service file to dns.conf (or mydocker.conf). Add --dns $ip after the daemon part of the ExecStart. E.g.:

[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon --dns 192.168.1.1 -H fd://

Where the 192.168.1.1 is the ip of the dns server.

Now restart docker via systemctl and docker should now restart with your own dns. (Checkable via systemctl status docker.service | grep dns).

Note that the empty ExecStart= is required, as systemctl only will overrule the ExecStart if it is cleared first.

Also note that a systemctl daemon-reload is needed after editing files in /etc/systemd/system/.

Last remark is that on some systems docker.service is not located in /usr/lib/systemd/system/, but in /lib/systemd/system/.

steviethecat
  • 870
  • 9
  • 14
  • 3
    Thank you this works. But is is quite convoluted … this is something that used to be easier, before systemd. – Torsten Bronger Sep 15 '16 at 07:39
  • For docker 18.06.0-ce on Ubuntu Bionic I needed `/usr/bin/dockerd --dns a.b.c.d -H fd://` (where a.b.c.d is your DNS server IP address) – Spacedman Jul 20 '18 at 11:08
  • 3
    It is also possible to put the option in `/etc/docker/daemon.json`. It is unfortunately somewhat poorly documented; its format is not described in the man page, but there is something in the [online documentation](https://docs.docker.com/engine/reference/commandline/#daemon-configuration-file). – Jan Hudec Mar 08 '19 at 10:08
3

Yes I agreed to previous answer given by @steviethecat but this changes overwrite to default when docker restart so I followed below steps. Using Docker version 18.09.2,

I followed link https://success.docker.com/article/using-systemd-to-control-the-docker-daemon

sudo systemctl edit docker //this opens new file use as overwrite file.

add below lines. Make sure you have ExecStart= before setting this value. Above given link having details.

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --dns 192.168.1.1 -H fd://

once above lines added to file, execute below lines.

sudo systemctl daemon-reload
systemctl restart docker
systemctl status docker
Community
  • 1
  • 1