由于某些原因,我在Docker Swarm中使用Docker合成文件创建的Docker容器作为堆栈的一部分,没有正确的网络设置。
阅读Docker网络文档
here
我明白了:
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文件完全不同。因此,我无法连接到同一网络上的数据库服务器。
# 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