docker自定义网络与网络连接

(1)自定义网络:

  docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet

    ##bridge -网络模式桥连接 subnet - 子网络网段 gateway - 路由 mynet- 网络名

 

 

 (2)查看网络信息:

  docker network inspect mynet

 

 

 

 自定义网络修复了docker0的容器之间无法用容器名相互ping通的问题,只能使用ip才能ping通,但是我们自定义的网络是可以用ip或者容器名相互ping通的

 

(3)我们在自己的网段里创建两个容器

  ① docker run -d -P --name tomcat01 --net mynet tomcat

  ② docker run -d -P --name tomcat02 --net mynet tomcat

 

 

 (4)查看自定义网络信息

    docker network inspect mynet

 

 (5)相互使用容器名ping测试

 

 

 

 相互可以ping通(测试成功)

 

结论:在docker 中直接 run 创建容器时不加自定义的网络,是无法使用容器名相互ping通的,只能通过ip。(通过docker0的桥连接相互通信)

我们自己定义好的网络在创建容器时,是可以直接通过容器名相互ping通的。

 

(6)我们如果在创建一个容器不在该网段内如何能和该网段内的容器进行ping呢?

    docker -d -P --name tomcat03 tomcat(没有加-net默认是走docker0的网段)

    docker -d -P --name tomcat04 tomcat(没有加-net默认是走docker0的网段)

 

    没有做网络链接时ping不通的

 

 

 

(7) 网络链接:(容器和网络链接,不是docker0和网络链接

    docker network connect mynet tomcat03

    链接完成之后再去ping

 

 

 为什么呢?原因:一个容器两个ip

[root@awei /]# docker network inspect mynet
[
    {
        "Name": "mynet",
        "Id": "f82e335a380b70d27116c9f2a6df5f063b527977a682e5622f9808fb3f4b0b02",
        "Created": "2020-07-30T09:46:56.227147587+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": {
            "0a58017c5bfdeb9904937bf26583b81c641c6211719d12f2cd3cc3876a159a3e": {
                "Name": "tomcat02",
                "EndpointID": "6f17ada1c5e8cb9e06db50aebef80da76ef9916b5cbfd7314ebd9af04d7f3919",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            },
            "125b2018a96149cf0b32914444cc1ac8b9b5e7c9510753a7fd1f2e5dfaf04546": {
                "Name": "tomcat01",
                "EndpointID": "69b3d904da02bddabde47cce4a1f164044eb5f20724f43e347d7a8e165714659",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            },
            "d1c4c32c1d6cf6deea78bd1d9ed4295d433fa3d4f1e264525ea0d05bbe4ba851": {
                "Name": "tomcat03",
                "EndpointID": "037234ab18713dea5abe06aa8167b046d01feea42d7ed2eaf0aa457d18309425",
                "MacAddress": "02:42:c0:a8:00:04",
                "IPv4Address": "192.168.0.4/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

    这里可以看到tomcat03加入到了mynet的网络中了

 

 

  (8)查看容器详细信息:

    docker inspect d1c4c32c1d6c(tomcat03的id)

 

 

 

[root@awei /]# docker network inspect mynet[    {        "Name": "mynet",        "Id": "f82e335a380b70d27116c9f2a6df5f063b527977a682e5622f9808fb3f4b0b02",        "Created": "2020-07-30T09:46:56.227147587+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": {            "0a58017c5bfdeb9904937bf26583b81c641c6211719d12f2cd3cc3876a159a3e": {                "Name": "tomcat02",                "EndpointID": "6f17ada1c5e8cb9e06db50aebef80da76ef9916b5cbfd7314ebd9af04d7f3919",                "MacAddress": "02:42:c0:a8:00:03",                "IPv4Address": "192.168.0.3/16",                "IPv6Address": ""            },            "125b2018a96149cf0b32914444cc1ac8b9b5e7c9510753a7fd1f2e5dfaf04546": {                "Name": "tomcat01",                "EndpointID": "69b3d904da02bddabde47cce4a1f164044eb5f20724f43e347d7a8e165714659",                "MacAddress": "02:42:c0:a8:00:02",                "IPv4Address": "192.168.0.2/16",                "IPv6Address": ""            },            "d1c4c32c1d6cf6deea78bd1d9ed4295d433fa3d4f1e264525ea0d05bbe4ba851": {                "Name": "tomcat03",                "EndpointID": "037234ab18713dea5abe06aa8167b046d01feea42d7ed2eaf0aa457d18309425",                "MacAddress": "02:42:c0:a8:00:04",                "IPv4Address": "192.168.0.4/16",                "IPv6Address": ""            }        },        "Options": {},        "Labels": {}    }]

 

posted @ 2020-07-30 10:20  潘小伟  阅读(4001)  评论(2编辑  收藏  举报