(四)在 overlay 中運行容器
上一節我們[創建了 overlay 網路]ov_net1,今天將運行一個 busybox 容器并連接到 ov_net1:

查看容器的網路配置:

bbox1 有兩個網路介面 eth0 和 eth1,eth0 IP 為 10.0.0.2,連接的是 overlay 網路 ov_net1,eth1 IP 172.17.0.2,容器的默認路由是走 eth1,eth1 是哪兒來的呢?其實,docker 會創建一個 bridge 網路 “docker_gwbridge”,為所有連接到 overlay 網路的容器提供訪問外網的能力,

從 docker network inspect docker_gwbridge 輸出可確認 docker_gwbridge 的 IP 地址范圍是 172.17.0.0/16,當前連接的容器就是 bbox1(172.17.0.2),
docker network inspect docker_gwbridge
[
......
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Containers": {
"100dd1207eb8412a81c980dcd191d00eb4377a7a175a806a26038e8c12259766": {
"Name": "gateway_100dd1207eb8",
"EndpointID": "5077a2bfef80c695661f555412c3679b1a309cbba8a2f1a3247d6b414d35b819",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
},
......
而且此網路的網關就是網橋 docker_gwbridge 的 IP 172.17.0.1,

這樣容器 bbox1 就可以通過 docker_gwbridge 訪問外網,

如果外網要訪問容器,可通過主機埠映射,比如:
docker run -p 80:80 -d --net ov_net1 --name web1 httpd
驗證完外網的連通性,下一節驗證 overlay 網路跨主機通信,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/230211.html
標籤:其他
