Table of Contents

,

Access host services from docker without using 'host' network mode

If you need access for example to a DB running on host from docker container while still using the bridge (default) network mode, start the container with following flag:

In docker run:

docker run --add-host host.docker.internal:host-gateway  

In docker-compose.yml:

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

This adds an /etc/hosts entry to the correct IP address for the host's gateway.

root@a499b6825b0d:/data# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::  ip6-localnet
ff00::  ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.1      host.docker.internal
172.17.0.2      a499b6825b0d

Tested on

See also

References