How to get the IP address of the docker host from

2019-01-01 11:42发布

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.

标签: docker ip
15条回答
其实,你不懂
2楼-- · 2019-01-01 12:17

If you are running a Windows container on a Service Fabric cluster, the host's IP address is available via the environment variable Fabric_NodeIPOrFQDN. Service Fabric environment variables

查看更多
牵手、夕阳
3楼-- · 2019-01-01 12:18

Here is another option for those running Docker in AWS. This option avoids having using apk to add the curl package and saves the precious 7mb of space. Use the built-in wget (part of the monolithic BusyBox binary):

wget -q -O - http://169.254.169.254/latest/meta-data/local-ipv4
查看更多
栀子花@的思念
4楼-- · 2019-01-01 12:20

Here is how I do it. In this case, it adds a hosts entry into /etc/hosts within the docker image pointing taurus-host to my local machine IP: :

TAURUS_HOST=`ipconfig getifaddr en0`
docker run -it --rm -e MY_ENVIRONMENT='local' --add-host "taurus-host:${TAURUS_HOST}" ...

Then, from within Docker container, script can use host name taurus-host to get out to my local machine which hosts the docker container.

查看更多
墨雨无痕
5楼-- · 2019-01-01 12:21
/sbin/ip route|awk '/default/ { print $3 }'

As @MichaelNeale noticed, there is no sense to use this method in Dockerfile (except when we need this IP during build time only), because this IP will be hardcoded during build time.

查看更多
长期被迫恋爱
6楼-- · 2019-01-01 12:22

Maybe the container I've created is useful as well https://github.com/qoomon/docker-host

You can simply use container name dns to access host system e.g. curl http://dockerhost:9200, so no need to hassle with any IP address.

查看更多
倾城一夜雪
7楼-- · 2019-01-01 12:24

In linux you can run

HOST_IP=`hostname -I | awk '{print $1}'`

In macOS your host machine is not the Docker host. Docker will install it's host OS in VirtualBox.

HOST_IP=`docker run busybox ping -c 1 docker.for.mac.localhost | awk 'FNR==2 {print $4}' | sed s'/.$//'`
查看更多
登录 后发表回答