Docker 容器不从主机继承 dns 设置

Mat*_*hyn 7 docker docker-compose docker-swarm

由于某种原因,我使用 Docker Swarm 中的 Docker Compose 文件创建的作为堆栈一部分的 Docker 容器没有正确的网络设置。

在这里阅读有关网络的 Docker 文档,我看到:

By default, a container inherits the DNS settings of the Docker daemon, including the /etc/hosts and /etc/resolv.conf.You can override these settings on a per-container basis.

但是,当我使用 Docker Compose 文件创建新堆栈时,容器内的 /etc/resolv.conf 文件与主机的 /etc/resolv.conf 文件完全不同。因此,我无法连接到同一网络上的数据库服务器。

现在主机上的 /etc/resolv.conf 看起来像这样:

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8) # DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN nameserver 10.0.0.254 search localdomain

Docker 容器内部如下所示:

search localdomain nameserver 127.0.0.11 options ndots:0

要创建服务,我使用以下命令:

docker stack deploy --compose-file config.compose --with-registry-auth

Docker Compose 文件相当大,但基本上只是它应该启动的一个大服务列表。Docker Compose 文件中没有定义网络选项。

作为测试,我还启动了一个服务,而不是作为堆栈,该服务确实正确继承了 /etc/resolv.conf 文件。

有人知道为什么 dns 设置没有从主机正确继承吗?

系统规格:

操作系统:Ubuntu 16.04

Docker:18.03.1-ce,内部版本 9ee9f40