Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

docker - Access to container by his hostname from host-machine

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...