--link
docker run -d -P --name mytomcat01 tomcat
# mytomcat02容器通过 --link 连接mytomcat01
docker run -d -P --name mytomcat02 --link mytomcat01 tomcat
测试
docker exec -it mytomcat02 ping mytomcat01
docker exec -it mytomcat01 ping mytomcat02
在使用mytomcat02 连通 mytomcat01测试是没有问题的,而mytomcat01 连通 mytomcat02是不行的。
自定义网络
查看所有的docker网络
网络模式
bridge:桥接,docker默认的模式
none:不配置网络
host:和宿主机共享网络
container:容器内网络连通(用得少, 局限性很大)
# docker默认启动容器网络 --net bridge
docker run -d -P --name mytomcat tomcat
docker run -d -P --name mytomcat --net bridge tomcat
创建自定义网络
- 使用bridge桥接模式
- 子网掩码192.168.0.0/16
- 网关192.1678.0.1
- 设置自定义网络名称为mynet
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
查看所有的docker网络,可以看到新定义的mynet网络规则
docker network ls
查看自定义网络mynet的详细配置
docker network inspect mynet
使用自定义网络进行测试
启动两个tomcat容器进行网络测试
docker run -d -P --name mytomcat01 --net mynet tomcat
docker run -d -P --name mytomcat02 --net mynet tomcat
再次查看自定义网络,会发现启动的两个tomcat容器分别占用了192.168.0.2和192.168.0.3。
[root@berbai01 ~]# docker network inspect mynet
[
{
"Name": "mynet",
"Id": "d20f927dca673bfed0171621dbaa7a188e1ace53527254fafd563b84a9b4b126",
"Created": "2021-08-28T16:10:48.145033744+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"47cc0d4b74a1102fde953848139f6861e35d4d82ac54a676631c4b15bd8edfae": {
"Name": "mytomcat01",
"EndpointID": "023dff7f5fd886722583cae01182d0d864e3af645cfbdbccbd7d74b74edd0cf6",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"a1c5e80a0a09acb38eb07e6abdca6e4d85c0c5da9ee773e69201ff14d43e337b": {
"Name": "mytomcat02",
"EndpointID": "e2a8ada261cccbed8f84ade28c4f390f73643a7236e958be560eaf0c1bf947c8",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
测试两个容器的网络连通情况
# 通过容器的ip访问
docker exec -it mytomcat01 ping 192.168.0.3
# 通过容器名访问
docker exec -it mytomcat01 ping mytomcat02
docker exec -it mytomcat02 ping mytomcat01
两者都会可以连通的
小结
docker使用自定义网络,可以自动实现容器的互联。
使用自定义网络的优势:在搭建集群时,可以根据不同的集群使用不同的网络,保证集群是安全和健康的
不同网络的连通
连通所使用的指令
docker network connect [OPTIONS] NETWORK CONTAINER
用默认bridge模式和自定义mynet模式进行测试
# 默认bridge模式运行
docker run -d -P --name tomcat01 tomcat
docker run -d -P --name tomcat02 tomcat
# 自定义mynet模式运行
docker run -d -P --name tomcatmynet01 --net mynet tomcat
先测试tomcat01和tomcat02能不能连通tomcatmynet01
docker exec -it tomcat01 ping tomcatmynet01
docker exec -it tomcat02 ping tomcatmynet01
显然是不能连通的。
查看bridge模式和mynet模式的配置
docker network inspect bridge
docker network inspect mynet
bridge模式网段是171.17.0.0/16
mynet模式网段是192.168.0.0/16
学过网络原理的同学都知道不同网段是不能进行连通的。
[root@berbai01 ~]# docker network inspect bridge
[
{
"Name": "bridge",
"Id": "1f223afddbadd639a936453c1eb184edf9f6ae9219e31babd69e240a39f5c195",
"Created": "2021-08-28T16:28:14.602655353+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"76c15c6238d6485d5379624300d4ea7f3796ead8f7df5ac38e29029ab7892d3a": {
"Name": "tomcat01",
"EndpointID": "63cb04611404ba6668319d76af24f12830b7db09a597a625ad88f60b576af1ff",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
},
"b7cd5d28c3a6388cae45da8d1399ddf22a303a1f5899efaa673290c3a411cc32": {
"Name": "tomcat02",
"EndpointID": "57dc989fb590b9431ae9aed7b291362c224e11ad1f861e7e84bd859f1b5589e9",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
[root@berbai01 ~]#
[root@berbai01 ~]# docker network inspect mynet
[
{
"Name": "mynet",
"Id": "d20f927dca673bfed0171621dbaa7a188e1ace53527254fafd563b84a9b4b126",
"Created": "2021-08-28T16:10:48.145033744+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"f554e062918dc35ac7e1ce858b7cb9fc9e5f0a7eaadc90bdce8fdaad00b9b1ae": {
"Name": "tomcatmynet01",
"EndpointID": "3e4b65c4826425e1d11397544055e0aa33cf113ccf7045f9c20b866ba6a5cdb3",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
[root@berbai01 ~]#
建立bride模式容器与mynet模式容器的连接
docker network connect mynet tomcat01
# 再次查看mynet配置
docker network inspect mynet
这里发现tomcat01也加入了mynet网络中,说明tomcat01容器已经可以与mynet网络中容器连通。下面通过测试也可以得知。
[root@berbai01 ~]# docker network inspect mynet
[
{
"Name": "mynet",
"Id": "d20f927dca673bfed0171621dbaa7a188e1ace53527254fafd563b84a9b4b126",
"Created": "2021-08-28T16:10:48.145033744+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"76c15c6238d6485d5379624300d4ea7f3796ead8f7df5ac38e29029ab7892d3a": {
"Name": "tomcat01",
"EndpointID": "0852fbe26a3d37668c2deff948671bc6a52749068f518104f99e8ad5ad3d7057",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
},
"f554e062918dc35ac7e1ce858b7cb9fc9e5f0a7eaadc90bdce8fdaad00b9b1ae": {
"Name": "tomcatmynet01",
"EndpointID": "3e4b65c4826425e1d11397544055e0aa33cf113ccf7045f9c20b866ba6a5cdb3",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
[root@berbai01 ~]#
再次测试tomcat01与tomcatmynet01的连通
docker exec -it tomcat01 ping tomcatmynet01
经测试tomcat01已经可以与tomcatmynet01进行连通
tomcat02与tomcatmynet01的连通情况呢?
docker exec -it tomcat02 ping tomcatmynet01
可以看到如果没有与mynet进行connect,是不能进行连通的
小结
docker容器的网络互通其实就是赋予了一个容器多个ip,如上述试验中tomcat01被新赋予了ip192.168.0.3,于是tomcat01可以完成与mynet网络中容器进行连通。
而tomcat02并没有与mynet进行connect连接,没有与mynet相同的网段地址,依然不能与mynet中容器进行连通。
以上如有错误,麻烦大家评论指正。