Every target renders one Go text/template into one file. A target names its template with template=<file> (resolved against --templates-dir unless absolute) or takes one from a preset (preset=caddy). Templates are read once at startup.
The template is executed with:
| Field | Type | Description |
|---|---|---|
.Label |
string |
The target's label. |
.Flags |
map[string]string |
The target's own extra key=value options (e.g. job=incus). |
.Instances |
[]Instance |
Every running instance carrying the label, sorted by project, then name. |
Each Instance:
| Field | Type | Description |
|---|---|---|
.Name |
string |
Instance name. |
.Project |
string |
Incus project. |
.Location |
string |
Cluster member the instance runs on; none on a standalone server. |
.Label |
string |
Value of user.label.<label>. |
.Labels |
map[string]string |
Keys below the label with the prefix cut: user.label.prom.port is .Labels.port. |
.Config |
map[string]string |
Full expanded configuration, without volatile.* keys. |
.Devices |
map[string]map[string]string |
Expanded devices by name. |
.Interfaces |
[]Interface |
One per NIC: .Network, .Project, .IPv4, .IPv6. |
.IPv4 "<network>" |
method | First non-loopback IPv4 on that network, or on any network when it has none; .IPv4 "" for any. |
A label change, an address change, or a move to another cluster member re-renders the target.
A label or flag an instance or target does not carry renders as an empty string, so {{ .Labels.port }} on an instance without port gives nothing rather than <no value>. Test for it with {{ if .Labels.port }}. Where an empty value would still pass the target's checks but be wrong, wrap it in required to fail the render instead, and the error is logged. When a render fails, each instance is rendered on its own: the ones that fail are left out of the file, and the rest are deployed. When every instance fails, or none does alone, the fault is the target's and the live file is left as it is. This blames the right instance only while each instance's part of the output depends on that instance alone: a template reading across .Instances (index .Instances 1, len .Instances, $.Instances inside the range) can have an instance left out for a fault that is not its own.
Besides the Go built-ins (index, printf, len, range, ...):
| Func | Description |
|---|---|
json <value> |
JSON-encode a value: {{ json .Name }} gives "web-1". |
join <list> <sep> |
strings.Join. |
split <string> <sep> |
strings.Split. |
required <msg> <value> |
<value>, or fail the render with <msg> when it is empty. |
vhosts . |
Caddy: group the instances into site blocks (see Caddy Preset). |
vhost <v> |
Caddy: render one site block. |
Caddy per-vhost templates get json, join, split and required too.
file_sdPrometheus watches the file itself, so the target needs no commands. Examples has the complete stack, with Prometheus reading the file from its own volume:
incus-templated run --templates-dir /etc/incus-templated/templates \
--target prom,path=/etc/prometheus/sd/incus.json,template=file_sd.json.tmpl,job=incus
{{/* /etc/incus-templated/templates/file_sd.json.tmpl */}}
[
{{- range $i, $inst := .Instances }}{{ if $i }},{{ end }}
{
"targets": [{{ json (printf "%s:%s" ($inst.IPv4 "") $inst.Labels.port) }}],
"labels": {
"job": {{ json $.Flags.job }},
"instance": {{ json $inst.Name }},
"project": {{ json $inst.Project }},
"location": {{ json $inst.Location }}
}
}
{{- end }}
]
services:
node-exporter:
image: quay.io/prometheus/node-exporter:latest
labels:
prom: ""
prom.port: "9100"
When instances sit on a per-member NAT bridge and are reached through a proxy device on the member's address, the upstream depends on where the instance runs. .Location follows the instance across members, so the mapping lives in the template. Keep the member addresses on the target as flags (member.node1=10.0.0.11) and look them up by location:
{{- range .Instances }}
{{ .Labels.domain }} {
reverse_proxy {{ required (printf "no member.%s flag" .Location) (index $.Flags (printf "member.%s" .Location)) }}:{{ .Labels.upstream }}
}
{{- end }}
--target edge,path=/etc/caddy/Caddyfile,template=members.caddyfile,format='caddy fmt --overwrite {staging}',reload='caddy reload --config {path}',member.node1=10.0.0.11,member.node2=10.0.0.12
Without required, an instance on a member with no flag would render reverse_proxy :8122, which caddy validate accepts and Caddy proxies to localhost. With it, that instance is left out and logged with no member.node3 flag, and the other instances keep deploying.
preset=caddy renders this template (see Configuration for the commands it sets):
{
admin localhost:2019
}
{{- range vhosts . }}
{{ vhost . }}
{{- end }}
vhosts . reads the target's instances with the label rules: domains, upstream=, network=, redir=, redirection domains, structured .N. routes, and load balancing across instances sharing a domain. vhost renders one site block with the block's own template= or the default below.
To change the global options block, copy the template above into your own file and point the target at it: --target edge,preset=caddy,instance=caddy,project=default,template=edge.caddyfile. The preset still provides the commands.
{{ .Domain }} {
{{- if .Redirect }}
redir {{ .Redirect }} permanent
{{- else if .Upstreams }}
reverse_proxy {{ range $i, $u := .Upstreams }}{{ if $i }} {{ end }}{{ $u }}{{ end }}
{{- end }}
}
A per-vhost template is executed with one site block:
| Variable | Type | Description |
|---|---|---|
.Domain |
string |
The domain(s) defined on the instance (user.label.<prefix>). |
.Service |
string |
The service name if defined (user.label.<prefix>.service or user.incus-compose.service). |
.Upstreams |
[]string |
Sorted list of resolved upstream addresses (e.g. ["10.0.1.5:8080", "10.0.1.6:8080"]). |
.Redirect |
string |
The redirect URL if configured via redir=<url> flag on domain. |
.Template |
string |
The raw template name or inline template string (template=<path> flag). |
.Flags |
map[string]string |
Custom flags parsed from label definitions (e.g. example.com,flag1=value1,resolvers='1.1.1.1 1.0.0.1'). Single or double quotes preserve spaces and commas. |
Custom templates are specified directly on the target label using the template=<value> flag:
For reusable site configurations, store templates in a directory mounted to incus-templated and pass --templates-dir /etc/caddy/templates.
When an instance specifies a valid path with an extension (dir+file+ext, e.g. template=spa_site.caddyfile or template=spa_site.tmpl), incus-templated resolves it against --templates-dir (or uses an absolute path if provided). If the file does not exist, rendering fails and the live Caddyfile stays as it is.
services:
web:
image: docker.io/library/nginx:alpine
labels:
edge: "spa.example.com,upstream=8080,template=spa_site.caddyfile"
Any template input without a file extension is evaluated as an inline Go template:
services:
web:
image: docker.io/library/nginx:alpine
labels:
edge: |
spa.example.com,upstream=8080,template={{ .Domain }} {
encode gzip zstd
reverse_proxy {{ index .Upstreams 0 }}
}
# /etc/caddy/templates/secure_proxy.caddyfile
{{ .Domain }} {
encode gzip zstd
header {
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
X-Content-Type-Options "nosniff"
X-Frame-Options "DENY"
Referrer-Policy "strict-origin-when-cross-origin"
}
reverse_proxy {{ range $i, $u := .Upstreams }}{{ if $i }} {{ end }}{{ $u }}{{ end }}
}
Usage in Compose:
services:
app:
image: my-app:latest
labels:
edge: "secure.example.com,upstream=3000,template=secure_proxy.caddyfile"
Useful for single-page applications (React, Vue) or microservices serving static assets:
# /etc/caddy/templates/spa_app.caddyfile
{{ .Domain }} {
root * /var/www/dist
file_server
try_files {path} /index.html
handle /api/* {
reverse_proxy {{ index .Upstreams 0 }}
}
}
Connects directly to an upstream PHP container running php-fpm:
# /etc/caddy/templates/php_site.caddyfile
{{ .Domain }} {
root * /var/www/html
php_fastcgi {{ index .Upstreams 0 }}
file_server
}
Usage in Compose:
services:
wordpress:
image: docker.io/library/wordpress:fpm-alpine
labels:
edge: "blog.example.com,upstream=9000,template=php_site.caddyfile"
Optimized for real-time applications requiring continuous flushes and keep-alives:
# /etc/caddy/templates/websocket_proxy.caddyfile
{{ .Domain }} {
reverse_proxy {{ range $i, $u := .Upstreams }}{{ if $i }} {{ end }}{{ $u }}{{ end }} {
flush_interval -1
}
}
Every rendered file is written to <path>.tmp and checked by the target's format and validate commands before it replaces the live file. With the Caddy preset that is caddy fmt and caddy validate, run inside the Caddy container.
If either fails:
incus-templated logs the command's output..tmp file is left for inspection.