0%

Docker网络Network

这个实验一定要在Linux环境下做,docker for mac, docker for win是不行的。

docker网络类型

docker会给我们创建三种网络类型:bridge, host, none

1
2
3
4
5
docker network ls
NETWORK ID NAME DRIVER SCOPE
a1681b4a3bc9 bridge bridge local
d724eb42948a host host local
c9381cce7bbb none null local

在没有指定相关网络的情况下,默认情况,会使用bridge网络模式。

网络验证

启动两个busybox容器。

1
2
3
4
5
docker run -dit --name busybox1 busybox
4f3c61775b5e8bcd38a0c97ff97bcd16ed717ab31bea417b198192f83b493846

docker run -dit --name busybox2 busybox
c523392d8949b53abfbe736c43d2d47ea60a3420e8931bebc69d05baff93889b

查看一下bridge网络

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
docker network inspect bridge
[
{
"Name": "bridge",
"Id": "a1681b4a3bc9bf973e2ff712677e373b663cc65b3a9dd6e868f5635fff295a6a",
"Created": "2020-02-05T16:24:09.290786154+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"4f3c61775b5e8bcd38a0c97ff97bcd16ed717ab31bea417b198192f83b493846": {
"Name": "busybox1",
"EndpointID": "bb138601e4d4018e8f36b4679cc22823fca3020d53fb7c372e47d1c73345b374",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
},
"c523392d8949b53abfbe736c43d2d47ea60a3420e8931bebc69d05baff93889b": {
"Name": "busybox2",
"EndpointID": "8f3d2661b8bd9fdf4f9bf2d763e36ab87a7e1fa8dca6e1e97b727c7a8ab2d0fe",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]

可以看到busybox1和busybox2两个容器都连接到了bridge网络。两个容器的ip地址也能看到。

进入busybox1容器

1
docker exec -it busybox1 sh

通过ip地址和容器名ping容器

1
2
3
4
5
6
7
/ # ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3): 56 data bytes
64 bytes from 172.17.0.3: seq=0 ttl=64 time=0.285 ms
64 bytes from 172.17.0.3: seq=1 ttl=64 time=0.130 ms

/ # ping busybox2
^C

可以得出,通过ip地址是可以ping通的,但通过容器名是不行的。

查看hosts信息,也找不到任何busybox2的信息

1
2
3
4
5
6
7
8
cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 4f3c61775b5e

新建bridge网络

1
2
docker network create --driver bridge busybox_bridge
bebfcba064b2afdfb265d4df19eaccd950a1d957dcb3e2256d05dc544b64369e

查看网络

1
2
3
4
5
6
docker network ls
NETWORK ID NAME DRIVER SCOPE
a1681b4a3bc9 bridge bridge local
bebfcba064b2 busybox_bridge bridge local
d724eb42948a host host local
c9381cce7bbb none null local

分别创建busybox3和busybox4容器,并加入到busybox_bridge网络中

1
2
3
4
5
docker run -dit --network busybox_bridge --name busybox3 busybox
8e39a57b7543288fb716f66eaa4a54609a571c7d30194ca456d4a1dc443f19e6

docker run -dit --network busybox_bridge --name busybox4 busybox
ed1715cfaf168f64351d46aaa7f393cae00962c7ef850d8824abe06a4906843c

进入busybox3

本以为可以PING得通busybox4,但貌似不行,此实验失败。后面再研究。

host模式

以host模式启动一个nginx容器。

1
2
docker run --rm -d --net host nginx
956b96f5d0c25835a1d4470c12ed5e4abda341e44f9344e72cf42864976d96fa

因为是host模式,容器和主机共享一样的网络。

1
2
3
4
5
6
7
8
9
curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...
<p><em>Thank you for using nginx.</em></p>
</body>
</html>