1. 程式人生 > 其它 >Docker網路之自定義網路

Docker網路之自定義網路

Docker網路之自定義網路

1、建立自定義網路

建立網路的命令是:

$ docker network create

使用方法:

[root@aliyun ~]# 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[])
      --config-from string   The network from which to copy the configuration
      --config-only          Create a configuration only network
  -d, --driver string        Driver to manage the Network (default "bridge")
      --gateway strings      IPv4 or IPv6 Gateway for the master subnet
      --ingress              Create swarm routing-mesh network
      --internal             Restrict external access to the network
      --ip-range strings     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
  -o, --opt map              Set driver specific options (default map[])
      --scope string         Control the network's scope
      --subnet strings       Subnet in CIDR format that represents a network segment

其中,subnet子網一定要配置,加上掩碼;driver預設就是bridge模式,寫不寫都可以;gateway也要寫一下,即閘道器,網路從哪個地方出去。

#自定義網路mynet
[root@aliyun ~]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
20cbe3257eda7d0999917f8b1ac59fbd879201e5c12d431c8f4dfb63840fc2db
#檢視mynet是否配置成功
[root@aliyun ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
8df3cdb08d2a   bridge    bridge    local
c3009610274a   host      host      local
20cbe3257eda   mynet     bridge    local
e6d7cbd64aa7   none      null      local
#檢視mynet網路詳細資訊
[root@aliyun ~]# docker network inspect mynet
[
    {
        "Name": "mynet",
        "Id": "20cbe3257eda7d0999917f8b1ac59fbd879201e5c12d431c8f4dfb63840fc2db",
        "Created": "2022-04-20T16:43:47.006249722+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

2、使用自定義網路

啟動兩個centos容器使用自定義網路

[root@aliyun ~]# docker run -itd --name tomcat01 --net mynet tomcat:v1
abed4e51eadfbca7a3ab0288d30be47a9fead7d076016e3f646d324cda1d25ba
[root@aliyun ~]# docker run -itd --name tomcat02 --net mynet tomcat:v1
259e2bdce64341c720a300767d0a9acaae12a085b42200384a70c670a6fdc781
[root@aliyun ~]# docker ps
CONTAINER ID   IMAGE       COMMAND             CREATED          STATUS          PORTS      NAMES
259e2bdce643   tomcat:v1   "catalina.sh run"   5 seconds ago    Up 4 seconds    8080/tcp   tomcat02
abed4e51eadf   tomcat:v1   "catalina.sh run"   11 seconds ago   Up 10 seconds   8080/tcp   tomcat01

再次檢視mynet網路配置資訊

[root@aliyun ~]# docker network inspect mynet
[
    {
        "Name": "mynet",
        "Id": "20cbe3257eda7d0999917f8b1ac59fbd879201e5c12d431c8f4dfb63840fc2db",
        "Created": "2022-04-20T16:43:47.006249722+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "259e2bdce64341c720a300767d0a9acaae12a085b42200384a70c670a6fdc781": {
                "Name": "tomcat02",
                "EndpointID": "26757361b31840d6b33eee01ea4b6d65c20c0ed4521f669eef9661a62571b044",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            },
            "abed4e51eadfbca7a3ab0288d30be47a9fead7d076016e3f646d324cda1d25ba": {
                "Name": "tomcat01",
                "EndpointID": "cbf2bcdbf401b057d4b29a7f64bc3462498188fe785fb8d3039d882cfdb0d11c",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

發現tomcat02和tomcat03容器已經加入了這個自定義網路。

自定義網路有什麼好處呢?

測試:

#使用容器名來測試網路連通性
#tomcat01容器ping tomcat02
[root@aliyun ~]# docker exec tomcat01 ping tomcat02
PING tomcat02 (192.168.0.3): 56 data bytes
64 bytes from 192.168.0.3: icmp_seq=0 ttl=64 time=0.096 ms
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.099 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.094 ms
^C
#tomcat02容器ping tomcat01
[root@aliyun ~]# docker exec tomcat02 ping tomcat01
PING tomcat01 (192.168.0.2): 56 data bytes
64 bytes from 192.168.0.2: icmp_seq=0 ttl=64 time=0.077 ms
64 bytes from 192.168.0.2: icmp_seq=1 ttl=64 time=0.107 ms
64 bytes from 192.168.0.2: icmp_seq=2 ttl=64 time=0.102 ms
64 bytes from 192.168.0.2: icmp_seq=3 ttl=64 time=0.096 ms
^C

發現可相互ping通,比--link更加方便