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

前端 未结 9 1140
面向向阳花
面向向阳花 2020-12-02 09:37

On Mac and Windows it is possible to use docker.for.mac.host.internal (replaces docker.for.mac.localhost) and docker.for.win.host.int

相关标签:
9条回答
  • 2020-12-02 10:11

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

    0 讨论(0)
  • 2020-12-02 10:15

    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
    
    
    0 讨论(0)
  • 2020-12-02 10:18

    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}
    
    0 讨论(0)
  • 2020-12-02 10:23

    For linux, I was able to use the service name I was trying to connect to, e.g. one of my containers (php-fpm) was trying to connect to mysql, so I used mysql as the host name, since that's the service name in my docker-compose

    0 讨论(0)
  • 2020-12-02 10:24

    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.

    0 讨论(0)
  • 2020-12-02 10:28

    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

    0 讨论(0)
提交回复
热议问题