none和host网络的适用场景
Docker 安装时会自动在 host 上创建三个网络,用 docker network ls
命令查看:
1 root@ubuntu:~# docker network ls 2 NETWORK ID NAME DRIVER SCOPE 3 74b905c25854 bridge bridge local 4 464e732836b8 host host local 5 1e467ad897d5 none null local 6 root@ubuntu:~#
none 网络
none 网络就是什么都没有的网络。挂在这个网络下的容器除了 lo,没有其他任何网卡。容器创建时,可以通过 --network=none
指定使用 none 网络。
1 root@ubuntu:~# docker run -it --network=none busybox 2 / # 3 / # ifconfig 4 lo Link encap:Local Loopback 5 inet addr:127.0.0.1 Mask:255.0.0.0 6 UP LOOPBACK RUNNING MTU:65536 Metric:1 7 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 8 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 9 collisions:0 txqueuelen:1 10 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) 11 12 / #
none网络的应用场景
隔离,一些对安全性要求高并且不需要联网的应用可以使用 none 网络。
比如某个容器的唯一用途是生成随机密码,就可以放到 none 网络中避免密码被窃取。
host 网络
连接到 host 网络的容器共享 Docker host 的网络栈,容器的网络配置与 host 完全一样。可以通过 --network=host
指定使用 host 网络。
1 root@ubuntu:~# docker run -it --network=host busybox 2 / # 3 / # ip l 4 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1 5 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 6 2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq qlen 1000 7 link/ether 00:50:56:87:03:ab brd ff:ff:ff:ff:ff:ff 8 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue 9 link/ether 02:42:84:1f:d9:25 brd ff:ff:ff:ff:ff:ff 10 35: vetha0836f4@if34: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue master docker0 11 link/ether a2:44:e5:67:d4:2b brd ff:ff:ff:ff:ff:ff 12 73: vethe93fca0@if72: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue master docker0 13 link/ether b2:7d:0b:27:07:5d brd ff:ff:ff:ff:ff:ff 14 / # 15 / # 16 / # ifconfig 17 docker0 Link encap:Ethernet HWaddr 02:42:84:1F:D9:25 18 inet addr:172.17.0.1 Bcast:172.17.255.255 Mask:255.255.0.0 19 inet6 addr: fe80::42:84ff:fe1f:d925/64 Scope:Link 20 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 21 RX packets:51367 errors:0 dropped:0 overruns:0 frame:0 22 TX packets:52135 errors:0 dropped:0 overruns:0 carrier:0 23 collisions:0 txqueuelen:0 24 RX bytes:53261486 (50.7 MiB) TX bytes:124403203 (118.6 MiB) 25 26 ens192 Link encap:Ethernet HWaddr 00:50:56:87:03:AB 27 inet addr:10.12.31.1 Bcast:10.12.31.255 Mask:255.255.252.0 28 inet6 addr: fe80::250:56ff:fe87:3ab/64 Scope:Link 29 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 30 RX packets:8898800 errors:0 dropped:1612 overruns:0 frame:0 31 TX packets:305141 errors:0 dropped:0 overruns:0 carrier:0 32 collisions:0 txqueuelen:1000 33 RX bytes:1187474166 (1.1 GiB) TX bytes:93847252 (89.4 MiB) 34 35 lo Link encap:Local Loopback 36 inet addr:127.0.0.1 Mask:255.0.0.0 37 inet6 addr: ::1/128 Scope:Host 38 UP LOOPBACK RUNNING MTU:65536 Metric:1 39 RX packets:10179 errors:0 dropped:0 overruns:0 frame:0 40 TX packets:10179 errors:0 dropped:0 overruns:0 carrier:0 41 collisions:0 txqueuelen:1 42 RX bytes:99596888 (94.9 MiB) TX bytes:99596888 (94.9 MiB) 43 44 vetha0836f4 Link encap:Ethernet HWaddr A2:44:E5:67:D4:2B 45 inet6 addr: fe80::a044:e5ff:fe67:d42b/64 Scope:Link 46 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 47 RX packets:5398 errors:0 dropped:0 overruns:0 frame:0 48 TX packets:5487 errors:0 dropped:0 overruns:0 carrier:0 49 collisions:0 txqueuelen:0 50 RX bytes:49767395 (47.4 MiB) TX bytes:49779783 (47.4 MiB) 51 52 vethe93fca0 Link encap:Ethernet HWaddr B2:7D:0B:27:07:5D 53 inet6 addr: fe80::b07d:bff:fe27:75d/64 Scope:Link 54 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 55 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 56 TX packets:8 errors:0 dropped:0 overruns:0 carrier:0 57 collisions:0 txqueuelen:0 58 RX bytes:0 (0.0 B) TX bytes:648 (648.0 B) 59 60 / # 61 / # hostname 62 ubuntu 63 / #
在容器中可以看到 host 的所有网卡,并且连 hostname 也是 host 的。
好处:
性能,如果容器对网络传输效率有较高要求,则可以选择 host 网络。
缺点:
要考虑端口冲突问题,Docker host 上已经使用的端口就不能再用了。
Docker host 的另一个用途是让容器可以直接配置 host 网路。比如某些跨 host 的网络解决方案,其本身也是以容器方式运行的,这些方案需要对网络进行配置,比如管理 iptables。
--------------------------------引用来自----------------------------------
https://mp.weixin.qq.com/s?__biz=MzIwMTM5MjUwMg==&mid=2653587677&idx=1&sn=f4b65107a7f19a6530014bf49a04c7d1&chksm=8d3080c4ba4709d21902b08986585bf42d79c0b8bfb771276292a2d46ab02dd955ca9bc29f44&scene=21#wechat_redirect