New package: Native Handlebars for Alpine Linux

Published: by Creative Commons Licence

Configuring Docker Images

Docker images are great because they encapsulate services making them easy to distribute and deploy.

However these services require configuration information, in one way or another.

Docker makes it easy to pass variables, but normally those variables need to be translated into configuration files.

There isn't a clean solution for doing this, the typical options are:

  • Shell scripts / commands to build configuration files from environment variables. This ends up as spaghetti code, while the configuration options are limited.
  • Mount configuration files that are generated externally, using for example Ansible. This method is cleaner, and configuration files can be more complex. However by using this method docker images stop being so well encapsulated / portable.

Another approach

I think docker images should continue to be portable and well packaged. There should be a clean interface between your docker image and your automation tool, such as ansible.

The obvious solution is to use a lightweight templating system such as Handlerbars to generate the configuration files when the image is started.

The problem then is that most templating systems require their own stack: Java, Ruby, Python Javascript, etc. An Alpine Lunux image is currently 2Mb download, while any of this stacks will easily reach 100Mb. At first it seems like doing some shell scripting is not such a bad idea.

There is however an implementation of Handlerbars in C, which is also used for php-handlebars. It is robust and well developed/maintained.

We generated a package for Alpine Linux. The result is all the power of Handlerbars in just 125Kb.

Handlebards for Apline

The package is in the testing repository you can try it by doing:

  • Run your favorite Alpine container, for example:
$ docker run -it alpine-x86_64
  • Enable the testing repository
$ echo http://dl-cdn.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories
  • Install handlebars utilites
$ apk update
$ apk add handlebars-utils
  • And you are ready to test
$ handlebarsc
Usage: handlebarsc [--lex|--parse|--compile|--execute] [--data JSON_FILE] [TEMPLATE]

This should allow for smarter docker images, without breaking their portability and encapsultation.