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

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

    For those running Docker in AWS, the instance meta-data for the host is still available from inside the container.

    curl http://169.254.169.254/latest/meta-data/local-ipv4
    

    For example:

    $ docker run alpine /bin/sh -c "apk update ; apk add curl ; curl -s http://169.254.169.254/latest/meta-data/local-ipv4 ; echo"
    fetch http://dl-cdn.alpinelinux.org/alpine/v3.3/main/x86_64/APKINDEX.tar.gz
    fetch http://dl-cdn.alpinelinux.org/alpine/v3.3/community/x86_64/APKINDEX.tar.gz
    v3.3.1-119-gb247c0a [http://dl-cdn.alpinelinux.org/alpine/v3.3/main]
    v3.3.1-59-g48b0368 [http://dl-cdn.alpinelinux.org/alpine/v3.3/community]
    OK: 5855 distinct packages available
    (1/4) Installing openssl (1.0.2g-r0)
    (2/4) Installing ca-certificates (20160104-r2)
    (3/4) Installing libssh2 (1.6.0-r1)
    (4/4) Installing curl (7.47.0-r0)
    Executing busybox-1.24.1-r7.trigger
    Executing ca-certificates-20160104-r2.trigger
    OK: 7 MiB in 15 packages
    172.31.27.238
    
    $ ifconfig eth0 | grep -oP 'inet addr:\K\S+'
    172.31.27.238
    

提交回复
热议问题