Can I get ip address inside my docker container?

后端 未结 9 1769
南旧
南旧 2020-12-28 12:10

How to get container ip address inside this container?

\'docker inspect $hostname ...\' not suitable, because I don\'t share /var/run/docker.sock host file to contai

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-28 12:46

    If you prefer to use ip rather than ifconfig, on Linux you can do:

    ip addr | grep inet | tr -s ' ' | cut -d ' ' -f 3
    

    This simply gets all of the IP addresses and interfaces, searches only for those starting with inet and returns only the 3rd column.

    As a nice side-effect, this includes the subnet mask, too! (e.g. 172.17.0.2/16)

    If you don't want the subnet mask, you can use:

    ip addr | grep inet | tr -s ' ' | cut -d ' ' -f 3 | tr -d '/'
    

    NOTE: This shows ALL of the IPs in the container. You can use awk or sed to remove 127.0.0.1/16 and other unwanted IPs. :)

提交回复
热议问题