其他分享
首页 > 其他分享> > 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更加方便

标签:docker,network,自定义,--,网络,192.168,64,Docker,mynet
来源: https://www.cnblogs.com/Torres-tao/p/16170594.html