1.創建用戶自定義的 alpine-net 網絡
[root@host1 ~]# docker network create --driver bridge alpine-net
9f6d634e6bd7327163a9d83023e435da6d61bc6cf04c9d96001d1b64eefe4a71
2.列出 Docker 主機上的網絡
[root@host1 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
9f6d634e6bd7 alpine-net bridge local
db2f3a6af212 bridge bridge local
4251d9be020b host host local
ba96ad98e029 none null local
? ? ? ? 查看該網絡的詳細信息
[root@host1 ~]# docker network inspect alpine-net
[{"Name": "alpine-net","Id": "9f6d634e6bd7327163a9d83023e435da6d61bc6cf04c9d96001d1b64eefe4a71","Created": "2025-09-11T23:44:03.139792399+08:00","Scope": "local","Driver": "bridge","EnableIPv4": true,"EnableIPv6": false,"IPAM": {"Driver": "default","Options": {},"Config": [{"Subnet": "172.18.0.0/16","Gateway": "172.18.0.1"}]},"Internal": false,"Attachable": false,"Ingress": false,"ConfigFrom": {"Network": ""},"ConfigOnly": false,"Containers": {},"Options": {},"Labels": {}}
]
3.分別創建 4 個 Alpine 容器(這里我創建的都是一個類型,根據自己的需求創建)
[root@host1 ~]# docker run -dit --name alpine1 --network alpine-net alpine ash
a4fa900ee55e683cf5b1de1d1c4b9d1f4595dba20daa53bbd7ed041c9a2b9ff5
[root@host1 ~]# docker run -dit --name alpine2 --network alpine-net alpine ash
065be7f4fadedbe63bbc2b2e73a408f40876a2fae9609830290ced37bd456bc8
[root@host1 ~]# docker run -dit --name alpine3 --network alpine-net alpine ash
cc049a88659c173e82713a1fa74c13f176e4c1095b64a973dfa8e73abf053557
[root@host1 ~]# docker run -dit --name alpine4 --network alpine-net alpine ash
b2f77131151cd9cb5136b5115c3530f1ba915caccc7f64223ba03ea6d7285877
? ? ? ? 將 alpine4 容器連接到默認橋接網絡
[root@host1 ~]# docker network connect bridge alpine4
? ? ? ? 查看所有正在運行的容器,結果表明容器正常運行
[root@host1 ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b2f77131151c alpine "ash" 53 seconds ago Up 53 seconds alpine4
cc049a88659c alpine "ash" 59 seconds ago Up 58 seconds alpine3
065be7f4fade alpine "ash" About a minute ago Up About a minute alpine2
a4fa900ee55e alpine "ash" About a minute ago Up About a minute alpine1
0dbf7ea2178e registry "/entrypoint.sh /etc…" 33 hours ago Up 36 minutes 0.0.0.0:5000->5000/tcp, [::]:5000->5000/tcp myregistry
4.查看連接網絡詳細信息
[root@host1 ~]# docker network inspect alpine-net
[{"Name": "alpine-net","Id": "9f6d634e6bd7327163a9d83023e435da6d61bc6cf04c9d96001d1b64eefe4a71","Created": "2025-09-11T23:44:03.139792399+08:00","Scope": "local","Driver": "bridge","EnableIPv4": true,"EnableIPv6": false,"IPAM": {"Driver": "default","Options": {},"Config": [{"Subnet": "172.18.0.0/16","Gateway": "172.18.0.1"}]},"Internal": false,"Attachable": false,"Ingress": false,"ConfigFrom": {"Network": ""},"ConfigOnly": false,"Containers": {"065be7f4fadedbe63bbc2b2e73a408f40876a2fae9609830290ced37bd456bc8": {"Name": "alpine2","EndpointID": "80b24a7d84cbd2c015d7f18903d466644983e07317a669c766b6fda494919c5e","MacAddress": "7e:06:dc:bc:69:9e","IPv4Address": "172.18.0.3/16","IPv6Address": ""},"a4fa900ee55e683cf5b1de1d1c4b9d1f4595dba20daa53bbd7ed041c9a2b9ff5": {"Name": "alpine1","EndpointID": "0d859ee29448bbb4f19f855ee54a23668817d82a754c08476e286b2d525f0ac0","MacAddress": "86:67:e5:e7:17:a8","IPv4Address": "172.18.0.2/16","IPv6Address": ""},"b2f77131151cd9cb5136b5115c3530f1ba915caccc7f64223ba03ea6d7285877": {"Name": "alpine4","EndpointID": "e85a0e3bd691fed7e81c5e65e5c8b24b8d1783166d2684c820db8c63355df056","MacAddress": "22:3f:d3:cc:ad:7d","IPv4Address": "172.18.0.5/16","IPv6Address": ""},"cc049a88659c173e82713a1fa74c13f176e4c1095b64a973dfa8e73abf053557": {"Name": "alpine3","EndpointID": "c91b18ee977e9c7e15470256ca779950d901f5c70b517d94a34ad405845b32d8","MacAddress": "ca:14:42:da:65:e1","IPv4Address": "172.18.0.4/16","IPv6Address": ""}},"Options": {},"Labels": {}}
]
5.自動服務功能發現
[root@host1 ~]# docker attach alpine1
/ # ping -c 2 alpine2
PING alpine2 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.054 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.062 ms--- alpine2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.054/0.058/0.062 ms
/ # ping -c 2 alpine4
PING alpine4 (172.18.0.5): 56 data bytes
64 bytes from 172.18.0.5: seq=0 ttl=64 time=0.054 ms
64 bytes from 172.18.0.5: seq=1 ttl=64 time=0.062 ms--- alpine4 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.054/0.058/0.062 ms
/ # ping -c 2 alpine3
PING alpine3 (172.18.0.4): 56 data bytes
64 bytes from 172.18.0.4: seq=0 ttl=64 time=0.060 ms
64 bytes from 172.18.0.4: seq=1 ttl=64 time=0.061 ms--- alpine3 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.060/0.060/0.061 ms
/ # ping -c 2 172.17.0.3
PING 172.17.0.3 (172.17.0.3): 56 data bytes--- 172.17.0.3 ping statistics ---
2 packets transmitted, 0 packets received, 100% packet loss
6.連通問題(我設置的都是在同一網絡中,所以都能通)
? ? ? ? 脫離 alpine1 容器而不要停止他(Ctrl + P + Q)
/ # read escape sequence
7. alpine4 容器測試
[root@host1 ~]# docker attach alpine4
/ # ping -c 2 alpine1
PING alpine1 (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.037 ms
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.060 ms--- alpine1 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.037/0.048/0.060 ms
/ # ping -c 2 alpine2
PING alpine2 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.074 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.065 ms--- alpine2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.065/0.069/0.074 ms
/ # ping -c 2 alpine3
PING alpine3 (172.18.0.4): 56 data bytes
64 bytes from 172.18.0.4: seq=0 ttl=64 time=0.099 ms
64 bytes from 172.18.0.4: seq=1 ttl=64 time=0.061 ms--- alpine3 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.061/0.080/0.099 ms
8.訪問外網
/ # ping -c 2 www.baidu.com
PING www.baidu.com (103.235.46.102): 56 data bytes
64 bytes from 103.235.46.102: seq=0 ttl=127 time=242.857 ms
64 bytes from 103.235.46.102: seq=1 ttl=127 time=237.804 ms--- www.baidu.com ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 237.804/240.330/242.857 ms
/ # read escape sequence
? ? ? ? 退出
測試其他容器
[root@host1 ~]# docker attach alpine3
/ # ping -c 2 www.baidu.com
PING www.baidu.com (103.235.46.115): 56 data bytes
64 bytes from 103.235.46.115: seq=0 ttl=127 time=237.227 ms
64 bytes from 103.235.46.115: seq=1 ttl=127 time=246.838 ms--- www.baidu.com ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 237.227/242.032/246.838 ms
/ # read escape sequence
[root@host1 ~]# docker attach alpine1
/ # ping -c 2 www.baidu.com
PING www.baidu.com (103.235.46.102): 56 data bytes
64 bytes from 103.235.46.102: seq=0 ttl=127 time=237.112 ms
64 bytes from 103.235.46.102: seq=1 ttl=127 time=240.606 ms--- www.baidu.com ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 237.112/238.859/240.606 ms
/ # read escape sequence
9.停止并刪除以上實驗用到的所有容器和 alpine-net 網絡,恢復實驗環境
[root@host1 ~]# docker container stop alpine1 alpine2 alpine3 alpine4
alpine1
alpine2
alpine3
alpine4
[root@host1 ~]# docker container rm alpine1 alpine2 alpine3 alpine4
alpine1
alpine2
alpine3
alpine4
[root@host1 ~]# docker network rm alpine-net
alpine-net