How to Change Docker's Virtual Network Subnet?

Docker is a popular container technology that lets developers and operations staff deploy and run applications in different environments. Docker uses virtual networks to connect containers to the host and to each other. By default, Docker creates a virtual network named bridge that uses the 172.17.0.0/16 subnet, which means container IP addresses range from 172.17.0.1 to 172.17.255.254. This subnet can conflict with the IP addresses of the host or other network devices, causing network communication problems. So sometimes we need to change Docker's virtual network subnet to avoid IP address conflicts.

How to Do It

  1. Stop all running containers, and stop the Docker service. You can use the following commands:
1
2
docker stop $(docker ps -aq)
sudo systemctl stop docker
  1. Edit Docker's configuration file /etc/docker/daemon.json; if the file doesn't exist, create it. Add a key-value pair to the file, with bip as the key and the subnet we want to use as the value. For example, if we want to use the 192.168.1.0/24 subnet, we can add the following:
1
2
3
{
"bip": "192.168.1.1/24"
}
  1. Save and close the configuration file, then restart the Docker service. You can use the following command:
1
sudo systemctl start docker
  1. Check the bridge network's details to confirm that the subnet was changed successfully. You can use the following command:
1
docker network inspect bridge

If the output shows the subnet and IP address we specified, the change was successful.

Notes:

  • Changing the virtual network subnet will cause previously created containers to lose network connectivity, so it's recommended to back up or remove containers before making the change.
  • Changing the virtual network subnet may affect the settings of other custom networks or port mappings, so it's recommended to check and reconfigure network-related options after the change.
  • Changing the virtual network subnet may conflict with the IP addresses of other hosts or network devices, so it's recommended to plan your IP address allocation before the change and avoid using public or reserved IP addresses.