docker,

Access host network from inside of a Docker container

Dec 11, 2021 · 1 min read · Post a comment
Access host network from inside of a Docker container

This might be a really weird or edge case scenario, but what If we need to access host service(s) from inside a Docker container. For instance, we already have configured and running a service on the dev host machine and kinda lazy to dockerize it.

Solution

Docker supports a host-gateway reference to the host’s gateway. This will allows us to map a host endpoint to the host-gateway.

From the CLI:

docker run --rm -it --add-host host.docker.internal:host-gateway alpine:latest

docker-compose:

services: 
  test_service:
    extra_hosts:
      - host.docker.internal:host-gateway

Conclusion

TL;DR add an additional host entry host.docker.internal:host-gateway.
Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on telegram.