正如标题所示,我需要能够检索docker托管的IP地址以及从主机到容器的portmap,并在容器内部完成这些工作。


当前回答

docker network inspect bridge -f '{{range .IPAM.Config}}{{.Gateway}}{{end}}'

可以使用docker网络inspect来检索它

其他回答

我是这样做的。在这种情况下,它在docker镜像的/etc/hosts中添加了一个hosts条目,将taurus-host指向我的本地机器IP::

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

然后,从Docker容器中,脚本可以使用主机名taurus-host访问到承载Docker容器的本地机器。

我的解决方案:

Docker运行——net=host

然后在docker容器中:

hostname -I | awk '{打印$1}'

如果您在Service Fabric集群上运行Windows容器,则主机的IP地址可以通过环境变量Fabric_NodeIPOrFQDN获得。服务Fabric环境变量

docker network inspect bridge -f '{{range .IPAM.Config}}{{.Gateway}}{{end}}'

可以使用docker网络inspect来检索它

所以…如果你使用Rancher服务器运行你的容器,Rancher v1.6(不确定2.0是否有)容器可以访问http://rancher-metadata/,其中有很多有用的信息。

从容器内部可以在这里找到IP地址: curl http://rancher-metadata/latest/self/host/agent_ip

详情见: https://rancher.com/docs/rancher/v1.6/en/rancher-services/metadata-service/