Docker 网络 none 模式
发布时间:2023-06-06 14:17:10 所属栏目:教程 来源:
导读:容器有自己的网络命名空间,但不做任何配置,它与宿主机、与其他容器都不连通的。我们新建一个 none 模式的 busyBox 镜像 b0:
docker run -d -t --network none --name b0 busyBox
使用 docker exec b0 ip a 查
docker run -d -t --network none --name b0 busyBox
使用 docker exec b0 ip a 查
容器有自己的网络命名空间,但不做任何配置,它与宿主机、与其他容器都不连通的。我们新建一个 none 模式的 busyBox 镜像 b0: docker run -d -t --network none --name b0 busyBox 使用 docker exec b0 ip a 查看它的网络状态, 验证它仅有 lo 接口,不能与容器外通信: 两个容器之间可以直连通信,但不通过主机网桥进行桥接。解决的办法是创建一对 peer 接口,分别放到两个容器中,配置成点到点链路类型即可。 以下操作需要在 Linux 下进行: 首先启动 2 个容器: docker run -it -d --net=none --name=none1 busyBox docker run -it -d --net=none --name=none2 busyBox 找到这两个容器的进程号: docker inspect -f '{{.State.Pid}}' none1 #6545docker inspect -f '{{.State.Pid}}' none2 #6606 然后创建网络命名空间的跟踪文件: sudo mkdir -p /var/run/netnssudo ln -s /proc/6545/ns/net /var/run/netns/6545sudo ln -s /proc/6606/ns/net /var/run/netns/6606 创建一对 peer 接口,然后配置路由: sudo ip link add A type veth peer name B sudo ip link set A netns 6545 sudo ip netns exec 6545 ip addr add 10.1.1.1/32 dev A sudo ip netns exec 6545 ip link set A up sudo ip netns exec 6545 ip route add 10.1.1.2/32 dev A sudo ip link set B netns 6606 sudo ip netns exec 6606 ip addr add 10.1.1.2/32 dev B sudo ip netns exec 6606 ip link set B up sudo ip netns exec 6606 ip route add 10.1.1.1/32 dev B 现在这 2 个容器就可以相互 ping 通,并成功建立连接。点到点链路不需要子网和子网掩码: 测试完毕删除无用的容器: docker rm -f none1 none2 (编辑:汽车网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |