Restrict Docker network ranges

06 Nov 2021

So I have had some issues where some of my Docker images have been created with a network of 192.168.16.x. And I have my VLAN for WIFI is running on 192.168.20.x. This is not an issue though, but Docker creates an /16 network for like two docker images. So because this was one of my internal Docker servers serving my Node-Red dashboard. I could not access it from my phone because the Docker container could not respond due to wrong routing because of the 192.168.16.0/16 network that had been created. The response to my WIFI network 192.168.20.x was routed to the 192.168.16.x network.

Daemon.json config file

The easies way I have found was to create /etc/docker/daemon.json to restrict Docker daemon network creation.

This is my content

{
  "default-address-pools":
  [
    { "base":"172.17.0.0/16","size":24}
  ]
}

As far as I know you can have multiple address pools for Docker networks configured in the daemon.json configuration file.

Then just restart Docker

sudo systemctl restart docker

If you had any docker container running before doing this you have to manually stop them and remove the network and then restart each container. Docker-compose will remove networks for you so if you are running docker-compose you don’t have to remove any networks. Just run docker-compose down and then docker-compose up -d to re-create the service(s) and network.