docker run: how to avoid overlap between Docker and external host on 172.18.x.x IP range

Viewed 834

i'm using docker run to test my container locally. I found that it is unable to connect to a certain host in my company's network, failing with "no route to host". It turns out this host has an IP address of 172.18.x.x, which overlaps with Docker's networking.

So, is there a way to change the docker run configuration so that it doesn't claim this particular IP range? I've already tried changing the bip and default-address-pools options in the Docker daemon configuration file, but that didn't solve the problem.

2 Answers

Create a docker config file if one doesn't exist in /etc/docker/daemon.json

Add an entry to the daemon.json with the subnet for the docker bridge0 to run in (you can add your desired subnet here), under the "bip" entry e.g. - "bip": "192.168.1.5/24"

Restart docker service : sudo systemctl restart docker

NOTE: Be ware not to use the loopback address in the subnet, i.e. IPs that end with 0 like 192.168.1.0/24 The bridge0 subnet should have enough addresses for all the containers on the machine that use the default network. skyformation does NOT use the default network.

Verify it worked by running ifconfig docker0 | grep -Po '(?<=inet )[\d.]+' . It should print out the IP address specified in "bip" example of daemon.json like this

{ "bip": "192.168.1.5/24" }

Now in your docker-compose file, add network tab as this.

networks:
  isolated_nw:
    driver: bridge
    ipam:
      config:
        - subnet: 192.167.0.0/16

Now restart your docker container, verify by grepping IP Address of the container. It should work as expected.

The 172.18.x.x network (ifconfig showed with name br############) seemed to be created when I start the docker-compose.

And this network will be deleted when I run docker-compose down.

Before it being deleted and recreated, the IP address range will not change.

Once it recreates, the default-address-pools options is activated, and the new IP range is up.


My work around solution

Step1:

Add the following to /etc/docker/daemon.json.

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

Step2:

Restart docker service.

sudo systemctl restart docker

Step3:

Rebuild the docker-compose.

!!DANGER!! Before you do this, ensure that removing the container then recreate is acceptable.

docker-compose down
docker-compose up -d
Related