Solution:
- Docker desktop (tested on Macos): use special DNS name
host.docker.internal
- Linux:
- use
--network host
ondocker run
, and uselocalhost
. This has same network namespace (better performance but less security) - or
--add-host=host.docker.internal:host-gateway
, so that the containers using NAT can resolve host IP by domain name ofhost.docker.internal
Use case: docker compose file:
version: 'x.x'
services:
xxx:
build:
context: .
extra_hosts:
- "host.docker.internal:host-gateway"
Ref: