自己局域网内部容器名字互相联通:
创建自定义名字:
# bridge网络,网络名字:hsjnet,B类网络192.168.0.0/16
[root@192 ~]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 hsjnet
启动两个tomcat容器:
[root@192 ~]# docker run -d -P --name tomcat-hsjnet-001 --network hsjnet tomcat
91837c70b1426b93bc4969c3aa80c63c9f9c694225d0e77d18121a5fd3c9fb80
[root@192 ~]# docker run -d -P --name tomcat-hsjnet-002 --network hsjnet tomcat
50ea9326b41af5b7d06e3bcd74e4358c605c5f133b689846b0054c0832cc9d00
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
50ea9326b41a tomcat "catalina.sh run" 3 seconds ago Up 2 seconds 0.0.0.0:49157->8080/tcp, :::49157->8080/tcp tomcat-hsjnet-002
91837c70b142 tomcat "catalina.sh run" 10 seconds ago Up 8 seconds 0.0.0.0:49156->8080/tcp, :::49156->8080/tcp tomcat-hsjnet-001
查看使用hsjnet网络的容器IP:
[root@192 ~]# docker network inspect hsjnet
[
{
"Name": "hsjnet",
"Id": "e4ed63b42a2632f11967c439412d86928078bdf2cdc5ccd28c14638139dd74b2",
"Created": "2021-08-24T14:54:55.584126839+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": {
"50ea9326b41af5b7d06e3bcd74e4358c605c5f133b689846b0054c0832cc9d00": {
"Name": "tomcat-hsjnet-002",
"EndpointID": "36bec44cec52fadd60ac1577bf70908aade4bccc3f6cee863b1f6ce6de705b37",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
},
"91837c70b1426b93bc4969c3aa80c63c9f9c694225d0e77d18121a5fd3c9fb80": {
"Name": "tomcat-hsjnet-001",
"EndpointID": "a8d4279badf98b22782cf2ca61b0ef3a5d08f53e6853f78e27779dbcb7afc8d5",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
容器名字互相ping操作:
[root@192 ~]# docker exec -it tomcat-hsjnet-001 ping tomcat-hsjnet-002
PING tomcat-hsjnet-002 (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat-hsjnet-002.hsjnet (192.168.0.3): icmp_seq=1 ttl=64 time=0.687 ms
64 bytes from tomcat-hsjnet-002.hsjnet (192.168.0.3): icmp_seq=2 ttl=64 time=0.150 ms
64 bytes from tomcat-hsjnet-002.hsjnet (192.168.0.3): icmp_seq=3 ttl=64 time=0.268 ms
[root@192 ~]# docker exec -it tomcat-hsjnet-002 ping tomcat-hsjnet-001
PING tomcat-hsjnet-001 (192.168.0.2) 56(84) bytes of data.
64 bytes from tomcat-hsjnet-001.hsjnet (192.168.0.2): icmp_seq=1 ttl=64 time=0.148 ms
64 bytes from tomcat-hsjnet-001.hsjnet (192.168.0.2): icmp_seq=2 ttl=64 time=0.222 ms
64 bytes from tomcat-hsjnet-001.hsjnet (192.168.0.2): icmp_seq=3 ttl=64 time=0.144 ms
跨局域网内部容器名字互相联通:
tomcat-hsjnet-001、tomcat-hsjnet-002在自定义的局域网(hsjnet)193.168.0.0/16内部
docker01-net-tomcat在docker默认的(docker01)172.16.0.0/16内部
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
17fb185afc2e tomcat "catalina.sh run" 15 seconds ago Up 11 seconds 0.0.0.0:49158->8080/tcp, :::49158->8080/tcp docker01-net-tomcat
50ea9326b41a tomcat "catalina.sh run" 17 minutes ago Up 17 minutes 0.0.0.0:49157->8080/tcp, :::49157->8080/tcp tomcat-hsjnet-002
91837c70b142 tomcat "catalina.sh run" 17 minutes ago Up 17 minutes 0.0.0.0:49156->8080/tcp, :::49156->8080/tcp tomcat-hsjnet-001
没有打通两个局域网之前:
[root@192 ~]# docker exec -it tomcat-hsjnet-001 ping docker01-net-tomcat
ping: docker01-net-tomcat: Temporary failure in name resolution
打通两个局域网:
[root@192 ~]# docker network connect hsjnet docker01-net-tomcat
打通两个局域网之后:
[root@192 ~]# docker exec -it tomcat-hsjnet-001 ping docker01-net-tomcat
PING docker01-net-tomcat (192.168.0.4) 56(84) bytes of data.
64 bytes from docker01-net-tomcat.hsjnet (192.168.0.4): icmp_seq=1 ttl=64 time=0.395 ms
64 bytes from docker01-net-tomcat.hsjnet (192.168.0.4): icmp_seq=2 ttl=64 time=0.144 ms
看一下此时的hsjnet:
[root@192 ~]# docker network inspect hsjnet
[
{
"Name": "hsjnet",
"Id": "e4ed63b42a2632f11967c439412d86928078bdf2cdc5ccd28c14638139dd74b2",
"Created": "2021-08-24T14:54:55.584126839+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": {
"17fb185afc2e324b2e6d7cbca85169de4521b4329f0936aa4e94b5b25710ef5d": {
"Name": "docker01-net-tomcat",
"EndpointID": "d3f665619371b73520090ef39470feb248bc8fb1c90f38b8d6f0e880ae0ec3f5",
"MacAddress": "02:42:c0:a8:00:04",
"IPv4Address": "192.168.0.4/16",
"IPv6Address": ""
},
"50ea9326b41af5b7d06e3bcd74e4358c605c5f133b689846b0054c0832cc9d00": {
"Name": "tomcat-hsjnet-002",
"EndpointID": "36bec44cec52fadd60ac1577bf70908aade4bccc3f6cee863b1f6ce6de705b37",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
},
"91837c70b1426b93bc4969c3aa80c63c9f9c694225d0e77d18121a5fd3c9fb80": {
"Name": "tomcat-hsjnet-001",
"EndpointID": "a8d4279badf98b22782cf2ca61b0ef3a5d08f53e6853f78e27779dbcb7afc8d5",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]