Docker 使用到的与 Linux 网络有关的技术分别有:网络名称空间、Veth、Iptables、网桥、路由python
ps : 若是不一样网络名称空间之间要进行通讯该怎么办呢?linux
ip netns list # 查看 ip netns add [namespace] # 添加 ip netns delete [namespace] # 删除
[root@shawn ~]#ip netns add test1 [root@shawn ~]#ip netns add test2 [root@shawn ~]#ip netns list test2 test1
[root@shawn ~]#ip link add veth1 type veth peer name veth2 [root@shawn ~]#ip link
[root@shawn ~]#ip link set veth1 netns test1 [root@shawn ~]#ip link set veth2 netns test2
[root@shawn ~]#ip netns exec test1 ip link [root@shawn ~]#ip netns exec test2 ip link
[root@shawn ~]#ip netns exec test1 ip addr add 172.17.0.111/20 dev veth1 [root@shawn ~]#ip netns exec test2 ip addr add 172.17.0.112/20 dev veth2 [root@shawn ~]#ip netns exec test1 ip link set dev veth1 up [root@shawn ~]#ip netns exec test2 ip link set dev veth2 up
[root@shawn ~]#ip netns exec test1 ip a [root@shawn ~]#ip netns exec test2 ip a
[root@shawn ~]#ip netns exec test1 ping 172.17.0.112 [root@shawn ~]#ip netns exec test2 ping 172.17.0.111
docker network [命令参数]
⚽查看当前系统有哪些网桥 "ls" [root@shawn ~]#docker network ls ''' NETWORK ID NAME DRIVER SCOPE befd59194a71 bridge bridge local 94f8e35f3357 host host local 79fb28a9a12e none null local ''' ⚽建立网桥 "create" [root@shawn ~]#docker network create shawn ffac93578a0ce40395936d226bd097fd049ad077022419a9b6b074b6fe2f892b [root@shawn ~]#docker network ls ''' NETWORK ID NAME DRIVER SCOPE befd59194a71 bridge bridge local 94f8e35f3357 host host local 79fb28a9a12e none null local ffac93578a0c shawn bridge local #新建的网桥 ''' ⚽查看网桥信息,格式 : "docker network inspect [网桥的名称|网桥ID]" [root@shawn ~]#docker network inspect shawn ''' [ { "Name": "shawn", "Id": "ffac93578a0ce40395936d226bd097fd049ad077022419a9b6b074b6fe2f892b", "Created": "2020-12-03T11:56:35.554136022+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.18.0.0/16", "Gateway": "172.18.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": {} } ] ''' ⚽删除网桥 "rm" [root@shawn ~]#docker network rm shawn shawn [root@shawn ~]#docker network ls ''' NETWORK ID NAME DRIVER SCOPE befd59194a71 bridge bridge local 94f8e35f3357 host host local 79fb28a9a12e none null local ''' # "Shawn"被删除了 ⚽清理网桥 "prune", 咱们先建立多个网桥,而后一次性清除 [root@shawn ~]#docker network create shawn1 f4d2f2b57b48cd35b3cc9eddad0377cae95d213032ed0d6a92e9de9571adeb4e #建立成功 [root@shawn ~]#docker network create shawn2 ea66ed0d06adbe7f5211f3ae38b6edeb47dffac0f53240e9204fd3dcaf4670d5 #建立成功 [root@shawn ~]#docker network create shawn3 222d6e298d6091cea0a6229e19c84825a41ea92b7c0b512db47c858dde7259f0 #建立成功 [root@shawn ~]#docker network prune WARNING! This will remove all custom networks not used by at least one container. Are you sure you want to continue? [y/N] y # 问你是否要这样作"yes" Deleted Networks: # 将要删除如下网桥 shawn1 shawn2 shawn3 [root@shawn ~]#docker network ls ''' NETWORK ID NAME DRIVER SCOPE befd59194a71 bridge bridge local 94f8e35f3357 host host local 79fb28a9a12e none null local ''' # 发现并无 "shawn"系列的网桥
network namespace | 主要提供了关于网络资源的隔离,包括网络设备、IPv4 和 IPv6 协议栈、IP 路由 表、防火墙、/proc/net 目录、/sys/class/net 目录、端口(socket)等 |
---|---|
linux Bridge | 功能至关于物理交换机,为连在其上的设备(容器)转发数据帧。如 docker0 网桥 |
iptables | 主要为容器提供 NAT 以及容器网络安全 |
veth pair | 两个虚拟网卡组成的数据通道。在 Docker 中,用于链接 Docker 容器和 Linux Bridge。一端在容器中做为 eth0 网卡,另外一端在 Linux Bridge 中做为网桥的一 个端口 |
安装Docker时,它会自动建立三个网络,bridge(建立容器默认链接到此网络)、 none 、hostnginx
docker network ls
: 查看当前系统有哪些网络(网桥)[root@shawn ~]#docker network ls NETWORK ID NAME DRIVER SCOPE befd59194a71 bridge bridge local 94f8e35f3357 host host local 79fb28a9a12e none null local
网络模式 | 设置方法 | 简介 |
---|---|---|
Host | --network host | 容器将不会虚拟出本身的网卡,配置本身的IP等,而是使用宿主机的IP和端口 |
Bridge | --network bridge(默认此模式) | 此模式会为每个容器分配、设置IP等,并将容器链接到一个docker0虚拟网桥,经过docker0网桥以及Iptables nat表配置与宿主机通讯 |
None | -- network none | 该模式关闭了容器的网络功能 |
Container | --network "container:[共享容器名称]" | 建立的容器不会建立本身的网卡,配置本身的IP,而是和一个指定的容器共享IP、端口范围 |
docker run --network host [镜像名称或ID]
[root@shawn ~]#docker run -d --network host nginx:latest
docker run --network "containe:[共享容器名称]" [镜像名称或ID]
⚽建立共享容器 "cont01" [root@shawn ~]#docker run -dit --name cont01 busybox:latest sh ⚽建立连接容器 "cont02", 并指定网络模式 "container" [root@shawn ~]#docker run -dit --name cont02 --network "container:cont01" busybox:latest sh ⚽查看两个容器的 "ip", 发现同样 [root@shawn ~]#docker exec cont01 ip a [root@shawn ~]#docker exec cont02 ip a
docker run --network none [镜像名称或ID]
[root@shawn ~]#docker run -dit --network none --name none_test busybox:latest sh