查看docker网络
[root@docker1-61 ~]# docker network lsNETWORK ID NAME DRIVER SCOPEb86803510533 bridge bridge local0afbb3fc88ea docker10-1 bridge local0d933cf96b95 docker11-1 bridge locale7ee51a5af17 host host localca0ca28f0c7d none null local
创建一个新的网络模型
[root@docker1-61 ~]# docker network create --subnet=192.168.10.0/24 docker10-1704508b07a743e6d1eb87f0eca5b93936115f11f4ed49141a110d2328bb9f407[root@docker1-61 ~]# docker inspect docker3[{"Name": "docker3","Id": "704508b07a743e6d1eb87f0eca5b93936115f11f4ed49141a110d2328bb9f407","Created": "2020-10-12T13:05:16.313515969+08:00","Scope": "local","Driver": "bridge","EnableIPv6": false,"IPAM": {"Driver": "default","Options": {},"Config": [{"Subnet": "192.168.10.0/24"}
使用刚刚创建的网路模型
[root@docker1-61 ~]# docker run --network=docker10-1 -it --name centos_test centos:centos7.5.1804[root@docker1-61 ~]# docker inspect centos_test | grep IPAddress"SecondaryIPAddresses": null,"IPAddress": "","IPAddress": "192.168.10.2",
将一个容器连接到另一个虚拟交换机上,实现本容器与另一个虚拟交换机所有容器互联互通(自己手动创建的容器可以通过容器名字互联)
docker network connect docker交换机 要打通网络的容器名
(探究发现容器与另一个虚拟交换机互联后实际是给容器分了一个本交换机网段的ip)
[root@docker1-61 ~]# docker network connect docker10-1 centos04[root@docker1-61 ~]# docker exec -it centos04 ping centos01PING centos01 (192.168.10.2) 56(84) bytes of data.64 bytes from centos01.docker10-1 (192.168.10.2): icmp_seq=1 ttl=64 time=0.090 ms64 bytes from centos01.docker10-1 (192.168.10.2): icmp_seq=2 ttl=64 time=0.075 ms64 bytes from centos01.docker10-1 (192.168.10.2): icmp_seq=3 ttl=64 time=0.081 ms
