Connecting from a Docker Container to the Host localhost

How to connect to a service on the host system’s localhost from inside a Docker container. host.docker.internal

Overview

This section introduces how to access the host from inside a Docker container.

Connection Method

Docker on Mac/Windows

On Mac or Windows, you can basically access the host from inside the container using the DNS name host.docker.internal.

Docker on Linux

To use host.docker.internal with Docker for Linux, add --add-host host.docker.internal:host-gateway.

For example:

$ docker run \
  -it \
  --rm \
  --add-host=host.docker.internal:host-gateway \
  alpine \
  cat /etc/hosts

References