How to get the IP address of the docker host from inside a docker container

后端 未结 24 1766
鱼传尺愫
鱼传尺愫 2020-11-22 06:41

As the title says. I need to be able to retrieve the IP address the docker hosts and the portmaps from the host to the container, and doing that inside of the container.

24条回答
  •  春和景丽
    2020-11-22 07:28

    TLDR for Mac and Windows

    docker run -it --rm alpine nslookup host.docker.internal
    

    ... prints the host's IP address ...

    nslookup: can't resolve '(null)': Name does not resolve
    
    Name:      host.docker.internal
    Address 1: 192.168.65.2
    

    Details

    On Mac and Windows, you can use the special DNS name host.docker.internal.

    The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is 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 purpose and will not work in a production environment outside of Docker Desktop for Mac.

提交回复
热议问题