docker-compose启动时创建网络失败问题解决方法

一直听说docker-compose是好东西,一直没用上。今天有时间学习下:https://www.runoob.com/docker/docker-compose.html

问题现象

执行docker-compose up的时候报错

(base) [root@VM-100-213-centos ~/composetest]# docker-compose up
Creating network "composetest_default" with the default driver
ERROR: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network

尝试方法

先查看networks,执行prune,问题依旧

(base) [root@VM-100-213-centos ~/composetest]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f5789b1eaa7b        bridge              bridge              local
a09d489d2abd        host                host                local
ef2dbca65b2a        none                null                local
(base) [root@VM-100-213-centos ~/composetest]# docker network prune
WARNING! This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N] y
(base) [root@VM-100-213-centos ~/composetest]# docker-compose up
Creating network "composetest_default" with the default driver
ERROR: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network
(base) [root@VM-100-213-centos ~/composetest]#  

执行docker network rm试试

(base) [root@VM-100-213-centos ~/composetest]# docker network rm $(docker network ls -q)
Error response from daemon: bridge is a pre-defined network and cannot be removed
Error response from daemon: host is a pre-defined network and cannot be removed
Error response from daemon: none is a pre-defined network and cannot be removed 

原来docker默认有3种网络:bridge、host、none,这三个网络是不能被删除的。所以,网上常见的解决方法不适用。

解决方法

方法一

在docker-compose.yml中指定网络配置

networks:
  default:
    driver: bridge
    ipam:
      config:
        - subnet: 172.31.1.0/24

方法二

首先手动创建网络

docker network create my-network --subnet 172.31.1.0/24

然后在docker-compose.yml指定上面创建的网络。将下面一段配置,添加到docker-compose.yml文件底部

networks:
  default:
    external: 
      name: my-network

  • 10
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值