Today I hit on an issue whereby I tried to bring up a new Docker project, using a named network, and hit the following error:
Error response from daemon: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network
Code language: JavaScript (javascript)
Which isn’t the most helpful thing in flagging that I had hit the default maximum number of networks that a Docker host can have active.
There’s a potential quick fix to this, should you not actually have 30 real networks running:
docker network prune
This should remove all unused networks from the host machine. Like when you “forget” to delete a container after you’re finished with it, so Docker networks are not automatically deleted when no longer used. Therefore it is possible to accumulate a bunch of these.
But what happens when you do have 30 networks, and you now need 31?
Increasing Max Networks In Docker
I found two ways to solve this problem. One seems more configurable, but also a little more scary to implement than the other.
Both require restarting Docker, which if you do have 30+ networks on a host then you will likely be already 🤦 face palming 🤦 as this is a pain in the arse.
But hey, needs must.
Method 1: max-networks
OK, so method one is what I went with.
You will need to open the Docker daemon configuration file in whatever text editor you use.
I’m running Ubuntu. The file you need to edit is located at /etc/docker/daemon.json
.
If the file does not exist, create it. In my case this file did not exist.
Then, add the max-networks
option and set its value to the maximum number of networks you want to allow. For example:
{ "max-networks": 100 }
Save the file and restart the Docker daemon for the changes to take effect.
sudo service docker restart
You will then need to manually restart everything. For that I created a Makefile
to work through all my projects one by one and restart them. Happy to do a post on that, if you are interested, but for now it’s outside the scope of this.
Method 2: Add More default-address-pools
A second approach, and the more scarier of the two is one I found here.
You still need to edit /etc/docker/daemon.json
.
And you still need to restart Docker after doing this.
Here’s the config:
{
"default-address-pools": [
{
"base":"172.17.0.0/12",
"size":16
},
{
"base":"192.168.0.0/16",
"size":20
},
{
"base":"10.99.0.0/16",
"size":24
}
]
}
Code language: JSON / JSON with Comments (json)
This definitely gives more control.
The reason I don’t like it, personally, is because my knowledge of subnetting is not great. I mean, I get it. Some subnets are private and can be used for private networks. Others are public ranges and, whilst can be used, will cause headaches… so don’t.
Rather than me figuring out what the above would actually do, I would rather Docker kindly handle the problem with me simply passing a number.
Anyway, both do work. I tried both, but I went with the first one.