Access to container by his hostname from host-machine

后端 未结 2 1171
慢半拍i
慢半拍i 2020-12-22 03:46

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 contai

相关标签:
2条回答
  • 2020-12-22 04:26

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

    0 讨论(0)
  • 2020-12-22 04:27

    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

    0 讨论(0)
提交回复
热议问题