Skip to main content

Render Docker Compose file templates with the power of Jinja2

Project description

Docker Compose Templer

This is a little Python3 utility that adds more dynamism to Docker Compose or Docker Stack files by utilizing the Jinja2 template engine.

Docker Compose (DC) files are quite static in nature. It is possible to use variable substitution to run slightly different container configurations based on a single DC file. This, however, doesn't allow complex variations in networks, volumes, etc. and proper code reuse. Therefore I decided to create this Python program to introduce Jinja2 templating to DC files. A definition file says where to find the templates, what variables to use and where to put the rendered files.

The documentation on the Jinja2 syntax can be found here.

Features:

  • templating using Jinja2
  • using YAML syntax for definition and variable files
  • monitoring of file changes and automatic rendering of templates (especially useful during development)
  • using some extra Jinja filters (comply with Ansible filters)

Table of contents:


Installation

Install directly from Github:

pip install git+https://github.com/Aisbergg/python-docker-compose-templer@v1.1.0

Install from PyPi:

pip install docker-compose-templer

If you like to use the optinal auto render function then you have to install the Pyinotify package as well:

pip install pyinotify

Usage

Command line arguments

usage: docker-compose-templer [-a] [-f] [-h] [-v] [--version]
                              definition_file [definition_file ...]

Render Docker Compose file templates with the power of Jinja2

positional arguments:
  definition_file    File that defines what to do.

optional arguments:
  -a, --auto-render  Monitor file changes and render templates automatically
  -f, --force        Overwrite existing files
  -h, --help         Show this help message and exit
  -v, --verbose      Enable verbose mode
  --version          Print the program version and quit

Definition File

The definition file defines what to do. It lists template and the variables to be used for rendering and says where to put the resulting file. The definition file syntax is as follows:

# (optional) define global variables to be used in all templates - can contain Jinja syntax
vars:
  some_global_var: foo
  another_global_var: "{{some_global_var}}bar" # will render to 'foobar'

# (optional) load global variables from YAML file(s) (order matters) - can contain Jinja syntax
include_vars:
  - path/to/file_1.yml
  - path/to/file_2.yml

# template definitions
templates:
  # first template
  - src: templates/my_template.yml.j2 # source file as Jinja2 template (Jinja syntax can be used on path)
    dest: stacks/s1/my_instance.yml   # path for resulting file (Jinja syntax can be used on path)
    include_vars: variables/s1.yml    # (optional) include local variables from YAML file(s)
    vars:                             # (optional) local variables for this template
      some_local_var: abc

  # second template
  - src: templates/my_template.yml.j2
    dest: stacks/s2/my_instance.yml
    vars:
      some_local_var: xyz

The variables can itself contain Jinja syntax, you only have to make sure the variables are defined prior usage. The different sources of variables are merged together in the following order:

  1. global include_vars
  2. global vars
  3. template include_vars
  4. template vars

Templates

The templates are rendered with Jinja2 using the global and local variables defined in the definition file. Any Jinja2 specific syntax can be used.

In addition to the extra filters the variable omit can be used in the templates. This concept is borrowed from Ansible and the purpose is to omit options from the DC file where a variable is not defined. In the following example the env variable VAR2 will be omitted from the template if my_var was not defined in the definition file:

services:
  foo:
    environment:
      - "VAR1=abc"
      - "VAR2={{ my_var|default(omit) }}"
    ...

Because of the omit functionality the renderer only renders YAML files, generic file types do not work.

Examples

Examples can be found in the examples directory. There are three stacks defined, one global stack and two user stacks. The user stacks define a Nextloud and Redis service. Both stacks depend on the global one, meaning those share a global MariaDB and a reverse proxy. To run this example execute the following command inside the examples/ directory: docker-compose-templer -f stack-global.yml stack-user1.yml stack-user2.yml

Extra Jinja2 Filters

In addition to the Jinja built-in filters the following extra filters are implemented. The filter are based on the filter in Ansible:

Filter Description
mandatory(msg) If the variable is undefined an error with a message msg will be thrown.
regex_escape() Escape special characters to safely use a string in a regex search.
regex_findall(pattern, ignorecase=False, multiline=False) Find all occurrences of regex matches.
regex_replace(pattern, replacement, ignorecase=False, multiline=False) Perform a regex search and replace operation.
regex_search(pattern, groups, ignorecase=False, multiline=False) Search with regex. If one or more match groups are specified the search result will be a list containing only those group matches. The groups are specified either by their position (e.g. \1) or by their name (e.g. foo: \gfoo).
regex_contains(pattern, ignorecase=False, multiline=False) Yields true if the string contains the given regex pattern.
to_bool(default_value=None) Converts a string to a bool value. The default_value will be used if the string cannot be converted.
to_yaml(indent=2, [...]) Converts a value to YAML.
to_json([...]) Converts a value to JSON.
to_nice_json(indent=2, [...]) Converts a value to human readable JSON.

Todo

  • Add pre_render and post_render options
  • Write more tests

License

Docker Compose Templer is released under the LGPL v3 License. See LICENSE.txt for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Docker Compose Templer-1.1.0.post2.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file Docker Compose Templer-1.1.0.post2.tar.gz.

File metadata

  • Download URL: Docker Compose Templer-1.1.0.post2.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.3

File hashes

Hashes for Docker Compose Templer-1.1.0.post2.tar.gz
Algorithm Hash digest
SHA256 34f4f0b7ce5ad3367f5ec7d6bed57e0f75bed61fc1cedb1f44742cf7830ba678
MD5 01a34234fc28b8a33f569edf93d8196e
BLAKE2b-256 490b91835661ced9c45ffc1a5a08935b2e0f8546ab008b0204c5d869fc84bead

See more details on using hashes here.

File details

Details for the file Docker_Compose_Templer-1.1.0.post2-py3-none-any.whl.

File metadata

  • Download URL: Docker_Compose_Templer-1.1.0.post2-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.3

File hashes

Hashes for Docker_Compose_Templer-1.1.0.post2-py3-none-any.whl
Algorithm Hash digest
SHA256 97323eeaee25f557ca4febe806e8f7256a897ac9f2165e9886362283f7875dde
MD5 5837afb6dee3086f20c8be4a80558f32
BLAKE2b-256 172c2ce5ba498e69f38a5f5c3eed74c67e4b12fdc8f03cc6c5ed477ff7e9835f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page