I have been designing and implementing various Docker-based microservices platforms for the last couple of years. One of the must-have capabilities of such platforms is service discovery. In previous set of articles I demonstrated an implementation based on such open source components like: etcd, skydns plus custom DNS-automation service built with Node.js and TypeScript. Recently, I discovered that starting from version 1.10, Docker has built-in DNS server that can be used for the same purpose. Only implementation would be much simpler.

I have already migrated some of my environments to use this solution and quite happy with the results. Today I will share my findings with you.

Microservice discovery solution overview

In result of this little exercise we will achieve pretty much the same what we achieved in the original series of 3 articles:

  • automated DNS entry management based on Docker container name
  • calling one service from another using service name as short host name
  • configuring Nginx to act as a simple reverse proxy (API gateway) for Dockerized microservices

Implemented service discovery is limited to a single host. If you want to expand your platform beyond that, you will need to setup network overlay. It will create single virtual network for Docker containers running on multiple hosts, letting them to talk to each other.

Microservices discovery with Docker built-in DNS and Nginx running in a container

Microservices discovery with Docker built-in DNS and Nginx running in a container

Enabling Docker built-in DNS

First, we need to create our custom network in Docker – this is a requirement for built-in DNS

Now, when we start our microservices in containers we need to use network isolated – we have just created it.

Similarly, how we did in our pervious articles, we will start 2 instances of a simple dockerized Web service – web-service-dockerized-example.


Then we will access them using curl through short host names.

Note: both, client and server have to run in Docker containers using our custom network.

We run curl command inside docker container to make services discoverable with Docker built-in DNS.

Setting up nginx as a reverse-proxy with DNS-based discovery

For our microservices to be useful, they must be accessible for external clients. This can be achieved thorough API Gateway pattern. In our example we will use Nginx configured as reverse proxy with DNS resolver set to Docker internal DNS (it’s available via IP inside Docker container).

Here is a minimal nginx.conf file satisfying our requirements. It will route all incoming HTTP requests to existing Docker containers using simple rewrite rule

http://host/api/[container-name]/query => http://[container-name]:3000/query

we will save this file to /etc/nginx/nginx.conf  on host file system

Then, we can start nginx using official Nginx docker image from Docker Hub

As expected, it will make our services accessible from outside Docker isolated network

DNS load balancing

I decided to add this section in response to the received question about failover configuration. When I initially tried to implement it with Docker 1.10 it didn’t quite work. Despite 2 instances of the service available, all requests would come to the first one. I suppose we can call it failover – having 2 instances, in normal state. First one will serve all requests and second will be idle. Then if active instance goes down for some reason, second instance will start serving requests. However my goal was to achieve load balancing – when all running instances will receive a portion of traffic.

After some additional research, I found that in Docker 1.11 this issue was resolved. Here’s how DNS load balancing can be implemented.

Make note of how we use  --net-alias service parameter to define service name.

Now we can access our services on load balanced endpoint service

as well as on individual endpoints for each service: service1 and service2

In this article we achieved almost all capabilities that perviously required installation of 3 additional components: etcd, skydns and our custom automation service.

If you want to know more about this subject, or want to share your experience, please leave a message below. I love your feedback!

Share This

Share This

Share this post with your friends!