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. Consul.io 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 172.20.22.10. We are using Jeff's provided docker containers for Consul and Registrator.

  • docker run --name consul \ -h $HOSTNAME \ -p 172.20.22.10:8300:8300 \ -p 172.20.22.10:8301:8301 \ -p 172.20.22.10:8301:8301/udp \ -p 172.20.22.10:8302:8302 \ -p 172.20.22.10:8302:8302/udp \ -p 172.20.22.10:8400:8400 \ -p 172.20.22.10:8500:8500 \ -p 172.17.42.1:53:53/udp \ -d progrium/consul -server -advertise \ 172.20.22.10 -bootstrap-expect 1 This command could easibly be genereated by docker run --rm progrium/consul cmd:run 172.20.22.10 -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=172.20.22.10:8400
  • Now let's setup the registrator listener container. docker run -d -v /var/run/docker.sock:/tmp/docker.sock -h $HOSTNAME progrium/registrator consul://172.20.22.10:8500. We now have a registrator container listening for any new containers we are creating.
  • If you browse to http://172.20.22.10:8500/ui, 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 http://172.20.22.10:8500/ui 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