查看docker网络

    1. [root@docker1-61 ~]# docker network ls
    2. NETWORK ID NAME DRIVER SCOPE
    3. b86803510533 bridge bridge local
    4. 0afbb3fc88ea docker10-1 bridge local
    5. 0d933cf96b95 docker11-1 bridge local
    6. e7ee51a5af17 host host local
    7. ca0ca28f0c7d none null local

    创建一个新的网络模型

    1. [root@docker1-61 ~]# docker network create --subnet=192.168.10.0/24 docker10-1
    2. 704508b07a743e6d1eb87f0eca5b93936115f11f4ed49141a110d2328bb9f407
    3. [root@docker1-61 ~]# docker inspect docker3
    4. [
    5. {
    6. "Name": "docker3",
    7. "Id": "704508b07a743e6d1eb87f0eca5b93936115f11f4ed49141a110d2328bb9f407",
    8. "Created": "2020-10-12T13:05:16.313515969+08:00",
    9. "Scope": "local",
    10. "Driver": "bridge",
    11. "EnableIPv6": false,
    12. "IPAM": {
    13. "Driver": "default",
    14. "Options": {},
    15. "Config": [
    16. {
    17. "Subnet": "192.168.10.0/24"
    18. }

    使用刚刚创建的网路模型

    1. [root@docker1-61 ~]# docker run --network=docker10-1 -it --name centos_test centos:centos7.5.1804
    2. [root@docker1-61 ~]# docker inspect centos_test | grep IPAddress
    3. "SecondaryIPAddresses": null,
    4. "IPAddress": "",
    5. "IPAddress": "192.168.10.2",

    将一个容器连接到另一个虚拟交换机上,实现本容器与另一个虚拟交换机所有容器互联互通(自己手动创建的容器可以通过容器名字互联)
    docker network connect docker交换机 要打通网络的容器名
    (探究发现容器与另一个虚拟交换机互联后实际是给容器分了一个本交换机网段的ip)

    1. [root@docker1-61 ~]# docker network connect docker10-1 centos04
    2. [root@docker1-61 ~]# docker exec -it centos04 ping centos01
    3. PING centos01 (192.168.10.2) 56(84) bytes of data.
    4. 64 bytes from centos01.docker10-1 (192.168.10.2): icmp_seq=1 ttl=64 time=0.090 ms
    5. 64 bytes from centos01.docker10-1 (192.168.10.2): icmp_seq=2 ttl=64 time=0.075 ms
    6. 64 bytes from centos01.docker10-1 (192.168.10.2): icmp_seq=3 ttl=64 time=0.081 ms