docker中4种网络模式
none,host,bridge(默认),contain。
借用一下马哥的一个图片
图中上方4个大格子就是4中模式
1.closed container
这个其实就是none,没有网络,只有一个LOOKBOOK接口
[root@localhost dockerfile]# docker run -it --rm --name='s1' --network=none ss:v1 [root@964b92481121 /]# ifconfig lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2.Bridged container
这个就是桥接模式,也是默认的,借用网上一个图就容易理解
可以看到有个veth的网卡一端在容器内,另一端在网桥docker0中
我们再来看看网卡
[root@localhost docker]# ifconfig docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 inet6 fe80::42:eff:fe6c:e013 prefixlen 64 scopeid 0x20<link> ether 02:42:0e:6c:e0:13 txqueuelen 0 (Ethernet) RX packets 3461 bytes 147879 (144.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3908 bytes 22231126 (21.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost ~]# docker run -it --rm --name='s1' ss:v1 [root@329376fef2e7 /]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0 inet6 fe80::42:acff:fe11:2 prefixlen 64 scopeid 0x20<link> ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 6 bytes 508 (508.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6 bytes 508 (508.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
可以看到我们原来的主机中多了一个网卡docker0,这个就是连接容器的网卡也是容器的网关,而且容器也有自己的网络,网络还是172.17.0.2网段,这个地址其实都是这样的,如有需要可到/etc/docker/daemon.json文件中添加相关参数进行修改
3.joined container
这个是容器连接,其实也很简单,容器之间共用网络名称空间中的IP地址端口号啥的,但是文件系统之类的就分开,我们在同一个镜像中在运行一个容器看看
[root@localhost ~]# docker run -it --rm --name='s2' --network=container:s1 ss:v1 [root@329376fef2e7 /]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0 inet6 fe80::42:acff:fe11:2 prefixlen 64 scopeid 0x20<link> ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 16 bytes 1296 (1.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8 bytes 648 (648.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
看吧,连地址啥的都跟容器s1一样
4.open container
这个跟前面的join cont差不多,只不过这次的对象是主机,也就是容器与我们的主机共用IP地址,就是host模式呗,这个就不再演示,把--network后面跟的改为host即可,原理也简单
当然容器间通信及通信的各种情况啥的还是有很多内容的,后面我也会写出来,也可以看看其他人的博客这里有一个我觉得不错的 https://www.cnblogs.com/whych/p/9595671.html