Enabling host.docker.internal in linux

First, a bit of context: I am the only person on my team which does not run docker desktop locally, all other members are either macOs or windows users. Therefore we need our docker containers to work on all platforms. Our docker containers reach out to the machine local network on host.docker.internal, which does not work on my system. A common approach is to use the extra_hosts feature. However, that would imply modifying all existing compose files and having to be mindful in the future. It would be better if no extra steps are needed.

Looking into docker desktop networking documentation, we see that on docker desktop the DNS name host.docker.internal resolves to the host docker internal ip address.

From the docker manuals we can also find out that the docker host internal ip is found in the bridge network. So it can be obtained with:

docker network inspect bridge

Usually that IP is 172.17.0.1. Now just need to route all the traffic to host.docker.internal to the brigde network IP. This can be done by adding the following rule to the /etc/hosts file:

172.17.0.1 host.docker.internal

From now all containers reaching to host.docker.internal will be redirected to the default bridge network, emulating the behavior on docker desktop. No need to modify any docker docker compose files or replace host.docker.internal witth any ip.