【目录导览】
1.Docker入门:什么是Docker?如何安装Docker?
2.Docker入门:Docker命令
3.Docker实战:Docker部署Nginx
4.Docker实战:Docker部署Tomcat
5.Docker实战:Docker部署MySQL
6.Docker镜像:什么是Docker镜像?Docker镜像加载原理?
7.Docker容器:什么是Docker容器?
8.Docker数据卷:数据交互、数据卷、数据卷容器
9.Docker镜像制作:Commit和DockerFile
10.Docker入门学习回顾小结
11.Docker网络:基础原理&示例
12.Docker网络:容器互联之–link
13.Docker网络:容器互联之自定义网络
14.Docker网络:容器互联之不同网络间的容器互联
上一节中已经创建好一个自定义网络my_net,于是我们可以测试Docker默认的bridge网络和自定义my_net网络的连通性,也可以再创建一个自定义网络my_net-1,测试my_net和my_net-1之间的连通性,本节以两个自定义网络为例测试。
# 创建自定义网络my_net-1
docker network create --driver bridge --subnet 192.167.0.0/24 --gateway 192.167.0.1 my_net-1
# 参数解释:
# --driver bridge 网络模式设置为桥接模式
# --subnet 192.167.0.0/24 子网划分
# --gateway 192.167.0.1 设置网关
# my_net-1 设置自定义网络的名字
# 查看Docker的网络信息
docker network ls
# 内容如下:
[root@iZ2zefjrz9szlt99tuu23fZ wy]# docker network ls
NETWORK ID NAME DRIVER SCOPE
c5440000e49f bridge bridge local
c115f00b8471 host host local
d3f68c147ecc my_net bridge local
4d12106e517b my_net-1 bridge local
4fdfe488370c none null local
# 可以看到多了一个my_net-1网络
docker run -d -P --name tomcat-1-bak --net my_net-1 tomcat
docker run -d -P --name tomcat-2-bak --net my_net-1 tomcat
# 查看容器列表
docker ps
# 内容如下:
[root@iZ2zefjrz9szlt99tuu23fZ wy]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2e7ff385e64 tomcat "catalina.sh run" 5 seconds ago Up 4 seconds 0.0.0.0:32776->8080/tcp tomcat-2-bak
ca6f5c322e63 tomcat "catalina.sh run" 6 seconds ago Up 5 seconds 0.0.0.0:32775->8080/tcp tomcat-1-bak
e6538d8bb4a0 tomcat "catalina.sh run" 36 minutes ago Up 36 minutes 0.0.0.0:32774->8080/tcp tomcat-2
28aabfacad76 tomcat "catalina.sh run" 39 minutes ago Up 39 minutes 0.0.0.0:32773->8080/tcp tomcat-3
38a4932d7692 tomcat "catalina.sh run" About an hour ago Up About an hour 0.0.0.0:32771->8080/tcp tomcat-1
docker exec -it tomcat-1 ping tomcat-1-bak
# 内容如下:
[root@iZ2zefjrz9szlt99tuu23fZ wy]# docker exec -it tomcat-1 ping tomcat-1-bak
ping: tomcat-1-bak: Name or service not known
可以看到,不同的网络之间不能直接通信
# 使用connect将A网络中的容器与B网络中的网关打通
docker network connect my_net tomcat-1-bak
# 再次测试tomcat-1与tomcat-1-bak之间的网络连通性
docker exec -it tomcat-1 ping tomcat-1-bak
# 内容如下:
[root@iZ2zefjrz9szlt99tuu23fZ wy]# docker exec -it tomcat-1 ping tomcat-1-bak
PING tomcat-1-bak (192.168.0.5) 56(84) bytes of data.
64 bytes from tomcat-1-bak.my_net (192.168.0.5): icmp_seq=1 ttl=64 time=0.070 ms
64 bytes from tomcat-1-bak.my_net (192.168.0.5): icmp_seq=2 ttl=64 time=0.061 ms
可以看到,my_net网络中的tomcat-1容器与my_net-1网络中的tomcat—1-bak能通信了
# 此时查看一下my_net网络详情
docker network inspect my_net
# 内容如下:
[root@iZ2zefjrz9szlt99tuu23fZ wy]# docker network inspect my_net
[
{
"Name": "my_net",
"Id": "d3f68c147eccd5cfd1b82de45258cd16b6fea5b7ec964903c770ce5abb3b7448",
"Created": "2020-08-31T18:13:33.323655501+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {
},
"Config": [
{
"Subnet": "192.168.0.0/24",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"28aabfacad7609261febb526cf4576fcfa4e5add9bb520f2176d9ee62d283215": {
"Name": "tomcat-3",
"EndpointID": "ac656cb82c269c712d5f008b3b2f40409376bee965ac1b35a56b34d44814c5c1",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/24",
"IPv6Address": ""
},
"38a4932d76927f1836fb471a6592612d1e17d771557284162dad8d75687ed085": {
"Name": "tomcat-1",
"EndpointID": "7d367237c259f98086e27984d0b8b49464afcd8540a92bb78012d170ad422f2e",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/24",
"IPv6Address": ""
},
"ca6f5c322e63ab7ded08899223cda9248a3c16c62ef158aa95c13284d2c2321e": {
"Name": "tomcat-1-bak",
"EndpointID": "e8fe285ee474d3830b6f0eb035df168f18d7bb8e848a9c5cc81b134209527fd5",
"MacAddress": "02:42:c0:a8:00:05",
"IPv4Address": "192.168.0.5/24",
"IPv6Address": ""
},
"e6538d8bb4a062f2051eeb490da92e04ca916ded1039ff4ebf84dbb964d1bf4c": {
"Name": "tomcat-2",
"EndpointID": "635851bc5d7d7b24bc78742d26a06e74edf0753d9b6904b937acd5bf161c60a1",
"MacAddress": "02:42:c0:a8:00:04",
"IPv4Address": "192.168.0.4/24",
"IPv6Address": ""
}
},
"Options": {
},
"Labels": {
}
}
]
可以看到tomcat-1-bak也在my_net网络中了,就像是tomcat-1-bak容器也基于my_net网络创建了一次。简单来说,一个容器两个ip地址。
对于tomcat-1-bak容器来说,是将一个容器加入到两个网络中
对于my_net网络来说,是给一个网络多加了一个网卡而已