Forbid docker to use specific network

docker, docker-compose

Solution

When you create a network inside `docker-compose.yml`, you can specify IP range, subnet, gateway, etc. You can do it this way.

version: "3"

services:    
  service1:
    build: .
    ...

networks:
  default:
    subnet: 172.28.0.0/16
    ip_range: 172.28.5.0/24
    gateway: 172.28.5.254

To do this in the Docker daemon, you have pass `--bip` and `--fixed-cidr` parameters.

From Docker docs: https://docs.docker.com/engine/userguide/networking/default_network/custom-docker0/

`--bip=CIDR`: supply a specific IP address and netmask for the `docker0` bridge, using standard CIDR notation. For example: `192.168.1.5/24`.

`--fixed-cidr=CIDR`: restrict the IP range from the `docker0` subnet, using standard CIDR notation. For example: `172.16.1.0/28`. This range must be an IPv4 range for fixed IPs, such as `10.20.0.0/16`, and must be a subset of the bridge IP range (docker0 or set using --bridge). For example, with --fixed-cidr=`192.168.1.0/25`, IPs for your containers will be chosen from the first half of addresses included in the `192.168.1.0/24` subnet.

To make this changes permanent, create or edit `/etc/docker/daemon.json` and add options `bip` and `fixed-cidr`.

{
"bip": "192.168.1.5/24",
"fixed-cidr": "192.168.1.0/25"
}

Problem

Is there a way to tell docker to not use some specific network when runing `docker-compose up`? I am using some out of the box examples (`hyperledger`) and each time docker takes an address that breaks my remote connection. ``` [xxx@xxx fabric]$ docker network inspect some_network [ { "Name": "some_network", "Id": "xxx", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "172.30.0.0/16", "Gateway": "172.30.0.1/16" } ] }, "Internal": false, "Containers": {}, "Options": {}, "Labels": {} } ] ``` What I would like is to tell docker (without editing the docker-compose.yaml file) to never use the network `172.30.0.0/16`.

Original source