Skip to main content

Docker compose to the next level with Jinja2 templating

Project description

Jinja Compose

jinja-compose is a tool for running docker-compose commands with Jinja2 templating.

Installation

pip install jinja-compose-wrapper

Then ensure your Python bin directory is in your PATH.

Usage

Jinja Compose operates using the same syntax as docker-compose, but with the addition of a template and optional injection file. The typical project setup looks like this:

/project_dir
├── compose.jyml
├── compose.py
└── compose.yaml  [generated by jinja-compose]

Where compose.jyml is a Jinja2 template file, compose.py is an optional Python file containing variables to be injected into the template, and compose.yaml is the generated docker-compose file.

Example

In compose.jyml we define a service called server, which only runs in production when the is_production variable is defined and only forwards ports on a certain host.

services:
  server:
    build:
      context: .
    image: sid220/apriltag_localisation:latest
    environment:
      - DAPRILTAG_PRODUCTION={{ MyJinjaComposeInjection.is_production }}
      - OPENCV_VIDEOIO_DEBUG=1
    {% if MyJinjaComposeInjection.my_static_method() == 'special_host' %}
    ports:
        - "5000:5000"
    {% endif %}

To define the is_production variable and my_static_method method, we create a compose.py file with a class that inherits from JinjaComposeInject.

from jinja_compose_wrapper.libjinja_compose import JinjaComposeInject
import socket

class MyJinjaComposeInjection(JinjaComposeInject):
    is_production = 1
    
    @staticmethod
    def my_static_method():
        return socket.gethostname()

Once done we can now bring this service up:

jinja_compose up

Or just build the yaml file:

jinja_compose -d echo

Full Usage Documentation

jinja_compose [-h] [--template TEMPLATE] [--output OUTPUT] [--injection-file INJECTION_FILE] [--dockercmd [DOCKERCMD]] [--as-root] [action ...]

positional arguments:
  action

options:
  -h, --help            show this help message and exit
  --template TEMPLATE, -i TEMPLATE, -t TEMPLATE
  --output OUTPUT, -o OUTPUT
  --injection-file INJECTION_FILE, -p INJECTION_FILE
  --dockercmd [DOCKERCMD], -d [DOCKERCMD]
  --as-root, -r

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

jinja-compose-wrapper-0.0.3.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

jinja_compose_wrapper-0.0.3-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file jinja-compose-wrapper-0.0.3.tar.gz.

File metadata

  • Download URL: jinja-compose-wrapper-0.0.3.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for jinja-compose-wrapper-0.0.3.tar.gz
Algorithm Hash digest
SHA256 741168b305697c8f2828bd78009a536307e10f82988f049f76a90c0c3716604b
MD5 6e16a0fbda4250b1a0e56ef88c255177
BLAKE2b-256 1b09748462e154357d5d9f85a91e21b391cf50400fa41c7d01353cfa0db9ba1b

See more details on using hashes here.

File details

Details for the file jinja_compose_wrapper-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for jinja_compose_wrapper-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 36045cb623a2df8bbebc187703f49084c25912a8834372d9119bdb094cf10c0b
MD5 250782f2cc70a2cfdf7eb05f8676ac86
BLAKE2b-256 188c7bfb6c535048b45480d9a64a44398ade163cd0b3a0a1d1d214bc858a269c

See more details on using hashes here.

Supported by

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