Changing default subnet for docker custom networks
Asked Answered
B

1

28

Our internal network has the range 172.20.0.0/16 reserved for internal purposes and docker uses the 172 range by default for its internal networking. I can reset the bridge to live in 192.168 by providing the bip setting to the daemon:

➜  ~ sudo cat /etc/docker/daemon.json
{
  "bip": "192.168.2.1/24"
}

➜  ~ ifconfig                        
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
    inet 192.168.2.1  netmask 255.255.255.0  broadcast 0.0.0.0

However, when creating new custom networks via docker network create or by defining them in the networks sections of the docker-compose.yaml these are still created in 172, thus eventually clashing with 172.20:

➜  ~ docker network create foo
610fd0b7ccde621f87d40f8bcbed1699b22788b70a75223264bb14f7e63f5a87
➜  ~ docker network inspect foo | grep Subnet
                "Subnet": "172.17.0.0/16",
➜  ~ docker network create foo1              
d897eab31b2c558517df7fb096fab4af9a4282c286fc9b6bb022be7382d8b4e7
➜  ~ docker network inspect foo1 | grep Subnet
                "Subnet": "172.18.0.0/16",

I understand I can provide the subnet value to docker network create, but I rather want all such subnets created under 192.168.*.

How can one configure dockerd to do this automatically?

Biostatics answered 16/5, 2017 at 14:1 Comment(0)
N
54

For anyone who found this question. Now it is possible.

$ docker -v
Docker version 18.06.0-ce, build 0ffa825

Edit or create config file for docker daemon:

# nano /etc/docker/daemon.json

Add lines:

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

Restart dockerd:

# service docker restart

Check the result:

$ docker network create foo
$ docker network inspect foo | grep Subnet
                    "Subnet": "10.10.1.0/24"

It works for docker-compose too.

Your "bip": "192.168.2.1/24" works for bridge0 only. It means that any container which run without --network will use this default network.

Nitrification answered 19/10, 2018 at 2:41 Comment(5)
Does this work with networks generated by docker-compose also? Probably not?Pinky
Dude, why it is /16 with a size of 24?Decagram
@Obay Abd-Algader docker will create a "pool" of 10.10.0.0/16. Each subnet will then take /24 out of the pool.Columbic
Thanks, I got it @anch2150. Also I noticed that old networks stay there unless removed manually.Decagram
Confirming that this works with Docker Desktop in Windows as well. Just add this config by opening the GUI and navigating to Settings>Docker Engine> Add this text as an entry inside the {} outer braces, then click the Apply& Restart button at the bottom.Nevanevada

© 2022 - 2024 — McMap. All rights reserved.