docker容器如何解析在同一台机器上运行的其他docker容器的主机名? [英] How do docker containers resolve hostname of other docker containers running on the same machine?

查看:2141
本文介绍了docker容器如何解析在同一台机器上运行的其他docker容器的主机名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用码头和喜欢它,主要是因为Docker容器是一种轻量级的虚拟机。但是我无法弄清楚码头集装箱如何能够解决每个人的主机名。他们可以使用IP连接到彼此,但不使用他们的主机名,我甚至不能编辑容器中的/ etc / hosts来弥补这一点。当我重新启动容器时,它们会获得不同的IP,因此我想使用主机名代替IP进行通信。让我们说,我想在容器中运行Zookeeper集群的Zookeeper实例,我想把Zookeeper服务器的主机名放在config(zoo.cfg)文件中。

I have started to use docker and liking it mostly because Docker containers are kind of light-weight VMs. But I am unable to figure out, how docker containers may be able resolve each-other's hostnames. They can connect to each other using there IPs, but not using their hostnames, I cannot even edit /etc/hosts in the containers to make up for that somehow. When I restart the containers, they get different IPs and hence I want to use the hostnames in place of IPs to communicate with each other. Let us say, I want to run Zookeeper instances of a Zookeeper cluster in the containers and I want to put the hostnames of the Zookeeper servers in the config (zoo.cfg) files.

推荐答案

可能需要检查Docker链接( https://docs.docker.com/userguide/dockerlinks/ )。链接到正在运行的容器时,将为要连接到的容器添加主机条目。

It may be worth checking out Docker links (https://docs.docker.com/userguide/dockerlinks/). When you link to a running container, a host entry is added for the container you wish to connect to.

在他们的示例中,他们显示

In their example they show

$ sudo docker run -t -i --rm --link db:db training/webapp /bin/bash
root@aed84ee21bde:/opt/webapp# cat /etc/hosts
172.17.0.7  aed84ee21bde
. . .
172.17.0.5  db

如你所见,他们链接他们所在的应用程序bash与名为 db 的容器,然后添加一个主机条目 db 与该容器的IP地址

As you see here, they link the application they're in bash with to the container named db, and subsequently a host entry is added for db with the IP address of that container.

所以在运行zookeeper的情况下,您可以简单地使容器开始只是链接到zookeeper。我希望这有帮助!

So in the instance of having zookeeper running, you could simply make the containers you start just link to zookeeper. I hope this helps!

这篇关于docker容器如何解析在同一台机器上运行的其他docker容器的主机名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆