Service Discovery solution for Docker containers

Building a Service Discovery solution across datacenters is no easy job. The guys at HashiCorp have done a fantastic job at building one. is a distributed and highly available with multi-datacenter support out of box. Jeff Lindsay wrote a great article that introduces why Consul is the solution to the biggest part of the puzzle when building service discovery.

In this post, I am just gonna walk you through a simple example of how to setup Consul along with auto-registered services for docker containers. Let's assume you have docker installed in your VM, and you have a private IP address for your VM. In this case, our private IP address is We are using Jeff's provided docker containers for Consul and Registrator.

  • docker run --name consul \ -h $HOSTNAME \ -p \ -p \ -p \ -p \ -p \ -p \ -p \ -p \ -d progrium/consul -server -advertise \ -bootstrap-expect 1 This command could easibly be genereated by docker run --rm progrium/consul cmd:run -d and changing -bootstrap-expect to 1
  • Now that we have a Consul server running, let's verify the status. curl localhost:8500/v1/catalog/nodes or if you have consul binary installed, try running consul members -rpc-addr=
  • Now let's setup the registrator listener container. docker run -d -v /var/run/docker.sock:/tmp/docker.sock -h $HOSTNAME progrium/registrator consul:// We now have a registrator container listening for any new containers we are creating.
  • If you browse to, you should see your Consul Dashboard.
  • Create a simple index.html in your home directory under static-test folder, and expose that through a nginx container. docker run -v /home/ubuntu/static-test:/home/app -p 9998:80 -d aminjam/nginx-static
  • Refresh the Consul Dashboard and you should see the nginx-static container was successfully registered with Consul.

In this exercise, we were quickly able to build a Service Discovery on top of our current infrastructure. In the later posts, I will go through how you can build more complex scenarios.

comments powered by Disqus