Sign inSign up

dinutac/jinja2docker

By dinutac

•Updated over 2 years ago

Jinja2 templating with Docker

Image
3

1M+

dinutac/jinja2docker repository overview

⁠Jinja2 templating with Docker

⁠Build & Coverage

Build Status Coverage Status Codacy Badge Maintainability

⁠Docker Hub

Docker Hub Image⁠

Docker Image Version (tag latest semver) MicroBadger Layers (tag) Docker Image Size (tag)

⁠Github docker image

Docker hub enforced rate limits⁠ starting with November 2020.
Github Docker Image as package⁠

⁠Steps to use

  • Mount the directory containing your template(s) to the container's /templates directory
  • Mount the directory containing your variables file(s) directory /variables
  • Pass needed env vars (any number)
  • In your jinja2 template get OS environment variables plus your inserted environment vars with environ('your_env_var')

⁠Supported formats (default)

  • YAML
  • JSON
  • XML
  • TOML

Check jinja2-cli⁠ commands for all supported formats.

⁠Syntax

docker run --rm \
-v **TEMPLATE_FOLDER**:/templates \ 
-v **VARIABLES_FOLDER**:/variables  \
-e CUSTOM_ENV_VAR=**VALUE** \
dinutac/jinja2docker:latest /templates/json.j2 /variables/json.json --format=json > **OUTPUT_FILE**

Example 1:

docker run --rm 
-v $PWD/inputs/templates:/templates 
-v $PWD/inputs/variables:/variables \
-e DATABASE=mysql56 -e IMAGE=latest \
dinutac/jinja2docker:latest /templates/standalone.j2 /variables/variables.yml --format=yaml > docker-compose.yml

Example 2:

docker run --rm 
-v $PWD/inputs/templates:/templates 
-v $PWD/inputs/variables:/variables
dinutac/jinja2docker:latest /templates/json.j2 /variables/json.json --format=json

⁠Templating example

template.json

Os: {{os}}
Flavour: {{flavour}}
   
Path: {{environ('PATH')}}

variables.json

{
  "os" : "Linux",
  "flavour": "CentOS"
}

⁠Templating result

result.json

Os: Linux
Flavour: CentOS

Path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

⁠Embedded custom render

If you want to use the custom embedded render you must override the entrypoint with /scripts/entities/render.py. It supports:

  • YAML
  • JSON

Example:

docker run --rm --entrypoint /scripts/entities/render.py
-e DATABASE=mysql56 -e IMAGE=latest 
dinutac/jinja2docker:latest json.j2 /variables/json.json

The call is similar to jinja2-cli default render, but the template is called by name, not by path. The template must exist in /templates dir.

⁠Write your own custom render

If you want to write your own custom jinja2 render:

  • Override the render.py file (you must use this file name) in /scripts/entities/render.py in order to execute your own logic.
  • Verify the Dockerfile and add the needed python packages (requirements.txt).

Tag summary

Content type

Image

Digest

sha256:9340dde8a…

Size

29.9 MB

Last updated

over 2 years ago

docker pull dinutac/jinja2docker