# 安装这个包, 可以查看本机的 bridge 信息。(桥连接)
[miller@docker4 python-flask]$ sudo yum install bridge-utils
[miller@docker4 python-flask]$ brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.02422dec8cee no vethddd5c78
# --link=test1 意思就是我在test2 这个容器link了test1
# 这样在test2 容器中去 ping test1的时候, 就不需要还输入 ip。 直接通过test1 这个名字就可以了
[miller@docker4 python-flask]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3c1f23e3530e caijiwandocker/flask-hello-world "python3 app.py" About an hour ago Up About an hour 5000/tcp test1 [miller@docker4 python-flask]$ docker run -d --name=test2 --link=test1 caijiwandocker/flask-hello-world dc6dd83f333e2944474095f4fc76ed4465f5c938402770e45cb743fe9d60befd [miller@docker4 python-flask]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dc6dd83f333e caijiwandocker/flask-hello-world "python3 app.py" 10 seconds ago Up 8 seconds 5000/tcp test2 3c1f23e3530e caijiwandocker/flask-hello-world "python3 app.py" About an hour ago Up About an hour 5000/tcp test1
# 是可以通过名字访问的. 如此 如果以后要在test2上 链接 test1上的 数据库的时候, 就可以 test1:3306 就连接上了。 不关心test1的ip是多少
root@dc6dd83f333e:/app# ping test1 PING test1 (172.17.0.2) 56(84) bytes of data. 64 bytes from test1 (172.17.0.2): icmp_seq=1 ttl=64 time=0.069 ms 64 bytes from test1 (172.17.0.2): icmp_seq=2 ttl=64 time=0.085 ms 。。。
root@dc6dd83f333e:/app# ping 172.17.0.2 PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data. 64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.074 ms 64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.091 ms 。。。
# docker network 可以自己创建一个 bridge:
[miller@docker4 python-flask]$ docker network create my-bridge f8004079b0d715a93b2cc49c4d8f4213af8fe2e201a0d2fa547e0914cda3d784 [miller@docker4 python-flask]$ brctl show bridge name bridge id STP enabled interfaces br-f8004079b0d7 8000.02426a43a22c no # 我自己刚刚创建的没有接口链接 docker0 8000.02422dec8cee no vethddd5c78 # docker0 连接着 test1 test2 两个容器 vethec36d73 [miller@docker4 python-flask]$ docker network ls NETWORK ID NAME DRIVER SCOPE d6e9516055df bridge bridge local 2d4c88aac3be host host local f8004079b0d7 my-bridge bridge local 8f43716b9791 none null local
还可以将运行中的test1 和 test2 连接到自己创建的 bridge上:
bridge name bridge id STP enabled interfaces br-f8004079b0d7 8000.02426a43a22c no veth4069f1b vetha20bc78
docker0 8000.02422dec8cee no vethddd5c78 vethec36d73
都连接上了, 而容器链接自己创建的 bridge 的时候, 是默认的 容器和容器之间都进行了 --link 这个操作的。
也就是说, 可以在两个容器的内部,, 只通过名字进行 ping 也可以的。
[miller@docker4 python-flask]$ docker exec -it test1 /bin/bash root@3c1f23e3530e:/app# ping test2 PING test2 (172.18.0.3) 56(84) bytes of data. 64 bytes from test2.my-bridge (172.18.0.3): icmp_seq=1 ttl=64 time=0.100 ms 64 bytes from test2.my-bridge (172.18.0.3): icmp_seq=2 ttl=64 time=0.089 ms .....
[miller@docker4 python-flask]$ docker exec -it test2 /bin/bash root@dc6dd83f333e:/app# ping test1 PING test1 (172.17.0.2) 56(84) bytes of data. 64 bytes from test1 (172.17.0.2): icmp_seq=1 ttl=64 time=0.069 ms 64 bytes from test1 (172.17.0.2): icmp_seq=2 ttl=64 time=0.092 ms ......
# 使用 docker network inspace 看看 原始的 bridge 和 自己的 bridge:
[miller@docker4 python-flask]$ docker network inspect bridge [ ......"Containers": { "3c1f23e3530eadb54eb2fc61e7e33b36627428ba25c3b037c70c8d9a5fd9954a": { "Name": "test1", "EndpointID": "555d09be7c38f0107bfbd74ce1161836dc5a6bc043fbe0e61d5bd31f3b70e90a", "MacAddress": "02:42:ac:11:00:02", "IPv4Address": "172.17.0.2/16", "IPv6Address": "" }, "dc6dd83f333e2944474095f4fc76ed4465f5c938402770e45cb743fe9d60befd": { "Name": "test2", "EndpointID": "ed6ed94771d58b779efa46bf3aaf1d843b1b297a64086e525053db3ee811f2fd", "MacAddress": "02:42:ac:11:00:03", "IPv4Address": "172.17.0.3/16", "IPv6Address": "" } }, ..... ]
[miller@docker4 python-flask]$ docker network inspect my-bridge [ ...."Containers": { "3c1f23e3530eadb54eb2fc61e7e33b36627428ba25c3b037c70c8d9a5fd9954a": { "Name": "test1", "EndpointID": "5e443196f92b1d88cbd057e80adb01ee9dca1b9f3d56307701e6df924ee1ecf6", "MacAddress": "02:42:ac:12:00:02", "IPv4Address": "172.18.0.2/16", "IPv6Address": "" }, "dc6dd83f333e2944474095f4fc76ed4465f5c938402770e45cb743fe9d60befd": { "Name": "test2", "EndpointID": "3f6e3d84608062869bf79bc999382db52187ad28c780724f3efecf62cd0b9eb6", "MacAddress": "02:42:ac:12:00:03", "IPv4Address": "172.18.0.3/16", "IPv6Address": "" } }, .... ]
可以看出 test1 和 test2 两个容器, 依然分别连接着两个不同的 bridge.
########################################################################################################