Docker容器网络
在分享06中学完了bridge网络
,接着学习none网络
和host网络
。
Docker在安装时会在host上默认创建三个网络,分别是bridge
、host
、null
。
[root@promote ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
527281654b19 bridge bridge local
9ab80c94885b host host local
c42335728d98 none null local
none网络
none网络就是什么网络都没有,就和一台没有联网的PC一样,可以在创建容器时通过--network=none
指定。
[root@promote ~]# docker run -it --name busybox --network none docker.io/busybox
/ # ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
创建完成后查看网络,发现只有一个回环设备,没有网卡设备(这里用的镜像是busybox
,因为busybox包含了一些像ifconfig
简单的命令,一开始我用的是centos
镜像,后来发现centos
镜像没有ifconfig
命令,需要安装,而我创建的是无网络环境,所以就换成了busybox
)。
host网络
host网络就是连接host网络的容器与主机共享网络,且网络配置一致。
[root@promote ~]# docker run -it --name busybox --network host docker.io/busybox
/ # ifconfig
docker0 Link encap:Ethernet HWaddr 02:42:80:4B:97:72
inet addr:172.17.0.1 Bcast:0.0.0.0 Mask:255.255.0.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
ens33 Link encap:Ethernet HWaddr 00:0C:29:EB:2D:01
inet addr:192.168.41.133 Bcast:192.168.41.255 Mask:255.255.255.0
inet6 addr: fe80::4881:9be0:2bb6:62e/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:586 errors:0 dropped:0 overruns:0 frame:0
TX packets:410 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:51651 (50.4 KiB) TX bytes:46089 (45.0 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:68 errors:0 dropped:0 overruns:0 frame:0
TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6044 (5.9 KiB) TX bytes:6044 (5.9 KiB)
再来看一下主机的网络。
[root@promote ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
ether 02:42:80:4b:97:72 txqueuelen 0 (Ethernet)
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
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.41.133 netmask 255.255.255.0 broadcast 192.168.41.255
inet6 fe80::4881:9be0:2bb6:62e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:eb:2d:01 txqueuelen 1000 (Ethernet)
RX packets 619 bytes 54111 (52.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 431 bytes 48129 (47.0 KiB)
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 68 bytes 6044 (5.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 68 bytes 6044 (5.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
可以发现他们是一致的。