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.
**I want to connect from a container to a service on the host.**
The host has a changing IP address, or none if there is no network access. It is recommended to connect to the special DNS name `host.docker.internal`, which resolves to the internal IP address used by the host. This is for development and does not work in production environments outside Docker Desktop.
You can also use `gateway.docker.internal` to connect to the gateway.
**Source**: [Official Docker documentation](https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host)
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