Docker无法解析主机名 [英] Docker doesn't resolve hostname
问题描述
我需要知道在同一台计算机上运行的某些容器的主机名(或IP地址)。
正如我已经评论的那样在这里(但没有答案),我使用 docker-compose
。 文档说,compose会自动为在同一<$中定义的所有容器创建一个主机名条目。 c $ c> docker-compose.yml 文件:
I need to know the hostnames (or ip addresses) of some container running on the same machine.
As I already commented here (but with no answer yet), I use docker-compose
. The documentation says, compose will automatically create a hostname entry for all container defined in the same docker-compose.yml
file:
服务的每个容器都加入默认网络,既可以被该网络上的其他容器访问,也可以通过与容器名称相同的主机名被发现。
Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
但是我可以通过 docker exec -it my_container tail -20 / etc / hosts
看不到任何主机条目。
我还尝试将链接
添加到我的容器中,但未做任何更改。
But I can't see any host entry via docker exec -it my_container tail -20 /etc/hosts
.
I also tried to add links
to my container, but nothing changed.
推荐答案
Docker 1.10引入了一些新的联网功能其中包括完成主机查找的内部DNS服务器。
Docker 1.10 introduced some new networking features which include an internal DNS server where host lookups are done.
在默认网桥网络(docker0)上,查找继续通过 / etc / hosts
起作用, 。 /etc/resolv.conf
将指向您的主机解析器。
On the default bridge network (docker0), lookups continue to function via /etc/hosts
as they use to. /etc/resolv.conf
will point to your hosts resolvers.
在用户定义的网络上,Docker将使用内部DNS服务器。 /etc/resolv.conf
将具有Docker DNS服务器的内部IP地址。此设置允许桥接,自定义和覆盖网络以类似的方式工作。因此,群集上的覆盖网络将像本地网桥网络一样填充整个群集中的主机数据。
On a user defined network, Docker will use the internal DNS server. /etc/resolv.conf
will have an internal IP address for the Docker DNS server. This setup allows bridge, custom and overlay networks to work in a similar fashion. So an overlay network on swarm will populate host data from across the swarm like a local bridge network would.
传统设置得以保留,因此可以引入新的网络功能而不会影响现有设置。
The "legacy" setup was maintained so the new networking features could be introduced without impacting existing setups.
DNS解析器可以通过该服务的名称为docker compose服务提供IP。
The DNS resolver is able to provide IP's for a docker compose service via the name of that service.
例如,使用 web
和 db
服务已定义,并且 db
服务已缩放为 3
,所有 db
实例将解决:
For example, with a web
and db
service defined, and the db
service scaled to 3
, all db
instances will resolve:
$ docker-compose run --rm web nslookup db
Name: db
Address 1: 172.22.0.4 composenetworks_db_2.composenetworks_mynet
Address 2: 172.22.0.5 composenetworks_db_3.composenetworks_mynet
Address 3: 172.22.0.3 composenetworks_db_1.composenetworks_mynet
这篇关于Docker无法解析主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!