Docker容器不使用主机DNS

时间:2018-11-07 06:33:52

标签: docker dns

我使用了名为“后端网络”的自定义docker网络

[root@localhost docker]# docker inspect backend-network
    {
        "Name": "backend-network",
        "Id": "18180c0c1ef14460a25b66b7fb971e090f7bb85f549921704d11937af70766c7",
        "Created": "2018-08-07T12:36:02.4175991+09:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Containers": {
            "531c1ecbe993ee13e632fbd9697b392ee989d756ff60c07eae96a700901aaa01": {
                "Name": "splash",
                "EndpointID": "c9e4e7ec319ecf9cdcbb9ca50170efb63c4fca33bcbbabb584c4a4e41576b15d",
                "MacAddress": "02:42:ac:12:00:05",
                "IPv4Address": "172.18.0.5/16",
                "IPv6Address": ""
            },
            "c6a5aa827e901b6b6d7b35d4a8be5a5b2fc73f1a7a385416ce200e847d400b21": {
                "Name": "flask",
                "EndpointID": "5d5abb3bc964d251379a7f6a84cb5b5d9bddac9b778f2222d52aba657b28dd34",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            },
            "da839143fb58d738e38922c669efa332c545fee4dd0a5b733583ed7b8df60875": {
                "Name": "django",
                "EndpointID": "f046e9cc93f895b12ce1c4de983fbe0e54a3904460c04db3ba238ba84ba82327",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "fc9e6ef183c81a3fe7dd29ecb5c17c0dc27fb803ef8e214d4f344a2b3407ec54": {
                "Name": "mongo",
                "EndpointID": "ab94182f4b175f105ab01ccbbc43b7dad37cf5506eee831168fd5bd9094ccde8",
                "MacAddress": "02:42:ac:12:00:04",
                "IPv4Address": "172.18.0.4/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }

但是每个容器未使用主机DNS。

主机DNS是。

[root@localhost docker]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.88.1

,容器DNS为

(django) root@da839143fb58:/opt/django_backend/scrapy_app# cat /etc/resolv.conf
nameserver 127.0.0.11
options ndots:0

我添加了

  

名称服务器192.168.88.1

容器的resolv.conf,它可以工作,但是请求时间太长。

我认为先搜索

  

名称服务器127.0.0.11

并搜索

  

名称服务器192.168.88.1

我如何设置docker-network来托管dns?

删除

  

名称服务器127.0.0.11

然后不按名称传达其他容器。喜欢

 (django) root@da839143fb58:/opt/django_backend/scrapy_app# ping splash
 ping: splash: Name or service not known

1 个答案:

答案 0 :(得分:0)

Docker容器正在通过嵌入式DNS服务器解析DNS请求(这是您在容器的/etc/resolv.conf中看到的IP –请参见documentation的底部注释)。根据您的配置,嵌入式DNS服务器会将查询转发到您的主机(默认)或其他DNS服务器。您可以使用--dns-标志传递自定义DNS服务器。

请在documentation中找到有关此信息的更多信息。