Archive

Archive for september, 2015

Docker Service Discovery (aka resolv the IP’s of your running containers)

september 30th, 2015 No comments

Note: In this blogpost I expect that you are aware of docker, it’s basics and how to use it. Also, don’t blame me if this doesn’t work, cause PEBKAC

I’ve been experimenting with docker for the last year in a hobby project, which is running a small Minecraft server network for a Youtuber called Xisumavoid

docker came to my attention some time last year (2014), heard people talking and praising it, so I checked it out, I’ve been hooked ever since.

One feature of docker is a lot of fun, the –link feature, this puts the hostname of the certain container to it’s proper IP.
However, this doesn’t update when the container you linked to restarts.. which is annoying.. also your container doesn’t start when that container isn’t up.

Also, if you want to connect to the containers from your host, or another host, say through a different docker container on a different host, yeah.. issues..

What I have found to work is this combination:

It’s a strange combination, but it works for me just fine.

This is how it works:

dnsdock is connected to the docker API, using the socket, Yes there is some security related discussion that discourages this practice… but for this one app, which is open source.. and has no direct incoming connection to the outside world.. I don’t worry that much.

The powerdns part, is basically a recursive dns server, which will combine the queries for all the docker instances, powerdns is quiet the powerfull dns server.. so that way I’m sure resolving doesn’t crash that easy.

Now, how do i make all of this work, like this:


#!/bin/bash
docker pull tonistiigi/dnsdock
docker pull urelx/pdns-recursor

docker run -d -v /var/run/docker.sock:/var/run/docker.sock --name dnsdock -p 172.17.42.1:5300:53/udp tonistiigi/dnsdock -environment="servername"
docker run -d --name powerdns \
-p 172.17.42.1:53:53 -p 172.17.42.1:53:53/udp \
urelx/pdns-recursor --forward-zones="servername.docker=172.17.42.1:5300, otherservername.docker=172.17.42.2:5300" --forward-zones-recurse=".=8.8.8.8"

In this example I also added a 2nd baremetal server, with ip 172.17.42.2

I’m aware this is not the most advanced thing, and could be expanded in lots of ways, but this just a simple proof of concept that does the basics you need in order to resolv containers.
More info you’ll find on the dnsdock repo.

However, how does this basically work

the hostname looks something like this:
containername.imagename.environment.docker.

so if you run a mysql server, name mysql1 (assuming you use the official mysql image) it looks like this:
mysql1.mysql.servername.docker.

Interesting fact, if you have mysql1, mysql2, etc, you can dns loadbalance:
mysql.servername.docker. will give back all the ip’s of the running containers, using that image.