创建新网络时,docker不使用已定义池中的IP [英] docker doesn't use IP from defined pool when creating new networks

查看:79
本文介绍了创建新网络时,docker不使用已定义池中的IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为避免冲突,我想为 docker0 桥和启动新容器时docker守护进程动态创建的所有接口使用定义范围的子网.

To avoid conflicts, I want to use a defined range of subnets for the docker0 bridge and all interfaces dynamically created by the docker deamon when a new container is started.

快速浏览docker文档后,我发现了很有前途的 fixed-cidr 选项.我将此选项添加到了daemon.json中.我的配置如下:

After a quick look in the docker documentation I found the promising fixed-cidr option. I added this option to my daemon.json. My configuration looks as follows:

/etc/docker/daemon.json

{
  "bip": "192.168.89.1/22",
  "fixed-cidr": "192.168.89.2/24"
}

重启docker deamon后,配置看起来很有希望:

after restarting the docker deamon the configuration looks promising:

docker inspect bridge

[
    {
        "Name": "bridge",
        "Id": "365e0d373bcfc82bc73c623d680dcaee773e247f631e5b2324e3b63401bcf6fd",
        "Created": "2018-03-19T09:59:22.20439309+01:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "192.168.89.1/22",
                    "IPRange": "192.168.89.0/24",
                    "Gateway": "192.168.89.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

但是由于某些原因,当我创建一个新网络时:

But for some reason, when I create a new network:

docker network create test-network

它从默认IP范围获取一个新地址:

it gets a new address from default IP range:

docker inspect test-network
[
    {
        "Name": "test-network",
        "Id": "bf0f6baa8239c73a9524f8a77035bc2be18a67ad4d0d2ba4f73b3d175f5315b3",
        "Created": "2018-03-19T10:31:24.450183553+01:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

现在,我有一个临时解决方案.我只需在docker-compose.yml

For now, I have a temporary solutation. I'll simply define a custom subnet in my docker-compose.yml

yaml
networks:
  default:
    ipam:
      config:
      - subnet: 192.168.89.2/24

但这会导致进一步的问题,因为由于网络冲突而无法在同一主机上多次启动项目.

But this leads to further problems as a cannot start the project multiple times on the same hosts, because of network conflicts.

有人知道问题出在哪里吗?

Does anyone know where the problem is?

修改:它似乎是Docker中缺少的功能. Github问题

It seems to be a missing feature in docker. Github Issue

幸运的是,此功能已于几天前提交给master分支:

Fortunately this feature has been committed to master branch some days ago: Committed feature

推荐答案

在新的docker版本中,提供了 default-address-pools 选项.

In new docker releases there is the default-address-pools option.

这篇关于创建新网络时,docker不使用已定义池中的IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆