更改docker服务网段分配地址

docker安装完毕后,会自动生成一个网卡名为docker0的网桥,如果其默认分配的网段地址和已有地址段冲突,可按如下步骤修改。

  • 查看默认地址段如下
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:6e:e3:0a:62  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 删除原有配置
yum install bridge-utils
systemctl stop docker.service
ip link set dev docker0 down
brctl delbr docker0
iptables -t nat -F POSTROUTING
  • 创建新的网桥
brctl addbr docker0
ip addr add 172.66.1.1/24 dev docker0
ip link set dev docker0 up
  • 在docker配置文件中追加参数
# cat /etc/docker/daemon.json
{
"bip": "172.66.1.1/24"
}
  • 启动docker
systemctl start docker.service
  • 查看是否更改成功
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.66.1.1  netmask 255.255.255.0  broadcast 172.66.1.255
        ether 8e:08:df:00:c3:dc  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

更改docker-compose网桥地址

使用单机容器编码工具,docker-compose时,容器之间的通信网络会用到br-xxx网桥,该网桥会在宿主机建立,示例如下:

[root@xingyongsheng ~]# ifconfig | head
br-35fc9d6212bd: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.238.1  netmask 255.255.255.0  broadcast 172.16.238.255
        ether 02:42:7a:4f:9a:b9  txqueuelen 0  (Ethernet)
        RX packets 6441897  bytes 8754859570 (8.1 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1525889  bytes 953197479 (909.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

该网段也有可能与,已有网段重复,若要修改此网段,可按如下步骤进行。安全停掉所有用docker-compose编排的容器,这里建议使用docker-compose down来彻底停掉容器,并自动帮你移除docker-compose之前创建的网桥。之后修改docker-compose.yml文件,增加自定义网络段,如下所示。

version: '2.1'
services:
  test-service:
    image: xxx
    container_name: xxxxx
    restart: always
    networks:
      - as4k-test

networks:
  as4k-test:
    ipam:
      config:
        - subnet: 172.16.238.0/24
          gateway: 172.16.238.1
  • 用新的docker-compose文件,启动容器。

作者:阿胜4K
出处:https://www.cnblogs.com/asheng2016/p/10532724.html

posted @ 2019-03-14 18:56 阿胜4K 阅读(...) 评论(...) 编辑 收藏