简体   繁体   中英

What is linux equivalent of “host.docker.internal”

On Mac and Windows it is possible to use docker.for.mac.host.internal (replaces docker.for.mac.localhost ) and docker.for.win.host.internal (replaces docker.for.win.localhost ) 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?

Depends what you're trying to do. If you're running with --net=host , localhost should work fine. If you're using default networking, use the static IP 172.17.0.1 . I suspect neither will behave quite the same as those domains.

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

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

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:

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

Docs - https://docs.docker.com/compose/compose-file/compose-file-v3/#extra_hosts

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

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> ...

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

Using the docker0 interface ip, say 172.17.0.1, could be a good workaround.

Just be sure that the service you need to reach listens to external connections. A typical example is Mysql who binds to 127.0.0.1 by default, resulting unreachable until you allow external connections (es. binding to 0.0.0.0)

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.

  • For allowing internal HTTP requests in Linux, use the static IP 172.17.0.1

  • 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

  • Now change the request URL with this IP

req = requests.get('http://172.17.0.1:8000/api/YOUR_ENDPOINT')

对于 linux,我可以使用我尝试连接的服务名称,例如我的一个容器 (php-fpm) 尝试连接到 mysql,所以我使用mysql作为主机名,因为这是服务名称我的 docker-compose

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM