1.创建网络bridge

(1) 创建新的网络 为lss-net

docker network create -d bridge lss-net

-d 后面第一个bridge是网络的连接方式

(2) 查看当前的网络列表

docker network ls

查看bridge列表

brctl show
2. 创建容器test3,连接到网络lss-net中

(1)创建容器

docker run -d --name test3 --network my-bridge busybox /bin/sh -c "while true ;do sleep 3600;done"

(2) 查看网络 lss-net 的连接情况

[root@localhost ~]# docker network inspect lss-net
[
    {
        "Name": "lss-net",
        "Id": "f0626cf13f9abe0bae5c741befc06df47a2bc5093e13ae5d1e94be8b85e57ae9",
        "Created": "2019-03-01T10:20:10.254037204+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "ceb07ba99ff7ac35bf4d7ecbafeae5963906383b763b91120f76e73479bb97b5": {
                "Name": "test3",
                "EndpointID": "2602f2d752de46c8a2c6b01829e291b2395d18c4163da6d52702630ab8c029fd",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

发现该网络已连接到容器test3中

3. 手动将指定网络与指定容器连接

(1)操作指令为:

docker network connect [网络] [容器]

(2) 下面将容器test2也连接到网络 lss-net中

docker network connect lss-net test2

(3) 此时再次查看 网络lss-net的连接情况,发现test2和test3都在其中

docker network inspect lss-net

 "Containers": {
            "89a8286ce291b63105bb08c78ff8aa3dab99c50765c421b3ea92cebd97b63174": {
                "Name": "test2",
                "EndpointID": "bca33cb77116a1bd9814ff42ae378bdb06a0079c198ac764b826d5efc894da5b",
                "MacAddress": "02:42:ac:13:00:03",
                "IPv4Address": "172.19.0.3/16",
                "IPv6Address": ""
            },
            "ceb07ba99ff7ac35bf4d7ecbafeae5963906383b763b91120f76e73479bb97b5": {
                "Name": "test3",
                "EndpointID": "2602f2d752de46c8a2c6b01829e291b2395d18c4163da6d52702630ab8c029fd",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            }
        },

此时,test2与test3都是可以ping通的.

(注意是自己创建的网络,不是系统自己默认的网络),这样双方都可以通过名字互ping上)

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐