Docker容器互连-自定义网络

查看docker所有网络

[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
c6cccbeca026        bridge              bridge              local
ef2aaeb91b95        host                host                local
9b941aa3bfdf        none                null                local

DRIVER
bridge :桥接模式(docker默认)
host:和宿主机共享网络
none:不配置网络

使用命令docker network create来创建网络。创建的网络中默认使用的是桥接模式,–subnet输入子网段,–gateway输入子网的网关地址。创建成功后,可以使用docker network ls查看到网络。

[root@localhost ~]# docker network create --help

Usage:  docker network create [OPTIONS] NETWORK

Create a network

Options:
      --attachable             Enable manual container attachment
      --aux-address map        Auxiliary IPv4 or IPv6 addresses used by Network driver (default map[])
  -d, --driver string          Driver to manage the Network (default "bridge")
      --gateway stringSlice    IPv4 or IPv6 Gateway for the master subnet
      --help                   Print usage
      --internal               Restrict external access to the network
      --ip-range stringSlice   Allocate container ip from a sub-range
      --ipam-driver string     IP Address Management Driver (default "default")
      --ipam-opt map           Set IPAM driver specific options (default map[])
      --ipv6                   Enable IPv6 networking
      --label list             Set metadata on a network (default [])
  -o, --opt map                Set driver specific options (default map[])
      --subnet stringSlice     Subnet in CIDR format that represents a network segment
[root@localhost ~]# docker network create --subnet 192.168.3.0/24 --gateway 192.168.3.1 mynet
3013d0f266f4e243fd7af37c8c8e3d60b6a75c1744a5aa8b8c6cb05a5d25220c
[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
c6cccbeca026        bridge              bridge              local
ef2aaeb91b95        host                host                local
3013d0f266f4        mynet               bridge              local
9b941aa3bfdf        none                null                local

使用docker network inspect 网络查看详细信息。此处可以看到该网络的网段和网关。目前刚创建,并没有容器被使用。

[root@localhost ~]# docker network inspect mynet 
[
    {
    
    
        "Name": "mynet",
        "Id": "3013d0f266f4e243fd7af37c8c8e3d60b6a75c1744a5aa8b8c6cb05a5d25220c",
        "Created": "2021-05-19T05:20:32.230092567-04:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
    
    
            "Driver": "default",
            "Options": {
    
    },
            "Config": [
                {
    
    
                    "Subnet": "192.168.3.0/24",
                    "Gateway": "192.168.3.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Containers": {
    
    },
        "Options": {
    
    },
        "Labels": {
    
    }
    }
]

运行容器,使用自定义网络mynet。

[root@localhost ~]# docker run -dit --name centosnet1 --network mynet centos
2688d2a9c1487769e223d6f0d8c834197044ff2607e2896aa12d53a0dc0e8279
[root@localhost ~]# docker run -dit --name centosnet2 --network mynet centos
bdf99b0c14809435732551fa375ad77809f62fcdf5b01659ed741451cd41e223
[root@localhost ~]# docker network inspect mynet 
[
    {
    
    
        "Name": "mynet",
        "Id": "3013d0f266f4e243fd7af37c8c8e3d60b6a75c1744a5aa8b8c6cb05a5d25220c",
        "Created": "2021-05-19T05:20:32.230092567-04:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
    
    
            "Driver": "default",
            "Options": {
    
    },
            "Config": [
                {
    
    
                    "Subnet": "192.168.3.0/24",
                    "Gateway": "192.168.3.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Containers": {
    
    
            "2688d2a9c1487769e223d6f0d8c834197044ff2607e2896aa12d53a0dc0e8279": {
    
    
                "Name": "centosnet1",
                "EndpointID": "c5e7b87d9302143a29b7855078b675eb9f2cbdcb7a11552b045eb270b44c2d21",
                "MacAddress": "02:42:c0:a8:03:02",
                "IPv4Address": "192.168.3.2/24",
                "IPv6Address": ""
            },
            "bdf99b0c14809435732551fa375ad77809f62fcdf5b01659ed741451cd41e223": {
    
    
                "Name": "centosnet2",
                "EndpointID": "f5524b98098a055a8d876440cb885c90fb5c43690fbd31c5aab3f91082769ffe",
                "MacAddress": "02:42:c0:a8:03:03",
                "IPv4Address": "192.168.3.3/24",
                "IPv6Address": ""
            }
        },
        "Options": {
    
    },
        "Labels": {
    
    }
    }
]

测试容器的网络是否能用。

[root@localhost ~]# docker exec -it centosnet1 ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=127 time=13.10 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=127 time=8.35 ms
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 8.345/11.166/13.987/2.821 ms
[root@localhost ~]# docker exec -it centosnet1 ping 192.168.3.3
PING 192.168.3.3 (192.168.3.3) 56(84) bytes of data.
64 bytes from 192.168.3.3: icmp_seq=1 ttl=64 time=0.059 ms
64 bytes from 192.168.3.3: icmp_seq=2 ttl=64 time=0.134 ms
^C
--- 192.168.3.3 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1ms
rtt min/avg/max/mdev = 0.059/0.096/0.134/0.038 ms
[root@localhost ~]# docker exec -it centosnet1 ping centosnet2
PING centosnet2 (192.168.3.3) 56(84) bytes of data.
64 bytes from centosnet2.mynet (192.168.3.3): icmp_seq=1 ttl=64 time=0.031 ms
64 bytes from centosnet2.mynet (192.168.3.3): icmp_seq=2 ttl=64 time=0.135 ms
^C
--- centosnet2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 0.031/0.083/0.135/0.052 ms

发现使用自定义的网络,无需配置,可以直接连网,同时可以与同一个网段的主机之间直接使用主机名进行ping通。
此时再运行一个容器,但使用默认的bridge网络。

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
bdf99b0c1480        centos              "/bin/bash"         14 hours ago        Up 18 minutes                           centosnet2
2688d2a9c148        centos              "/bin/bash"         14 hours ago        Up 18 minutes                           centosnet1
68d3a7b97c71        centos              "/bin/bash"         2 days ago          Up 17 minutes                           centos01
[root@localhost ~]# docker network inspect mynet 
[
    {
    
    
        "Name": "mynet",
        "Id": "3013d0f266f4e243fd7af37c8c8e3d60b6a75c1744a5aa8b8c6cb05a5d25220c",
        "Created": "2021-05-19T05:20:32.230092567-04:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
    
    
            "Driver": "default",
            "Options": {
    
    },
            "Config": [
                {
    
    
                    "Subnet": "192.168.3.0/24",
                    "Gateway": "192.168.3.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Containers": {
    
    
            "2688d2a9c1487769e223d6f0d8c834197044ff2607e2896aa12d53a0dc0e8279": {
    
    
                "Name": "centosnet1",
                "EndpointID": "6cb4ba38bea24827af564af42fbbb0d1e9b16ecd4ba47327d4356d5bd966b700",
                "MacAddress": "02:42:c0:a8:03:02",
                "IPv4Address": "192.168.3.2/24",
                "IPv6Address": ""
            },
            "bdf99b0c14809435732551fa375ad77809f62fcdf5b01659ed741451cd41e223": {
    
    
                "Name": "centosnet2",
                "EndpointID": "8ebe1a1ce31c16aed22e347de3b10b6e1752f9c3af673b3377f88e045f6188e6",
                "MacAddress": "02:42:c0:a8:03:03",
                "IPv4Address": "192.168.3.3/24",
                "IPv6Address": ""
            }
        },
        "Options": {
    
    },
        "Labels": {
    
    }
    }
]
[root@localhost ~]# docker inspect bridge 
[
    {
    
    
        "Name": "bridge",
        "Id": "20e9044be43140025f8aa87002f62e1799e074b3b2c3c7c74956ed96b952d767",
        "Created": "2021-05-19T19:49:55.025845235-04:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
    
    
            "Driver": "default",
            "Options": null,
            "Config": [
                {
    
    
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Containers": {
    
    
            "68d3a7b97c71851e3391846d7fd248f718fbc0b88b6979d3009da66b86ac2be2": {
    
    
                "Name": "centos01",
                "EndpointID": "c6c22a88df1a493eee8383add2afb92d9ee21912392450939dcdd6eb1a9fb883",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
    
    
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {
    
    }
    }
]

查看mynet和bridge网络信息,可以看到当前centosnet1和centosnet2属于mynet网络,网络分别是192.168.3.2/24,192.168.3.3/24,而centos01属于bridge网络,网络是172.17.0.2/16。
此时使用centos01主机进行ping连接centosnet1

[root@localhost ~]# docker exec -it centos01 ping 192.168.3.2
PING 192.168.3.2 (192.168.3.2) 56(84) bytes of data.
^C
--- 192.168.3.2 ping statistics ---
71 packets transmitted, 0 received, 100% packet loss, time 166ms

发现无法连通,因为属于不同的网络。
此时使用connect命令将centos01加入到mynet网络中,再进行容器之间的ping通实验。

[root@localhost ~]# docker network connect mynet centos01
[root@localhost ~]# docker network inspect mynet
[
    {
    
    
        "Name": "mynet",
        "Id": "3013d0f266f4e243fd7af37c8c8e3d60b6a75c1744a5aa8b8c6cb05a5d25220c",
        "Created": "2021-05-19T05:20:32.230092567-04:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
    
    
            "Driver": "default",
            "Options": {
    
    },
            "Config": [
                {
    
    
                    "Subnet": "192.168.3.0/24",
                    "Gateway": "192.168.3.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Containers": {
    
    
            "2688d2a9c1487769e223d6f0d8c834197044ff2607e2896aa12d53a0dc0e8279": {
    
    
                "Name": "centosnet1",
                "EndpointID": "6cb4ba38bea24827af564af42fbbb0d1e9b16ecd4ba47327d4356d5bd966b700",
                "MacAddress": "02:42:c0:a8:03:02",
                "IPv4Address": "192.168.3.2/24",
                "IPv6Address": ""
            },
            "68d3a7b97c71851e3391846d7fd248f718fbc0b88b6979d3009da66b86ac2be2": {
    
    
                "Name": "centos01",
                "EndpointID": "45f33bd7f21d56844ab42741b0950b68f536496cc775562c82b401cdedb6d8c6",
                "MacAddress": "02:42:c0:a8:03:04",
                "IPv4Address": "192.168.3.4/24",
                "IPv6Address": ""
            },
            "bdf99b0c14809435732551fa375ad77809f62fcdf5b01659ed741451cd41e223": {
    
    
                "Name": "centosnet2",
                "EndpointID": "8ebe1a1ce31c16aed22e347de3b10b6e1752f9c3af673b3377f88e045f6188e6",
                "MacAddress": "02:42:c0:a8:03:03",
                "IPv4Address": "192.168.3.3/24",
                "IPv6Address": ""
            }
        },
        "Options": {
    
    },
        "Labels": {
    
    }
    }
]
[root@localhost ~]# docker exec -it centos01 ping 192.168.3.2
PING 192.168.3.2 (192.168.3.2) 56(84) bytes of data.
64 bytes from 192.168.3.2: icmp_seq=1 ttl=64 time=0.074 ms
64 bytes from 192.168.3.2: icmp_seq=2 ttl=64 time=0.131 ms
^C
--- 192.168.3.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.074/0.102/0.131/0.030 ms
[root@localhost ~]# docker exec -it centos01 ping centosnet2
PING centosnet2 (192.168.3.3) 56(84) bytes of data.
64 bytes from centosnet2.mynet (192.168.3.3): icmp_seq=1 ttl=64 time=0.220 ms
64 bytes from centosnet2.mynet (192.168.3.3): icmp_seq=2 ttl=64 time=0.053 ms
^C
--- centosnet2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 0.053/0.136/0.220/0.084 ms
[root@localhost ~]# 

发现此时mynet网络中加入了centos01,同时centos01可以ping通mynet网段,而且可以使用主机名进行ping通实验。
回到centos01的详细信息中,也可以看到,此时centos01的网络信息增加了一个mynet网络。

猜你喜欢

转载自blog.csdn.net/qq_26350199/article/details/117034965