更改 docker 自定义网络的默认子网 [英] Changing default subnet for docker custom networks

查看:24
本文介绍了更改 docker 自定义网络的默认子网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的内部网络保留了 172.20.0.0/16 范围供内部使用,docker 默认使用 172 范围作为其内部网络.我可以通过向守护程序提供 bip 设置来重置网桥以使其生活在 192.168 中:

Our internal network has the range 172.20.0.0/16 reserved for internal purposes and docker uses the 172 range by default for its internal networking. I can reset the bridge to live in 192.168 by providing the bip setting to the daemon:

➜  ~ sudo cat /etc/docker/daemon.json
{
  "bip": "192.168.2.1/24"
}

➜  ~ ifconfig                        
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
    inet 192.168.2.1  netmask 255.255.255.0  broadcast 0.0.0.0

但是,当通过 docker network create 或通过在 docker-compose.yamlnetworks 部分定义它们来创建新的自定义网络时这些仍然是在 172 中创建的,因此最终与 172.20 发生冲突:

However, when creating new custom networks via docker network create or by defining them in the networks sections of the docker-compose.yaml these are still created in 172, thus eventually clashing with 172.20:

➜  ~ docker network create foo
610fd0b7ccde621f87d40f8bcbed1699b22788b70a75223264bb14f7e63f5a87
➜  ~ docker network inspect foo | grep Subnet
                "Subnet": "172.17.0.0/16",
➜  ~ docker network create foo1              
d897eab31b2c558517df7fb096fab4af9a4282c286fc9b6bb022be7382d8b4e7
➜  ~ docker network inspect foo1 | grep Subnet
                "Subnet": "172.18.0.0/16",

我知道我可以向 docker network create 提供子网值,但我更希望在 192.168.* 下创建所有此类子网.

I understand I can provide the subnet value to docker network create, but I rather want all such subnets created under 192.168.*.

如何配置 dockerd 以自动执行此操作?

How can one configure dockerd to do this automatically?

推荐答案

对于发现此问题的任何人.现在有可能了.

For anyone who found this question. Now it is possible.

$ docker -v
Docker version 18.06.0-ce, build 0ffa825

为 docker daemon 编辑或创建配置文件:

Edit or create config file for docker daemon:

# nano /etc/docker/daemon.json

添加行:

{
  "default-address-pools":
  [
    {"base":"10.10.0.0/16","size":24}
  ]
}

重启dockerd:

# service docker restart

检查结果:

$ docker network create foo
$ docker network inspect foo | grep Subnet
                    "Subnet": "10.10.1.0/24"

它也适用于 docker-compose.

It works for docker-compose too.

您的 "bip": "192.168.2.1/24" 仅适用于 bridge0.这意味着任何没有 --network 运行的容器都将使用这个默认网络.

Your "bip": "192.168.2.1/24" works for bridge0 only. It means that any container which run without --network will use this default network.

这篇关于更改 docker 自定义网络的默认子网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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