Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
From inside of a Docker container, how do I connect to the localhost of the machine?
(43 answers)
Closed
last year
.
On Mac and Windows it is possible to use
host.docker.internal
(Docker 18.03+) inside container.
Is there one for Linux that will work out of the box without passing env variables or extracting it using various CLI commands?
–
–
–
–
–
For linux systems, you can – starting from major version
20.04
of the docker engine – now also communicate with the host via
host.docker.internal
. This won't work
automatically
, but you need to provide the following run flag:
--add-host=host.docker.internal:host-gateway
See the answer here: https://stackoverflow.com/a/61424570/3757139
See also this answer below to add to a docker-compose file - https://stackoverflow.com/a/67158212/243392
–
–
–
–
If you are using Docker Compose
+ Linux
, you have to add it manually (at least for now). Use extra_hosts
on your docker-compose.yaml
file:
version: '3.7'
services:
build:
context: .
extra_hosts:
- "host.docker.internal:host-gateway"
Do not forget to update Docker since this only works with Docker v20.10+.
Source : https://github.com/docker/for-linux/issues/264#issuecomment-784985736
–
One solution is to use a special container which redirects traffic to the host. You can find such a container here: https://github.com/qoomon/docker-host. The idea is to grab the default route from within the container and install that as a NAT gateway for incoming connections.
An imaginary example usage:
docker-host:
image: qoomon/docker-host
cap_add: [ 'NET_ADMIN', 'NET_RAW' ]
restart: on-failure
environment:
- PORTS=999
some-service:
image: ...
environment:
SERVER_URL: "http://docker-host:999"
command: ...
depends_on:
- docker-host
This is my solution:
IP_ADDRESS=$(ip addr show | grep "\binet\b.*\bdocker0\b" | awk '{print $2}' | cut -d '/' -f 1)
then in docker-compose:
extra_hosts:
docker.host: ${IP_ADDRESS}
–
–
–
For linux there isn't a default DNS name for the host machine. This can be verified by running the command:
docker run -it alpine cat /etc/hosts
This feature has been requested, however wasn't implemented. You can check this issue. As discussed you can use the following command to find the IP of the host from the container.
netstat -nr | grep '^0\.0\.0\.0' | awk '{print $2}'
Alternatively, you can provide the host ip to the run command via docker run --add-host dockerHost:<ip-address> ...
–
–
Doing HTTP request towards the host:
Run the following command to get the static IP: ip addr show | grep "\binet\b.*\bdocker0\b" | awk '{print $2}' | cut -d '/' -f 1
Add the new IP to allowed hosts
Use the IP address just found in your requests: req = requests.get('http://172.17.0.1:8000/api/YOUR_ENDPOINT')
https://github.com/docker/for-linux/issues/264
IP=$(ip -4 route list match 0/0 | awk '{print $3}')
echo "Host ip is $IP"
echo "$IP host.docker.internal" | sudo tee -a /etc/hosts
It will add host.docker.internal
to your hosts. Then you can use it in xdebug config.
Here is example of env variable in docker-compose.yml
XDEBUG_CONFIG: remote_host=host.docker.internal remote_autostart=On remote_enable=On idekey=XDEBUG remote_log=/tmp/xdebug.log remote_port=9999
host.docker.internal exists only in Windows WSL because Docker Desktop for Windows runs Docker daemon inside the special WSL VM Docker-Desktop. It has its own localhost and its own WSL2 interface to communicate with Windows. This VM has no static IP. The IP is generated every time when VM is created and passed via host.docker.internal in the generated /etc/hosts to every distro. Although there is no bridge or real v-switch all ports opened on eth0 of VM internal network are mapped on the host Local network, BUT NOT ON THE ETH0 OF THE HOST.
There is no real bridge and port mapping - nothing to configure.
Inside WSL VM its Localhost is the same as the localhost of the Linux machine. 2 processes inside WSL VM can communicate via localhost. Cross-distro IPC must use host.docker.internal. It is possible to create bridge inside WSL VM -Docker does it.
–
–
–