Access to container by his hostname from host-machine

和自甴很熟 提交于 2019-11-28 12:21:02

问题


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).

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


回答1:


You can simply add 127.0.0.1 <hostname_inside_docker> to your hosts file (on your local machine




回答2:


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

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.

Now if you launch a docker container

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

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 is the default domain name. You can change this using environment variables. For more details refer to https://github.com/ruudud/devdns



来源:https://stackoverflow.com/questions/45898458/access-to-container-by-his-hostname-from-host-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!