Skip to content

Instantly share code, notes, and snippets.

@levihuayuzhang
Last active November 2, 2023 07:42
Show Gist options
  • Save levihuayuzhang/53392c27912b76d6d5ceb8c7d2887dce to your computer and use it in GitHub Desktop.
Save levihuayuzhang/53392c27912b76d6d5ceb8c7d2887dce to your computer and use it in GitHub Desktop.
Docker access host IP by resolving specific domain name

Solution:

  1. Docker desktop (tested on Macos): use special DNS name host.docker.internal
  2. Linux:
  • use --network host on docker run, and use localhost. 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 of host.docker.internal

Use case: docker compose file:

version: 'x.x'

services:

  xxx:
    build:
      context: .
    extra_hosts:
      - "host.docker.internal:host-gateway"

Ref:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment