docker

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > docker > Docker network自定义网络

Docker network自定义网络方式

作者:AdamShyly

这篇文章主要介绍了Docker network自定义网络方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

Docker network自定义网络

现在官方已经废弃了 run --link 的使用(可通过容器名来 Ping 通),所以我们可以采用自定义网络的做法来联通容器

第一步

组建子网,我取名为 nettest

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

注意我这里是使用IP地址前16位作为CIDR前缀,不要和集群内的其他子网冲突

可用 docker network inspect nettest 指令查看子网信息

[
    {
        "Name": "nettest",
        "Id": "241ec33539eae571bd0ce269f9b6ec8525dfbbe5e96d1c7e00539fa10a0adc10",
        "Created": "2022-03-31T13:07:44.672058045+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": {}
    }
]

第二步

向nettest子网添加容器,这里我将两个我自己定义的centos容器加入了该子网

docker run -d -P --name centos-net-01 --net nettest mycentos
docker run -d -P --name centos-net-02 --net nettest mycentos

如果不指定net,则会自动加入bridge网络(对应于docker0网桥)

此时再次查看子网信息

[
    {
        "Name": "nettest",
        "Id": "241ec33539eae571bd0ce269f9b6ec8525dfbbe5e96d1c7e00539fa10a0adc10",
        "Created": "2022-03-31T13:07:44.672058045+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": {
            "126e1b28a5958ee159fa16073501cbc61508ce803ccaab1682489fb859fe6ae3": {
                "Name": "centos-net-02",
                "EndpointID": "14ae44c2a7f9b7572b814e86c06fef3597d5eb2308ea588c91cabe38d22ec426",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            },
            "903a5af6d1050998523a23b10f0dcee86f7d8a35cca8544ea5499b5cd6d22c9f": {
                "Name": "centos-net-01",
                "EndpointID": "7c1ff29b1fad9df3943d4d3eda6520cce66ebcbe61a8c5127b3161ddb7f6649a",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

可以发现 "Containers" 下多了两个容器对象

而网关为宿主机

第三步

测试子网内的容器是否能联通

可以发现通过容器名和其对应的内网IP都能Ping通

而这种自定义网络我在容器的/etc/hosts中并没有看到其他容器的域名IP映射

如果后续还需要向该子网添加容器,可以使用以下指令

docker network connect multi-host-network container1
[root@Hadoop100 docker-test-volume]# docker network connect nettest centos-net-03
[root@Hadoop100 docker-test-volume]# docker network inspect nettest
[
    {
        "Name": "nettest",
        "Id": "241ec33539eae571bd0ce269f9b6ec8525dfbbe5e96d1c7e00539fa10a0adc10",
        "Created": "2022-03-31T13:07:44.672058045+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": {
            "126e1b28a5958ee159fa16073501cbc61508ce803ccaab1682489fb859fe6ae3": {
                "Name": "centos-net-02",
                "EndpointID": "14ae44c2a7f9b7572b814e86c06fef3597d5eb2308ea588c91cabe38d22ec426",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            },
            "163f33f66ba95bc9ee7dece20f87fe7c18ec290cd2618145bba1a07d7ebf2b6e": {
                "Name": "centos-net-03",
                "EndpointID": "6854d517cae02fa4b41fdd4c6406afd61c6529a573257829bc78003dca01b079",
                "MacAddress": "02:42:c0:a8:00:04",
                "IPv4Address": "192.168.0.4/16",
                "IPv6Address": ""
            },
            "903a5af6d1050998523a23b10f0dcee86f7d8a35cca8544ea5499b5cd6d22c9f": {
                "Name": "centos-net-01",
                "EndpointID": "7c1ff29b1fad9df3943d4d3eda6520cce66ebcbe61a8c5127b3161ddb7f6649a",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

我之前使用run --link来联通容器时,发现其中被link的源容器被我stop后两个容器都从docker进程中消失了,而这种自定义网络则不会出现这种奇怪的现象

最后需要强调一点,千万不要试图去使用 docker network rm -f $(docker network ls -q) 这种指令,这可能会把docker默认创建的网络也给删去!

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文