使用 systemd 在操作系统上为 Docker 守护进程设置 DNS [英] Setting DNS for Docker daemon on OS with systemd
问题描述
Docker 的默认 DNS(例如 8.8.8.8)在我工作的地方被阻止,所以我想更改默认值.我已经能够使用
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>
但我想使用 systemd 插件来做到这一点,因为官方 Docker 文档推荐这种方式.我制作了一个 /etc/systemd/system/docker.service.d/dns.conf
文件,并使用了这样的东西:
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>
但我只是不知道变量名应该是什么.我该如何设置?更重要的是,是否有一个页面记录了可在 Docker 的 systemd 插件中使用的所有配置变量?
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?
(顺便说一句,这是 Ubuntu 15.10 上的 Docker 1.9,虽然我不怀疑任何错误)
(btw, this is Docker 1.9 on Ubuntu 15.10, although I don't suspect any bugs)
推荐答案
/etc/systemd/system/docker.service.d
中的所有 .conf
文件否决了/usr/lib/systemd/system/docker.service
文件中的设置,这几乎是您尝试过的.
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.
您需要从 /usr/lib/systemd/system/中复制
文件到 ExecStart=
部分,而不是放入 DNS=..
行docker.servicedns.conf
(或 mydocker.conf
).在 ExecStart
的 daemon
部分之后添加 --dns $ip
.例如:
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://
其中192.168.1.1
是dns服务器的ip.
Where the 192.168.1.1
is the ip of the dns server.
现在通过 systemctl 重新启动 docker,docker 现在应该使用您自己的 dns 重新启动.(可通过 systemctl status docker.service | grep dns
检查).
Now restart docker via systemctl and docker should now restart with your own dns. (Checkable via systemctl status docker.service | grep dns
).
请注意,空的 ExecStart=
是必需的,因为 systemctl 只有先清除 ExecStart 才会否决 ExecStart.
Note that the empty ExecStart=
is required, as systemctl only will overrule the ExecStart if it is cleared first.
还要注意在/etc/systemd/system/
中编辑文件后需要一个systemctl daemon-reload
.
Also note that a systemctl daemon-reload
is needed after editing files in /etc/systemd/system/
.
最后要说明的是,在某些系统上,docker.service
不在 /usr/lib/systemd/system/
中,而是在 /lib/systemd 中/system/
.
Last remark is that on some systems docker.service
is not located in /usr/lib/systemd/system/
, but in /lib/systemd/system/
.
这篇关于使用 systemd 在操作系统上为 Docker 守护进程设置 DNS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!