我們前面建立了overlay網路 ov_net1 ,今天我們執行一個busybox容器並連線到 ov_net1
root@host01:~# docker run -itd --name bbox1 --network ov_net1 busybox # 建立容器bbox1,使用ov_net1網路
e666ab4af792055c63afde141a13286a8e108df2472994a42f22d92e4dbd4f61
root@host01:~# docker exec bbox1 ip r # 檢視容器內路由表
default via 172.18.0.1 dev eth1
10.0.0.0/24 dev eth0 scope link src 10.0.0.2
172.18.0.0/16 dev eth1 scope link src 172.18.0.2
root@host01:~# docker exec bbox1 ip a # 檢視容器內網路資訊,比我們想象的多了一塊網路卡,且預設路由走這塊網路卡
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1450 qdisc noqueue
link/ether 02:42:0a:00:00:02 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.2/24 brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
11: eth1@if12: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.2/16 brd 172.18.255.255 scope global eth1
valid_lft forever preferred_lft forever
root@host01:~# docker network ls # 上面使用的網路卡是overlay網路自動建立的一種docker網路 docker_gwbridge,專門給使用overlay網路的機器上網用
NETWORK ID NAME DRIVER SCOPE
5f1cb3e7ea16 bridge bridge local
467a0c3b1d73 docker_gwbridge bridge local
a08d5e1df638 host host local
83f08e15caa8 none null local
609020e03ff4 ov_net1 overlay global
root@host01:~# docker network inspect docker_gwbridge
[
{
"Name": "docker_gwbridge",
"Id": "467a0c3b1d73a3b025ff0fd32d87158855573ee7c056060b7967d02096fc626d",
"Created": "2019-03-18T08:55:22.63696796+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"e666ab4af792055c63afde141a13286a8e108df2472994a42f22d92e4dbd4f61": {
"Name": "gateway_a8d468c12df8",
"EndpointID": "d083b420b79f99a4456eccacc8b472e77a29c821cffa726afff63b484323750e",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.enable_icc": "false",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.name": "docker_gwbridge"
},
"Labels": {}
}
]
root@host01:~# ifconfig docker_gwbridge
docker_gwbridge Link encap:Ethernet HWaddr 02:42:4d:d7:0f:5f
inet addr:172.18.0.1 Bcast:172.18.255.255 Mask:255.255.0.0
inet6 addr: fe80::42:4dff:fed7:f5f/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:648 (648.0 B)
root@host01:~# docker exec bbox1 ping -c 2 www.qq.com
PING www.qq.com (123.151.137.18): 56 data bytes
64 bytes from 123.151.137.18: seq=0 ttl=51 time=5.621 ms
64 bytes from 123.151.137.18: seq=1 ttl=51 time=5.695 ms
--- www.qq.com ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 5.621/5.658/5.695 ms
上面是如何讓容器訪問外網,下面學習一下如何讓外面訪問容器
和之前方法一樣,把網路型別換成 overlay 網路即可
docker run -d -p 80:80 --network ov_net1 --name web01 httpd