为什么docker为所有容器提供不同范围的IP地址,如何将其设置为默认值?

时间:2018-11-01 10:38:05

标签: docker ubuntu docker-compose ubuntu-16.04

我在创建docker容器时获得了不同范围的IP地址。

Docker版本:18.06

Ubuntu:16.04

Docker为所有容器提供默认IP地址。

  

默认IP地址范围172.17.0.1/16

但是这次它提供了不同范围的IP地址(168.92.x.x)。

问题:为什么docker为所有容器提供不同范围的IP地址,如何将其设置为默认值?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

但是我不知道为什么发生(Docker assign different Range of IP address to all containers)

创建Docker容器时,它是在创建一个新的projectName_default网络。

在此网络中已定义了以下数据:

ubuntu@pro_staging:~> sudo docker network ls
NETWORK ID          NAME                 DRIVER              SCOPE
fb1861a4afc0        projecName_default   bridge              local
e60b761f9aa9        bridge               bridge              local
0ab8ff5w5f2d        host                 host                local
a4e689dc9dcb        none                 null                local
ubuntu@pro_staging:~> sudo docker inspect projecName_default
[
    {
        "Name": "projecName_default",
        "Id": "fb1861a4ac4059f6abea25b49c331390d651fdsdde24e15a848507723c2e4cd4f",
        "Created": "2018-11-02T11:15:37.136315048Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "87855a403d18df21910d6fd6d0132328d09a954800200e46ad35fa325fb42897f": {
                "Name": "projecName_default_containr1",
                "EndpointID": "a55d85fb759d997957fdd15eddba28e1598176d7c5bdb9e2d42fd1967f7f763b",
                "MacAddress": "02:42:ac:14:00:03",
                "IPv4Address": "172.20.0.3/16",
                "IPv6Address": ""
            },
            "9b0d45d8310e9877de147b89dd74c7dfb2d55a1660c7ab664f3e831d100eee0f": {
                "Name": "projecName_default_containr_2",
                "EndpointID": "68ba68e2c4bb3a7a8861b1fa2d43edecbfc0d29586c8169361bb47e94c37d460",
                "MacAddress": "02:42:ac:14:00:04",
                "IPv4Address": "172.20.0.4/16",
                "IPv6Address": ""
            },
            "edf3bf11199dba6b7ada6105773341baca4790ff713d40988ffbe54cd73a638d": {
                "Name": "projecName_default_containr_3",
                "EndpointID": "62d17e5b7f1909d60aa4c79fccb276f5a50f84d79b9da8d6f6bf5c7f54cb313f",
                "MacAddress": "02:42:ac:14:00:02",
                "IPv4Address": "172.20.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

解决方案:

# Remove default network 
ubuntu@pro_staging:~> sudo docker network rm projecName_default
#Restart docker
ubuntu@pro_staging:~> sudo systemctl restart docker
#After restart docker create container again.