Docker容器互访背景
Docker容器在默认情况下处于相互隔离的状态,无法直接进行通信。然而,当存在服务依赖关系时,必须解决容器间的互访需求。以下将详细阐述三种可行的解决方案。
基于虚拟IP的访问方式
方式一:基于虚拟IP的访问
在安装Docker时,系统会默认创建一个名为docker0的内部桥接网络。每当启动一个新容器,都会为其分配一个虚拟网卡,使得容器之间能够通过各自的IP地址互相访问。
[root@33fcf82ab4dd /]# [root@CentOS ~]# ifconfig
......
docker0: flags=4163 mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:35ff:feac:66d8 prefixlen 64 scopeid 0x20
ether 02:42:35:ac:66:d8 txqueuelen 0 (Ethernet)
RX packets 4018 bytes 266467 (260.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4226 bytes 33935667 (32.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
......
首先运行一个centos镜像,并检查其IP地址,结果显示为:172.17.0.7
[root@CentOS ~]# docker run -it --name centos-1 docker.io/centos:latest
[root@6d214ff8d70a /]# ifconfig
eth0: flags=4163 mtu 1500
inet 172.17.0.7 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:7 prefixlen 64 scopeid 0x20
ether 02:42:ac:11:00:07 txqueuelen 0 (Ethernet)
RX packets 16 bytes 1296 (1.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 648 (648.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
使用相同的命令再次启动一个容器,查看其IP地址,结果为:172.17.0.8
[root@CentOS ~]# docker run -it --name centos-2 docker.io/centos:latest
[root@33fcf82ab4dd /]# ifconfig
eth0: flags=4163 mtu 1500
inet 172.17.0.8 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:8 prefixlen 64 scopeid 0x20
ether 02:42:ac:11:00:08 txqueuelen 0 (Ethernet)
RX packets 8 bytes 648 (648.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 648 (648.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
在容器内部进行的ping测试验证了连通性,结果如下:
[root@33fcf82ab4dd /]# ping 172.17.0.7
PING 172.17.0.7 (172.17.0.7) 56(84) bytes of data.
64 bytes from 172.17.0.7: icmp_seq=1 ttl=64 time=0.205 ms
64 bytes from 172.17.0.7: icmp_seq=2 ttl=64 time=0.119 ms
64 bytes from 172.17.0.7: icmp_seq=3 ttl=64 time=0.118 ms
64 bytes from 172.17.0.7: icmp_seq=4 ttl=64 time=0.101 ms
这种方式的局限性在于必须预先知晓每个容器的具体IP地址,在实际生产环境中并不具备很高的实用性。
使用link参数连接容器
方式二:使用link参数
在运行容器时,可以通过添加--link参数来实现容器间的链接。
首先运行第一个容器:
docker run -it --name centos-1 docker.io/centos:latest
接着运行第二个容器,并指定链接参数:
[root@CentOS ~]# docker run -it --name centos-2 --link centos-1:centos-1 docker.io/centos:latest
--link参数的格式中,第一个centos-1代表源容器名,第二个centos-1是定义的容器别名(用于访问目标容器)。为了便于记忆和使用,通常将别名设置为与容器名一致。
经过测试,ping通结果如下:
[root@e0841aa13c5b /]# ping centos-1
PING centos-1 (172.17.0.7) 56(84) bytes of data.
64 bytes from centos-1 (172.17.0.7): icmp_seq=1 ttl=64 time=0.210 ms
64 bytes from centos-1 (172.17.0.7): icmp_seq=2 ttl=64 time=0.116 ms
64 bytes from centos-1 (172.17.0.7): icmp_seq=3 ttl=64 time=0.112 ms
64 bytes from centos-1 (172.17.0.7): icmp_seq=4 ttl=64 time=0.114 ms
该方法对容器的创建顺序有严格要求。如果集群内多个容器需要相互访问,这种方式的配置会变得非常繁琐和不方便。
创建自定义bridge网络
方式三:创建自定义bridge网络
1. 安装好Docker后,执行以下命令创建名为testnet的bridge网络:docker network create testnet
查询确认新创建的bridge网络testnet已存在。

2. 启动容器并将其连接到testnet网络。
使用方法示例:docker run -it --name <容器名> --network testnet --network-alias <网络别名> <镜像名>
[root@CentOS ~]# docker run -it --name centos-1 --network testnet --network-alias centos-1 docker.io/centos:latest
[root@CentOS ~]# docker run -it --name centos-2 --network testnet --network-alias centos-2 docker.io/centos:latest
3. 从一个容器ping另一个容器,测试结果如下:
[root@fafe2622f2af /]# ping centos-1
PING centos-1 (172.20.0.2) 56(84) bytes of data.
64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=1 ttl=64 time=0.158 ms
64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=2 ttl=64 time=0.108 ms
64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=3 ttl=64 time=0.112 ms
64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=4 ttl=64 time=0.113 ms
4. 若需访问容器内的具体服务,可以使用<网络别名>:<服务端口号>的方式进行连接。
强烈推荐使用这种方法。自定义网络允许使用网络别名,无需担心IP地址变动的问题,只要连接到Docker内部的bridge网络即可实现互访。此外,可以创建多个bridge网络以实现不同网段的隔离。
以上即为Docker容器互访三种方法的详细内容。如需获取更多关于Docker容器互访的资料,请关注本站其他相关文章!