命令
docker network connect 网络名称 容器名称
参数
平时我们直接启动那个的命令是省略了默认参数–net bridge
(即:使用我们的edocker0做路由)
So 使用自定义网络需要自己定义参数:
- –driver
bridge
网络模式 - –subnet
192.168.0.0/16
IP地址/域 [192.168.0.2~192.168.255.255] - –gateway
192.168.0.1
默认网关
测试
# 新建自定义网络 mynet-02
root@db7d2aa74a7f:/# docker network create --driver bridge --subnet 193.168.0.0/16 --gateway 193.168.0.1 mynet-02
# 查看自定义网络 mynet-02 的信息
root@db7d2aa74a7f:/# docker network inspect mynet-02
[
{
"Name": "mynet-02",
"Id": "05eefd1d85935337e304e73f073e8b03f6d288eab77307c267aed806922a1051",
"Created": "2021-05-23T08:42:54.5906674Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "193.168.0.0/16",
"Gateway": "193.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
# 查看当前网络
root@db7d2aa74a7f:/# docker network ls
NETWORK ID NAME DRIVER SCOPE
1ca1ad3cf619 bridge bridge local
8e5cc694b1a8 host host local
f532c792d595 mynet bridge local
05eefd1d8593 mynet-02 bridge local
56951558f000 none null local
# 新建两个mynet-02网络下的tomcat容器
root@db7d2aa74a7f:/# docker run -d -P --name t-mynet02-01 tomcat
4090d6020e9eef9857a520d3aea110217e73b63aad49f2e4b57562e4c44c798f
root@db7d2aa74a7f:/# docker run -d -P --name t-mynet02-02 --net mynet-02 tomcat
9067df0f39fc1c3f81a8890d2b382440d8011521d1008e69dfd6eb2388037d29
# 查看正在运行中的容器
root@db7d2aa74a7f:/# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f9016021242 tomcat "catalina.sh run" 24 seconds ago Up 13 seconds 0.0.0.0:32781->8080/tcp t-mynet02-01
9067df0f39fc tomcat "catalina.sh run" 56 seconds ago Up 45 seconds 0.0.0.0:32780->8080/tcp t-mynet02-02
509b121c9c33 tomcat "catalina.sh run" 44 minutes ago Up 44 minutes 0.0.0.0:32775->8080/tcp tomcat-mynet-02
db49ff7883cc tomcat "catalina.sh run" 45 minutes ago Up 45 minutes 0.0.0.0:32778->8080/tcp tomcat-mynet-01
# ping
root@db7d2aa74a7f:/# docker exec -it t-mynet02-01 ping tomcat-mynet-01 # 不能
ping: tomcat-mynet-01: Temporary failure in name resolution
# 打通 tomcat-mynet-01 mynet-02
root@db7d2aa74a7f:/# docker network connect mynet-02 tomcat-mynet-01
root@db7d2aa74a7f:/# docker exec -it t-mynet02-01 ping tomcat-mynet-01 # tomcat-mynet-01 可以ping通 t-mynet02-01
PING tomcat-mynet-01 (193.168.0.4) 56(84) bytes of data.
64 bytes from tomcat-mynet-01.mynet-02 (193.168.0.4): icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from tomcat-mynet-01.mynet-02 (193.168.0.4): icmp_seq=2 ttl=64 time=0.056 ms
^C
--- tomcat-mynet-01 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 35ms
rtt min/avg/max/mdev = 0.056/0.067/0.079/0.014 ms
root@db7d2aa74a7f:/# docker exec -it tomcat-mynet-01 ping t-mynet02-02 # tomcat-mynet-01 可以ping通 t-mynet02-02
PING t-mynet02-02 (193.168.0.2) 56(84) bytes of data.
64 bytes from t-mynet02-02.mynet-02 (193.168.0.2): icmp_seq=1 ttl=64 time=0.055 ms
64 bytes from t-mynet02-02.mynet-02 (193.168.0.2): icmp_seq=2 ttl=64 time=0.081 ms
^C
--- t-mynet02-02 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 22ms
rtt min/avg/max/mdev = 0.055/0.068/0.081/0.013 ms
root@db7d2aa74a7f:/# docker exec -it t-mynet02-01 ping tomcat-mynet-02 # t-mynet02-01 不能ping通 tomcat-mynet-02
ping: tomcat-mynet-02: Temporary failure in name resolution
# 打通 t-mynet02-01 mynet
root@db7d2aa74a7f:/# docker network connect mynet t-mynet02-01
root@db7d2aa74a7f:/# docker exec -it t-mynet02-01 ping tomcat-mynet-02 # t-mynet02-01 可以ping通 tomcat-mynet-02
PING tomcat-mynet-02 (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat-mynet-02.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.076 ms
64 bytes from tomcat-mynet-02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.050 ms
^C
--- tomcat-mynet-02 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 36ms
rtt min/avg/max/mdev = 0.050/0.063/0.076/0.013 ms
BUT
t-mynet02-02 不能ping
通 tomcat-mynet-02
root@db7d2aa74a7f:/# docker exec -it t-mynet02-02 ping tomcat-mynet-02
ping: tomcat-mynet-02: Temporary failure in name resolution
原因
打通tomcat-mynet-01 mynet-02 后就是将tomcat-mynet-01放在了 mynet-02 网络下 因此只要是在mynet-02网络下的都可以ping
通 tomcat-mynet-01;
打通t-mynet02-01 mynet 后就是将t-mynet02-01放在了mynet网络下 因此只要是在mynet网络下的都可以 ping
通t-mynet02-01而此时t-mynet02-02和tomcat-mynet-02并无关系 因此仍旧无法ping
通。
于是出现了—>
一个容器同时在两个网络下—>
一个容器有两个ip
(比如阿里云服务的共有IP和私有IP)