Loading

docker 网络命令总结

查看网络模式

docker network ls

NETWORK ID          NAME                DRIVER              SCOPE
6856e3d6a118        bridge              bridge              local
74519c816cff        host                host                local
9187a93406a2        none                null                local
ecb198e04339        test-net            bridge              local

查看容器网络配置

docker inspect 06c88a111a61 | grep -i "network" 

"NetworkMode": "default",
"NetworkSettings": {
      "Networks": {
            "NetworkID": "6856e3d6a118c05fb39f557be229a5cb3ee7a9cc66788a25ce56e19f73bda999",

类似用法: docker inspect 06c88a111a61 | grep -i "IP" 查看ip信息

查看所有容器的ip

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}} {{.IPAddress}} {{end}}' $(docker ps -aq)

/redis -  172.17.0.4  172.18.0.4
/php-fpm -  172.17.0.3  172.18.0.3
/nginx -  172.17.0.2  172.18.0.2
/myrabbit -  172.17.0.4

创建docker 网络

docker network create -d bridge --subnet 172.17.0.0/16 my-net
(-d bridge可以省略)

Error response from daemon: Pool overlaps with other one on this address space
如果报错,说明172.17.0.0已被其他网络空间占用(默认bridge就是这个子网段),此时可以改172.18.0.0
docker network create --subnet 172.18.0.0/16 my-net

查看网络具体信息

docker network inspect my-net

[
    {
        "Name": "my-net",
        "Id": "684e82a9f4d171ee379eb637c2190863d21d7d23be659994db13643a8861da72",
        "Created": "2021-03-16T02:42:28.239558199Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

容器创建时,连接网络和指定ip

docker run --network=my-net --ip=172.18.0.3 -itd --name=redis-test -d redis

容器创建后,新增网络和指定ip(相当于给某容器多接上一条网线)

docker run --name redis-test2 -d redis  (默认接入bridge网络,172.17.0.0网段)

docker network connect --ip 172.18.0.5 my-net redis-test2 (连接新网络,并指定ip)

docker inspect redis-test2

可以查看到接入了两个网络:

"Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "70d8553b1b8fa83cea39a42943f86734c4883f6ab19913cfe1e3d57dc312e715",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                },
                "my-net": {
                    "IPAMConfig": {
                        "IPv4Address": "172.18.0.5"
                    },
                    "Links": null,
                    "Aliases": [
                        "393c4a6faeb2"
                    ],
                    "NetworkID": "",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": {}
                }
            }
        }

网络别名(创建容器时指定)

docker run --network=my-net --ip=172.18.0.6 --network-alias r4-alias -itd --name=redis-test3 -d redis

可以看到,此时指定别名r4-alias,

因为redis-test容器也是my-net网络内,此时可以ping r4-alias

docker exec -it redis-test bash
ping r4-alias
PING m5 (172.18.0.6) 56(84) bytes of data.
64 bytes from redis-test3.my-net (172.18.0.6): icmp_seq=1 ttl=64 time=0.146 ms
64 bytes from redis-test3.my-net (172.18.0.6): icmp_seq=2 ttl=64 time=0.112 ms
64 bytes from redis-test3.my-net (172.18.0.6): icmp_seq=3 ttl=64 time=0.108 ms

网络别名添加(创建容器之后追加)

docker run  -itd --name=redis-test4 redis
docker network connect --alias r4-alias --ip=172.18.0.7 my-net redis-test4
posted @ 2021-03-12 12:13  集君  阅读(227)  评论(0编辑  收藏  举报