Config for multiple domains and apps via environment-variable

1. The problem I’m having:

I have multiple configs for each app (subdomain). My goal is not touching the config file for multiple domains. I want to define the different domain names via an environment variable.

At the moment i define my apps for a domain like this. So i can specify the domain name via environment var.

app1.{$BASE_DOMAIN} {
 ...
}

This is working fine, but i have several host that have multiple (alternative) domain-names that should serve the same apps.

app1.domain-a.com, app1.domain-a.com {
...
}

app2.domain-a.com, app2.domain-b.com {
...
}

so it would be nice to have something like this possible.

app1.{$DOMAINS} {
...
}

app2.{$DOMAINS} {
...
}

where environment var $DOMAINS can be for example “localhost | domain-a | domain-b”

Is it possible to use dynamic (environment-based) base-domain-names, so that i did not need to touch the caddy-config for multiple domains?

Thank you for any hint on this.

Tobias

At this point, I would probably look at something like dockerize to see if it can template the Caddyfile the way you want. Then run dockerize shortly before starting Caddy so it generates the final Caddyfile from a template.

Dockerize is meant for templating inside Docker containers, but you can also use it outside containers. It is a relatively small Golang app.

Another option is to write a simple shell script that generates the final Caddyfile based on your criteria before launching Caddy.