Docker: Using the host's network

2 min read | by Jordi Prats

If we use the --net=host option with docker to create a new container, it will share it's network namespace with the host machine. It's main advantage is that it will provide higher performance (it will be close to bare metal speed); however, we might get port conflicts.

If we are not interested on it's higher performance, we might still need it if we are unable to use the docker's network due to some conflict with other networks we have:

$ docker run -it --rm mysql mysql -hdemo.pet2cattle.com
ERROR 2005 (HY000): Unknown MySQL server host 'demo.pet2cattle.com' (-3)

We can workaround it by skipping the docker network using the mentioned --net=host option:

$ docker run --net=host -it --rm mysql mysql -hdemo.pet2cattle.com

Since we are just connecting to a remote host we won't face any port conflicts. If we want to permanently fix this issue, we can also change the default bridge IP (bip) and the default-address-pools on the /etc/docker/daemon.json file

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

Posted on 24/02/2022

Categories