There have been multiple accounts created with the sole purpose of posting advertisement posts or replies containing unsolicited advertising.

Accounts which solely post advertisements, or persistently post them may be terminated.

Templating engine for docker compose

Maybe this is a little bit off-topic. I would like to ask how you manage your dockerfile.

I have a git repo hosting my configurations (docker-compose, traefik, etc). Then, I have a python script that reads data from JSON, renders the placeholder inside these files (the {{replace_me}}) by an actual value and outputs them to another directory. Finally, I cd to that directory and run docker-compose up -f …. (This approach takes inspiration from the terraform templatefile)

That JSON file is generated by some terraform code, along with terraform code for other stuffs (storage bucket, vps, dns, etc).

It works well for me so far. Especially for:

  • templating traefik toml configuration (I like it a lot more than the label approach).
  • secret in the docker env file (so my docker.env file has the form of secrect={{secret}}.

I know most templating docker part can be replaced by directly interpolating with environment variables but I don’t really like it because it seems environment variables are not persistent.

Do you have any suggestions for my workflow ? I am always feel a litte bit off about this approach.

Edit: Thank you for your suggestions. I will try k8s for edge computing and if it does not work really well, I will stick with my current approach.

itsamenathan ,

Here is an ansible role that will create a compose file using variables passed in. ironicbadger/ansible-role-docker-compose-generator

I tried this but ended up using ansible directly to manage my containers.

lyoko OP ,

I dont think Ansible is a general templating engine ? I do templating for all most everything and not only docker compose, for example this for traefik configuration.

ryapric ,

fwiw Ansible uses Jinja under the hood, which itself is a templating engine.

lyoko OP ,

That sounds like what I need. Thank you very much !

vampatori ,
@vampatori@feddit.uk avatar

I was recently helping someone working on a mini-project to do a bit of parsing of docker compose files, when I discovered that the docker compose spec is published as JSON Schema here.

I converted that into TypeScript types using JSON Schema to TypeScript. So I can create docker compose config in code and then just export it as yaml - I have a build/deploy script that does this at the end.

But now the great thing is that I can export/import that config, share it between projects, extend configs, mix-in, and so on. I’ve just started doing it and it’s been really nice so far, when I get a chance and it’s stabilised a bit I’m going to tidy it up and share it. But there’s not much I’ve added beyond the above at the moment (just some bits to mix-in arrays, which was what set me off on this whole thing!)

lyoko OP ,

so you are also generating the docker-compose from code. I think I am looking for something that aware of both templating and docker-compose deployment because right now, at the end of the day, I am still have to run docker-compose up -f … while helm can do both templating and deployment.

Fenzik ,

So far I just hand roll my docker-compose (at home, anyway). However, docker-compose does also support overrides via yaml merging, maybe that’s worth looking into?

My idea with that is to have a base compose that configures also my services and then to have a few override yamls with environment specific stuff (like prod, local, …)

This is similar to Kustomize from kubernetes land which I’ve worked with in the past

vampatori ,
@vampatori@feddit.uk avatar

I was using file merging, but one issue I found was that arrays don’t get merged - and since switching to use Traefik (which is great) there are a lot of arrays in the config! And I’ve since started using labels for my own tooling too.

lyoko OP ,

I actually need more than merging. For example, right now my traefik.template will look like this:

<pre style="background-color:#ffffff;">
<span style="color:#323232;">[http.routers.{{ router_name }}]
</span><span style="color:#323232;">  rule = "{{ router_rule }}"
</span><span style="color:#323232;">  service = "{{ service_name }}"
</span><span style="color:#323232;">  middlewares = [{{ middlewares | map("tojson") | join(", ") }}]
</span><span style="color:#323232;">{% block router %}{% endblock %}
</span><span style="color:#323232;">  [http.routers.{{ router_name }}.tls]
</span><span style="color:#323232;">    certResolver = "leresolver_http"
</span><span style="color:#323232;">
</span><span style="color:#323232;">{% if service_host is defined %}
</span><span style="color:#323232;">[[http.services.{{ service_name }}.loadBalancer.servers]]
</span><span style="color:#323232;">  url = "{{ 'https' if service_use_https is defined else 'http' }}://{{ service_host }}{{ ':' ~ service_port if service_port is defined else '' }}"
</span><span style="color:#323232;">{% endif %}
</span>

and then one of my traefik.toml could look like this

<pre style="background-color:#ffffff;">
<span style="color:#323232;">{% extends "template/traefik.jinja" %}
</span><span style="color:#323232;">
</span><span style="color:#323232;">{% set router_name = "dozzle" %}
</span><span style="color:#323232;">{% set router_rule = "Host(`dozzle.example.com`)" %}
</span><span style="color:#323232;">{% set service_name = "dozzle" %}
</span><span style="color:#323232;">{% set service_host = "dozzle" %}
</span><span style="color:#323232;">{% set service_port = 8080 %}
</span>
Fenzik ,

This is definitely a job for templating, seems you’ve got the right tool to me!

neoney ,
@neoney@lemmy.neoney.dev avatar

nix is pretty nice

lyoko OP ,

you mean nix the package manager ?

neoney ,
@neoney@lemmy.neoney.dev avatar

Yeah, maybe it can be used for that somehow.

scrubbles ,
@scrubbles@poptalk.scrubbles.tech avatar

Man you’re so close to Helm at this point, for me if I’m getting into templating I’d rather go on full Kubernetes and Helm

lyoko OP ,

I know about Helm but I don’t need k8s features and my VPS is just too weak for k8s I think :/

FancyGUI ,
@FancyGUI@lemmy.fancywhale.ca avatar

Honestly look into k3s or other “distros” of k8s. There are some versions of the orchestrator made for edge computing that are quite slim in their size and gives you all the perks of the k8s api layer.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • [email protected]
  • random
  • lifeLocal
  • goranko
  • All magazines