Analysis of Implementation Method of Docker Default Network Segment Modification

  • 2021-09-16 08:35:38
  • OfStack

Background

All the servers of the company are purchased Alibaba Cloud ECS hosts, and the default intranet segment is 172.16. 0.0/12. The services on the test suits are basically deployed using Docker.
All Docker installed by previous colleagues on this machine are default configurations, and the network segment is 172.17. 0.0/24. In actual use, there will be a need to access the host machine, which will conflict with other host ip, so it is necessary to modify the default network segment of the container.

Solution

1. Stop the Docker service on the host machine

sudo systemctl stop docker

2. Modify the container profil/etc/docker/daemon. json


sudo cat /etc/docker/daemon.json
{
  "registry-mirrors": ["https://gbs4gco0.mirror.aliyuncs.com"],
  "bip": "10.50.0.1/16",
}

As described in the code above, the bip field in the configuration file sets the host's docker network segment and configures it as a private network address (192.168. 0.0/10.0. 0.0/172.0. 0.0)

3. Start Docker service

sudo systemctl daemon-reload
sudo systemctl start docker

4. Verify the docker-0 bridge address


sudo ifconfig
...
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
    inet 10.50.0.1 netmask 255.255.0.0 broadcast 10.50.255.255
    inet6 fe80::42:46ff:fe18:add5 prefixlen 64 scopeid 0x20<link>
    ether 02:42:46:18:ad:d5 txqueuelen 0 (Ethernet)
    RX packets 14697564 bytes 5943503139 (5.5 GiB)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 15379709 bytes 6846099156 (6.3 GiB)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
...

At this point, the default network segment of Docker has been modified. You can check whether the ip of the previous container has been modified

sudo docker container inspect alipne --format="{{.NetworkSettings.IPAdress}}"
10.50.0.2


Related articles: