đŸŗ Docker
🏗ī¸ Building the cloudflare-ddns Docker image

Last updated: August 10th, 2020

🏗ī¸ Building the cloudflare-ddns Docker image

Background

I maintain a free and open source solution for updating DNS on Cloudflare. The solution is based on crontab, a software utility, to repeatedly run a python3 script based on a config file. It is simple, and educational for first time programmers to setup on a cheap hosting solution, like a Raspberry Pi Zero.

This is an article I wrote to document my experiences writing a docker container for this small Python project. The source code for the project can be found here (opens in a new tab).

On a recent project, I deployed a HIPPA compliant API backend to an on premises environment. Docker minimized setup time of the production environment & allowed us to create redundancy for regular updates to minimize down time. For my software engineering LLC, I found some cost & privacy advantages in self hosting web apps for managing office file transfers operations and video conferencing.

Docker support solves a lot of problems for running multiple instances of a program on the same machine. Container solutions sandbox the environment your app runs inside. Besides enhancing security in production by tightly controlling file system and network access, it simplifies deployment.

Implementing best practices for building docker images is non-trivial. However, a template containing a multi-stage build support based on Alpine Linux and the LTS-tagged docker image of your runtime environment can help simplify deployment.

Docker containers

Docker containers build upon cgroups, kernel namespaces, and UnionFS, resource isolation features of the Linux kernel. Platform specific implementation by platform may vary, but it still provides a unified layer for deploying platforms to support an application. Docker containers are light & avoid the overhead of virtual machines. Docker allows for multiple containers to run on the same instance.

Docker is the base layer for a number of compute scaling technologies, such as Docker Swarm, Docker Compose, Kubernetes, and Open Shift.

Multi-stage build

There has been significant advancements in the Linux distro space to minimize platform size. The industry leader in the smallest Linux distro, while maintaining the minimum required feature set for implementing best practices in Alpine Linux.

At 5.6mb for the base image, Alpine Linux adds very minimal bulk to the application package. The base Python3 environment clocked in at 29.3mb. For installing python dependencies, pip was used, which added 7.2mb of bulk to the build. 8.4mb for requests. 4.2kb for the code inside cloudflare-ddns.py.

Python

As python is an interpreted language, it doesn't have a strict development/production library folder. No need to copy production dependencies from a separate dependency folder like my TypeScript + Docker template (opens in a new tab).

I am using the latest Python3 syntax & best practices.

Docker Compose

There are many languages for writing compute infrastructure as code, but Docker Compose is the simplest to maintain. There are two main commands to remember for controlling Docker Compose.

To create and start the docker container:

docker-compose up -d

And to uninstall your docker container from the runtime environment:

docker-compose down

All docker compose syntax for setting up this container has been covered, but there is much more syntax to learn on the official Docker docs (opens in a new tab).

The official docker image can be found here (opens in a new tab).

Where to go from Here

Instructions for setting up this project can be found on the project Github page as well as the DockerHub README. If you haven't already, give my past article on creating the DDNS client using Python a read.

Summary

Container technologies like Docker & infrastructure as code languages like Docker Compose are going to continue thriving because they solve a set of common use cases like platform security, application size, and consistency. I look forward to the next advances in open source software.