Saturday, October 4, 2014

DDNS with Docker and nsupdate

Many robust and scalable solutions exist for monitoring docker containers and publish their IP addresses into some service discovery solution.  SkyDNS or consul.io are such examples.  However I wanted a simpler solution for the few containers I run on my home network server, so I put together the simplest service discovery mechanism possible.

Place the following file in /etc/cron.daily - this has been tested on Ubuntu 14.04:
#!/bin/bash

CONTAINERS=($(docker ps -q))    # fetch all containers
DOMAIN="example.com"
KEY="/etc/bind/rndc.key"
TTL="86400"                     # 1 day
TMPFILE="/tmp/dns_updates"

# prepare file
if [ -f $TMPFILE ]; then
  rm $TMPFILE
fi

for CONTAINER in ${CONTAINERS[@]}; do
  NAME=`docker inspect -f '{{.Name}}' $CONTAINER`
  NAME=${NAME#/} # remove leading / from container name
  IP=`docker inspect -f '{{.NetworkSettings.IPAddress}}' $CONTAINER`
  echo "update add $NAME.$DOMAIN $TTL A $IP" >> $TMPFILE
done
echo "send" >> $TMPFILE

nsupdate -k $KEY $TMPFILE
exit 0

The script be executed daily. It fetches all containers from docker and publishes their name+domain with their ip address to bind9 DNS server.

If start new containers, you can manually run the script again to update DNS.

Note: this script does not remove DNS names when you stop containers.

4 comments:

  1. Good article. Thank you for sharing it.

    ReplyDelete
  2. Thank you For The Articles.. Good One...
    Devops

    ReplyDelete
  3. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.

    IOT Training in Bangalore - Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.

    ReplyDelete
  4. Hadoop concepts, Applying modelling through R programming using Machine learning algorithms and illustrate impeccable Data Visualization by leveraging on 'R' capabilities.With companies across industries striving to bring their research and analysis (R&A) departments up to speed, the demand for qualified data scientists is rising.
    data science training in bangalore
    Big Data and Hadoop training Unlike traditional systems, Big Data and Hadoop enables multiple types of analytic workloads to run on the same data, at the same time, at massive scale on industry-standard hardware.myTectra Big Data and Hadoop training is designed to help you become a expert Hadoop developer. myTectra offers Big Data Hadoop Training in Bangalore using Class Room.
    hadoop training in bangalore
    Looking for best Machine Learning Training in Bangalore then join myTectra the leader in Machine Learning Training in Bangalore. Classroom & Online Training
    machine learning training in bangalore

    ReplyDelete