Docker Bridge网络DNS

时间:2018-01-21 08:10:54

标签: docker docker-compose docker-container docker-networking docker-network

我在默认的docker bridge网络上运行了两个容器。守护进程为他们分配一个"随意" ip在特定范围内。

network_mode: bridge -> 172.17.0.X

如何通过别名或名称相互联系?

1 个答案:

答案 0 :(得分:1)

让以下容器运行基本映像Ubuntu:16.04:

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
2461f410228b        ubuntu:16.04        "/bin/bash"         20 minutes ago      Up 20 minutes                           competent_shockley
6da9d8196637        ubuntu:16.04        "/bin/bash"         21 minutes ago      Up 21 minutes                           hungry_mccarthy

Docker默认网络,了解有关默认网络的详情,请参阅here

$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f83800356529        bridge              bridge              local
8169b2246500        host                host                local
b61e4f39c0ec        none                null                local

让我们使用docker inspect找出容器的IP地址:

$ docker inspect 2461f410228b | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.18.0.3",
                    "IPAddress": "172.18.0.3",


$ docker inspect 6da9d8196637 | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.18.0.2",

默认情况下,在BRIDGE网络中启动的容器将能够访问在同一网络中启动的其他容器

root@2461f410228b:/# ping 172.18.0.2
PING 172.18.0.2 (172.18.0.2) 56(84) bytes of data.
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.051 ms

当您正在寻找使用名称/别名到达其他容器时,请在文件/ etc / hosts中添加目标容器信息以解析DNS:

root@2461f410228b:/# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.18.0.3      2461f410228b
172.18.0.2      docker2

现在尝试使用主机名

到达目标容器
root@2461f410228b:/# ping docker2
PING docker2 (172.18.0.2) 56(84) bytes of data.
64 bytes from docker2 (172.18.0.2): icmp_seq=1 ttl=64 time=0.077 ms

我相信这可能在某种程度上有所帮助。