修改docker-compose容器默认ip范围

修改docker-compose容器默认ip范围

问题描述

项目在测试环境使用docker-compose方式部署微服务,刚开始一切正常。某次重新部署后突然出现内网172.31网段服务器无法访问。

问题定位

  1. 虚机访问非172.31网段一切正常;
  2. 同网段其他虚机访问172.31网段一切正常;
  3. 虚机防火墙设置正常;

ip addr查看网址时发现存在docker虚拟网卡使用了172.31网段地址,与内网172.31网段地址冲突造成服务器无法访问。

解决方案

When user creates a network without specifying a --subnet, docker will pick a subnet for the network from the static set 172.[17-31].0.0/16 and 192.168.[0-240].0/20 for the local scope networks and from the static set 10.[0-255].[0-255].0/24 for the global scope networks.

  1. 修改docker相关配置,调整默认的IP范围(推荐方式);
  2. 修改docker-compose配置文件,指定网络配置;

这里主要介绍方案1.

  1. 停用docker;
systemctl stop docker
  1. 删除之前docker创建的网络
# 查看docker网络信息
docker network ls

# 删除未使用的网络,主要是把发生网络冲突的那个网段给清掉
docker network prune
  1. 修改docker配置文件
vi /etc/docker/daemon.json

在配置文件中添加以下内容,其中default-address-pools的base表示CIDR地址,size表示docker创建的网络的掩码长度,CIDR的掩码长度应该小于size,否则docker将会出现网络失败。这里使用192.168网段地址,其中CIDR为16为掩码,划分的网络子网掩码24位,理论可以划分出2(32-16)-(32-24)=28=256个子网。

{
    "registry-mirrors": [
        "https://docker.mirrors.ustc.edu.cn/"
    ],
    "debug": true,
    "default-address-pools": [
        {
            "base": "192.168.1.1/16",
            "size": 24
        }
    ]
}

4)重新启动docker

# 请再次确认已将docker之前创建的网络删除
# 启动docker
systemctl start docker

设置bip无效

在daemon配置文件中单纯配置bip只对docker启动的容器有效,但是对docker-compose启动的容器无效。

Docker engine is started with --bip argument, but it is ignored by docker-compose, which set up an additional bridge interface in addition to the docker0 interface. It is named br-f4a912f7750b and has the typical Docker 172.17.x.x format, which happens to conflict with the network I’m currently using.

参考链接:
[1]: https://github.com/docker/compose/issues/4336
[2]: https://github.com/moby/moby/pull/29376

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值