通过主机名按其主机名访问容器 [英] Access to container by his hostname from host-machine

查看:274
本文介绍了通过主机名按其主机名访问容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些docker容器,这些容器将单个dockers覆盖网络结合在一起. 在此网络下,每个容器都按(容器的)主机名访问.但是我无法从host-mashine(我的真实主机)按主机名访问容器.

I have some docker containers which united single dockers overlay network. Under this network every containers access by hostname (of container). But I cant access to container by hostname from host-mashine (my real host).

如何从真实机器上按docker容器主机名访问容器?

How I can get access to container by docker container hostname from my real machine?

推荐答案

您可以通过启动自己的DNS解析器容器来实现.

You can do that by launching your own DNS resolver container.

docker run -d --name devdns -p 53:53/udp \
  -v /var/run/docker.sock:/var/run/docker.sock ruudud/devdns

一旦运行了DNS服务器.服务器已映射到您的本地主机.在Linux上,您可以编辑/etc/resolv.conf并在顶部添加nameserver 127.0.0.1.重新启动后,此更改将恢复.

Once you have run the DNS server. The server is mapped to your localhost. On linux you can edit /etc/resolv.conf and add nameserver 127.0.0.1 at the top. This change will be reverted after reboot.

现在,如果您启动一个Docker容器

Now if you launch a docker container

docker run -d --hostname tarunlalwani --name tlalwani ubuntu:16.04 sleep 2000

现在您可以使用容器名称或主机名ping容器

Now you can ping the container using either the container name or the hostname

$ ping tlalwani.dev
PING tlalwani.dev (172.17.0.6) 56(84) bytes of data.
64 bytes from 172.17.0.6: icmp_seq=1 ttl=64 time=0.030 ms

$ ping tarunlalwani.dev
PING tarunlalwani.dev (172.17.0.1) 56(84) bytes of data.
64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.026 ms

dev是默认域名.您可以使用环境变量来更改它.有关更多详细信息,请参考 https://github.com/ruudud/devdns

dev is the default domain name. You can change this using environment variables. For more details refer to https://github.com/ruudud/devdns

这篇关于通过主机名按其主机名访问容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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