docker 启动时候还会有,名为docker0的虚拟网桥,注意网址为 127.0.0.1
[root@iZuf6hxabqikytnrumsi4gZ ~]# ifconfig
docker0: flags=4163 mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:7fff:fe81:d287 prefixlen 64 scopeid 0x20
ether 02:42:7f:81:d2:87 txqueuelen 0 (Ethernet)
RX packets 135 bytes 9419 (9.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 213 bytes 15282 (14.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
查看docker的 网络命令
docker network COMMAND
如下:
docker network ls 查看网络
docker network create 网络名 创建网络
docker network connect 网络名 为容器连接到网络
docker network disconnect 网络名 断开容器的网络
docker network inspect 网络名 查看网络源数据
docker network rm 网络名字 删除网络
docker network prune 删除所有无用的网络
Docker有四种网络模式:
- bridge模式:默认设置,容器和宿主机都配置在一个网络空间中。
- none模式:不配置任何网络参数,需要手动配置。
- host模式:容器和宿主机共享一个网络命名空间,不配置独立的网络空间。
- container模式:将容器设置为网络命名空间,使容器和宿主机有相同的IP地址和端口号。
查看容器ip,可以看到容器内的 IPAddress 与上面 docker0在同一个网段。
[root@iZuf6hxabqikytnrumsi4gZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
462ff8d8719e b66e5037b732 "bash" 3 minutes ago Up 31 seconds 80/tcp centosjava8
a34aff536643 redis:6.2.6 "docker-entrypoint.s…" 13 days ago Up 3 hours 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis6.2.6
2a384f369e0b mysql:5.7 "docker-entrypoint.s…" 2 weeks ago Up 3 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql5.7
2-查看最后二十行关于网络的配置
[root@iZuf6hxabqikytnrumsi4gZ ~]# docker inspect centosjava8|tail -n 20
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "a09227a0c60fe5b8afd1082d3ba2d5d7425d0f7aeafd861caf1d8103886f34c7",
"EndpointID": "9a3b00d21d96ec4e1bf34549b77429ecd835e0f69db4d92bc27e03354f75239b",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:04",
"DriverOpts": null
}
}
}
}
]
网桥模式通信示意图:
1、整个宿主机的网桥模式都是docker0,类似一个交换机有一堆接口,每个接口叫veth,在本地主机和容器内分别创建一个虚拟接口,并让他们彼比联通(这样一对接口叫veth pair) :
2、 每个容器实例内部也有一块网卡,每个接口叫eth0;
3、docker0上面的每个veth匹配某个容器实例内部的eth0,两两配对。所以:网桥模式下:docker0 相当于交换机一样功能,让各个容器通信
容器将不会获得一个独立的Network Namespace, 而是和宿主机共用一个Network Namespace。容器将不会虚拟出自己的网卡而是使用宿主机的IP和端口。
启动一个本地的镜像:以host 方式
docker run -it --network=host --name centosjava8 b66e5037b732 bash
容器共享宿主机ip,端口也是默认的。
1-查看容器
[root@iZuf6hxabqikytnrumsi4gZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74e40d517ff9 b66e5037b732 "/bin/sh -c /bin/bash" 3 minutes ago Up 3 minutes kind_kepler
a34aff536643 redis:6.2.6 "docker-entrypoint.s…" 2 weeks ago Up 7 hours 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis6.2.6
2a384f369e0b mysql:5.7 "docker-entrypoint.s…" 2 weeks ago Up 7 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql5.7
2-查看容器信息 看到host方式启动。与宿主机共用一份,ip网关都是空的
[root@iZuf6hxabqikytnrumsi4gZ ~]# docker inspect 74e40d517ff9|tail -n 20
"Networks": {
"host": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "f41a3dd68f79020a979539af616a2fadbb7cb95f18597a1611d4ef19198d9039",
"EndpointID": "331746e74ee6e7a53ffd2d32aa3d00ab15e53ff2037ec3b0584a2c1007e98247",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
[root@iZuf6hxabqikytnrumsi4gZ ~]#
在none模式下,并不为Docker容器进行任何网络配置。 也就是说,这个Docker容器没有网卡、IP、路由等信息,只有一个lo需要我们自己为Docker容器添加网卡、配置IP等。
新建的容器和已经存在的一个容器共享一个网络ip配置而不是和宿主机共享。新创建的容器不会创建自己的网卡,配置自己的IP,而是和一个指定的容器共享IP、端口范围等。同样,两个容器除了网络方面,其他的如文件系统、进程列表等还是隔离的。
示例如下:
借用容器 centosjava8 的配置,共用一个网桥,如果关闭centosjava8 容器那么生成的centosjava81也就没有ip
docker run -it --network container:centosjava8 --name centosjava81 b66e5037b732 bash
如图:
docker多个容器之间的集群规划要使用服务名,因为ip是会变动,使用自定义网络模式能够使用服务名进行通信
1-创建自定义网络
[root@iZuf6hxabqikytnrumsi4gZ ~]# docker network create mynetwork
f43d61b56f9e124f6ea6cd80d627d15990c7a9316974be773c33d7d232731212
2-查看自己创建的网络 mynetwork
[root@iZuf6hxabqikytnrumsi4gZ ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
e8026a20994a bridge bridge local
f41a3dd68f79 host host local
f43d61b56f9e mynetwork bridge local
9eae36819684 none null local
[root@iZuf6hxabqikytnrumsi4gZ ~]#
后台启动2个容器:
docker run -it --network mynetwork --name t1 centosjava8:1.0
docker run -it --network mynetwork --name t2 centosjava8:1.0
过程如下
1-查看镜像
[root@syf ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centosjava8 1.0 b66e5037b732 8 days ago 1.27GB
redis 6.2.6 7614ae9453d1 24 months ago 113MB
mysql 5.7 c20987f18b13 24 months ago 448MB
[root@syf ~]# docker run -it --network mynetwork --name t1 centosjava8:1.0
[root@syf ~]# docker run -it --network mynetwork --name t2 centosjava8:1.0
2-查看容器
[root@syf ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9bc9c68b9582 centosjava8:1.0 "/bin/sh -c /bin/bash" 27 seconds ago Up 26 seconds 80/tcp t2
e50315d22041 centosjava8:1.0 "/bin/sh -c /bin/bash" About a minute ago Up About a minute 80/tcp t1
a34aff536643 redis:6.2.6 "docker-entrypoint.s…" 2 weeks ago Up 6 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis6.2.6
2a384f369e0b mysql:5.7
3-查看 t1、t2 2个容器都是在一个网段上
[root@syf ~]# docker inspect t1|tail -n 20
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "de322bc41064e1f7314af8f991d7b984a975c890ebbcf505598f2a24bea7e52e",
"EndpointID": "5ab83e65694b064a8a7237a5e53fe28c6170502de8a10af0af2e8a06583057f0",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:04",
"DriverOpts": null
}
}
}
}
]
[root@syf ~]# docker inspect t2|tail -n 20
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "de322bc41064e1f7314af8f991d7b984a975c890ebbcf505598f2a24bea7e52e",
"EndpointID": "0c281bffcded7267cd9ce428ecdc9be6fb6f375b35d02f1e62080a5ac28f3ee1",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.5",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:05",
"DriverOpts": null
}
}
}
}
]
测试
1-进入t1容器 ping t2
[root@syf ~]# docker exec -it t1 bash
2- 用服务名也可以ping通
[root@9bc9c68b9582 local]# ping t2 -c 3
PING 172.17.0.5 (172.17.0.5) 56(84) bytes of data.
64 bytes from 172.17.0.5: icmp_seq=1 ttl=64 time=0.104 ms
64 bytes from 172.17.0.5: icmp_seq=2 ttl=64 time=0.082 ms
64 bytes from 172.17.0.5: icmp_seq=3 ttl=64 time=0.067 ms