赞
踩
刚接触Docker的时候,你是否好奇容器之间是怎么通信的呢?今天我们就一起来认识一下docker的网络吧~
Docker的网络模块是可插拔式的,默认有五种网络模式可以选择。通过docker network ls这个命令来查看本机中所有的网络模式。
zxl@linux:~/Downloads$ docker network ls
NETWORK ID NAME DRIVER SCOPE
7073c3dcfabb bridge bridge local
75e8b72bcb7c docker-compose-postgresql_default bridge local
f674a227bd35 host host local
f907cba0eede kind bridge local
79f129884d9a minikube bridge local
180116fefa44 none null local
zxl@linux:~/Downloads$
默认Docker容器运行会分配独立的Network Namespace隔离子系统,基于host模式,容器将不会获得一个独立的Network Namespace,而是和宿主机共用一个Network Namespace,容器将不会虚拟出自己的网卡,配置自己的IP等,而是使用宿主机的IP和端口。(用的是宿主机的IP,也就是和宿主机共用一个IP地址,host模式不需要加-p进行端口映射,因为和宿主机共享网络IP和端口)
连接到 host 网络的容器共享 Docker host 的网络栈,容器的网络配置与 host 完全一样。可以通过 --network=host 指定使用 host 网络。
[root@www ~]# docker rm -f `docker ps -aq` b67e41213bfc [root@www ~]# docker run -itd --net=host centos7-ssh-v1:latest a0b030bf26338f41c8fa8c47de3cdc7dd9880036640e048bb563e740f567728b [root@www ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a0b030bf2633 centos7-ssh-v1:latest "/bin/sh -c /usr/sbi?? 5 seconds ago Up 5 seconds laughing_williamson [root@www ~]# docker inspect a0b030bf2633 | grep -i networkmode "NetworkMode": "host", [root@www ~]# docker exec -it a0b030bf2633 /bin/bash [root@a0b030bf2633 ~]# ifconfig br-a68a7681f73a: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.20.0.1 netmask 255.255.0.0 broadcast 172.20.255.255 inet6 fe80::42:b6ff:fed1:6076 prefixlen 64 scopeid 0x20<link> ether 02:42:b6:d1:60:76 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 br-b09178df0a79: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.21.0.1 netmask 255.255.0.0 broadcast 172.21.255.255 inet6 fe80::42:45ff:feff:369d prefixlen 64 scopeid 0x20<link> ether 02:42:45:ff:36:9d 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 docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:44ff:fea0:1092 prefixlen 64 scopeid 0x20<link> ether 02:42:44:a0:10:92 txqueuelen 0 (Ethernet) RX packets 7021 bytes 306134 (298.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7957 bytes 60566091 (57.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.179.100 netmask 255.255.255.0 broadcast 192.168.179.255 inet6 fe80::afe7:df8d:107b:abd1 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:93:42:61 txqueuelen 1000 (Ethernet) RX packets 98616 bytes 117487805 (112.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 22452 bytes 2530627 (2.4 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.20.2.129 netmask 255.255.255.0 broadcast 172.20.2.255 inet6 fe80::e64a:a3d7:df04:1664 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:93:42:6b txqueuelen 1000 (Ethernet) RX packets 82168 bytes 46060412 (43.9 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 12879 bytes 1182653 (1.1 MiB) 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 1 (Local Loopback) RX packets 12 bytes 1004 (1004.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 12 bytes 1004 (1004.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 #容器22端口是启动不了的,因为是共享宿主机的22端口的,在容器内要改为6022才可以,然后重新启动ssh [root@a0b030bf2633 ~]# vi /etc/ssh/sshd_config [root@a0b030bf2633 ~]# cat /etc/ssh/sshd_config | grep -i port | grep -v "#" Port 6022 [root@a0b030bf2633 ~]# /usr/sbin/sshd #在容器里也可以看到宿主机的端口 [root@a0b030bf2633 ~]# netstat -tpln Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:6022 0.0.0.0:* LISTEN 39/sshd tcp6 0 0 :::22 :::* LISTEN - tcp6 0 0 ::1:25 :::* LISTEN - tcp6 0 0 :::6022 :::* LISTEN 39/sshd [root@a0b030bf2633 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 Connecting to 192.168.179.100:6022... Connection established. To escape to local shell, press 'Ctrl+Alt+]'.
在容器中可以看到 host 的所有网卡,并且连 hostname 也是 host 的。host 网络的使用场景又是什么呢?
Docker host 的另一个用途是让容器可以直接配置 host 网路。比如某些跨 host 的网络解决方案,其本身也是以容器方式运行的,这些方案需要对网络进行配置,比如管理 iptables等。
使用主机模式不需要加-p映射,因为是共用宿主机的端口了,企业一般用的比较少,容易乱套搞晕懵圈,因为你区分不了在容器还是宿主机里面(共享网络,共享主机名)。
Docker 镜像内集成了 V2Ray 内核,因此内核无需额外被安装。
–network=host指定使用host模式
# run v2raya
docker run -d \
--restart=always \
--privileged \
--network=host \
--name v2raya \
-e V2RAYA_ADDRESS=0.0.0.0:2017 \
-v /lib/modules:/lib/modules:ro \
-v /etc/resolv.conf:/etc/resolv.conf \
-v /etc/v2raya:/etc/v2raya \
mzz2017/v2raya
如果你使用 MacOSX 或其他不支持 host 模式的环境,在该情况下无法使用全局透明代理,或者你不希望使用全局透明代理,docker 命令会略有不同:
# run v2raya
docker run -d \
-p 2017:2017 \
-p 20170-20172:20170-20172 \
--restart=always \
--name v2raya \
-v /etc/v2raya:/etc/v2raya \
mzz2017/v2raya
https://blog.csdn.net/a772304419/article/details/126056421?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2aggregatepagefirst_rank_ecpm_v1~rank_v31_ecpm-1-126056421-null-null.pc_agg_new_rank&utm_term=host%E7%BD%91%E7%BB%9C&spm=1000.2123.3001.4430
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。