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

后端 未结 24 1771
鱼传尺愫
鱼传尺愫 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:26

    AFAIK, in the case of Docker for Linux (standard distribution), the IP address of the host will always be 172.17.0.1 (on the main network of docker, see comments to learn more).

    The easiest way to get it is via ifconfig (interface docker0) from the host:

    ifconfig
    

    From inside a docker, the following command from a docker: ip -4 route show default | cut -d" " -f3

    You can run it quickly in a docker with the following command line:

    # 1. Run an ubuntu docker
    # 2. Updates dependencies (quietly)
    # 3. Install ip package   (quietly)
    # 4. Shows (nicely) the ip of the host
    # 5. Removes the docker (thanks to `--rm` arg)
    docker run -it --rm ubuntu:19.10 bash -c "apt-get update && apt-get install iproute2 -y && ip -4 route show default | cut -d' ' -f3"
    

提交回复
热议问题