How to ping a private host within a private network by hostname and NOT private IP address

Posted October 7, 2016 10.7k views
DockerUbuntu 16.04


I have created a Docker Swarm (private network) - complete with H/A & Im at the stage of setting up TLS....however the nodes must recognise each other from hostname and not ipaddress.

When I try to ping either of the 7 nodes within the private network using the hostname, it does not work, however I can ping with the private ip address.

I did try to set up a DNS - however this did not resolve my issue.

Thank you and look forward to any suggestions that may lead me to a solution.


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
4 answers

What about using hosts file?
hosts will allow you to resolve host names to IP addresses.

Open hosts file with text editor of your choice:

  • sudo nano /etc/hosts

There will probably be already some lines it.
Add new line for example:

...    sammy2

Now if you ping sammy2 it should ping
If nothing happens try restarting your Droplet, but this should not be a case.


Super suggestion, however, this was tried and failed also for me. I navigated to the sudo vim /etc/hosts file on one of my droplets and added all other 6 nodes in my swarm cluster (within the private network) and pinged the hostname associated with the node with no sucess, but all pings were successful when I pinged the actually address’s.....

  • I really can’t think of why it doesn’t work. Usually hosts file worked excellent for me.
    Maybe you can try something like:

    /etc/hosts sammy2.localdomain sammy2

    Try now pinging sammy2.localdomain or only sammy2.

I have used BIND as DNS to do what you are talking about, but it did not work every time, and my knowledge was not sufficient to trouble-shoot to find out why it failed sometimes. I was also trying to use it in conjunction with OpenVPN, so there were other variables at play.

Another method that will work, but might not be the best of ways…but will work..

Purchase a domain. get a cheap domain, like .space or .biz

create a subdomain for each node or droplet with the private ip address

once those subdomains propagate, then your nodes should be able to ping each other using your subdomains.

I have done this before and it works…but bear in mind it will only work within the network cluster or whatever it is you are doing…external computers that are not on that network will get a “Cannot connect to host.”

@sierracircle this is definitely the easiest/dirtiest solution. Problem is that propagation could take a while. New deployments and changes could take a while. But all the other options look exceedingly difficult for a small shop to implement. I may do this option and see how well I like it.