简体   繁体   English

如何从docker容器中获取外部主机ip地址

[英]How do I get the external hosts ip address from inside a docker container

We are transitioning (we hope) from CoreOS to CentOS, from fleet to swarm. 我们正在从CoreOS到CentOS(从船队到群体)过渡(我们希望)。 I need to determine the ip address of the machine running docker from inside the container. 我需要从容器内部确定运行docker的机器的ip地址。

The problem is that we use nginx to determine which of the machines in our docker cluster runs which service. 问题是我们使用nginx来确定docker集群中的哪些机器运行哪个服务。 To make this work we need the container to be able to post to our etcd repository the ip address of the machine upon which it is located. 为了完成这项工作,我们需要容器能够向我们的etcd存储库发送它所在的机器的IP地址。 Everything I have seen so far has been able to get me to a 172.17.0.1 ip address for the external machine, but ALL of our containers on ALL of our dockers will have that private address. 到目前为止我看到的所有内容都能够让我获得外部机器的172.17.0.1 ip地址,但我们所有码头工人的所有容器都将拥有该私人地址。 I need an EXTERNAL address that nginx may use to get to the service. 我需要一个外部地址,nginx可以使用它来获取服务。

I could use the '--hostname ip ... ' option or the '-e EXT_HOST_IP= ip ... ' option to set an ip address, but if I include these in the 'docker run' command, the shell processing the docker command will expand the 'ip...' and return the ip address of the current machine -- NOT the machine upon which swarm will eventually run the container. 我可以使用'--hostname ip ... '选项或'-e EXT_HOST_IP = ip ... '选项来设置IP地址,但是如果我在'docker run'命令中包含这些,那么shell处理docker命令将展开'ip ...'并返回当前机器的ip地址 - 而不是swarm最终运行容器的机器。

The best I have come up with so far is to create a file/directory on the host machine that contains the ip address of the host machine. 到目前为止,我提出的最好的方法是在主机上创建一个包含主机IP地址的文件/目录。 I can then use the docker '-v' option to mount the directory inside the container, and get the ip address from that. 然后我可以使用docker'-v'选项将目录挂载到容器中,并从中获取ip地址。 It just seems like there should be an easier way to do this. 看起来应该有一种更简单的方法来做到这一点。

Swarm issue 1106 mentions in a recent answer Swarm问题1106最近的回答中提及

inside swarm I can get the ip of the host machine like so 在swarm中我可以像这样得到主机的ip

ip route | awk '/default/ { print $3 }' 

Which is fine for many purposes. 这对于许多目的来说都很好。

But when I talk to that IP I need to use the proper dns name for TLS to work. 但是,当我与该IP通信时,我需要使用适当的DNS名称来使用TLS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 nginx 将地址重定向到另一个 docker 容器? - How do I redirect an address to another docker container using the nginx? Dokku-获取容器内Nginx反向代理的IP地址? - Dokku - get ip address of nginx reverse proxy inside container? Docker 与 Symfony 和 Nginx 如何获得真实的 IP 地址? - Docker with Symfony an Nginx how to get real ip address? 如何从服务器的公共IP访问Docker容器 - How to access a docker container from the public ip of a server 如何通过托管多个网站的服务器中的IP地址访问网站? - How to access a website by IP address in a server that hosts many websites? 如何在 Django 通道中获取 websocket 连接的客户端 IP 地址? - How do I get the client IP address of a websocket connection in Django Channels? 无法从外部 IP 地址访问 Tomcat - Cannot access Tomcat from external IP address 如何在 Docker Compose 中将目录从一个容器复制到另一个容器? - How do I copy a directory from one container to another container in Docker Compose? ReactPHP和Docker:无法将主机从php解析为nginx容器 - ReactPHP and docker: could not resolve hosts from php to nginx container 将我的Docker容器暴露于GCP上的外部IP - Expose my docker container to my external IP on GCP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM