Env var in templates

1. Output of caddy version:

v2.6.2

2. running caddy via docker

docker-compose.yml

  caddy:
    build:
      context: .
      dockerfile: ./Docker/compose/local/caddy/Dockerfile
    image: caddy
    container_name: caddy
    restart: unless-stopped
    environment:
      - ADMIN_URL=secreturl
    ports:
      - 80:80
      - 443:443/tcp
      - 443:443/udp
    volumes:
      - ./Docker/compose/local/caddy/Caddyfile:/etc/caddy/Caddyfile
      - ./Docker/compose/local/caddy/common:/srv/common
      - caddy_data:/data
      - caddy_config:/config
      - ./certs:/certs

3. Question

How can I use env variables inside templates?
I found few clues on the forum and docs, but nothing worked.
It does work in Caddyfile, but not in html template.

I tried
<h1>{{env "ADMIN_URL"}}</h1>

<h1>{{.Env.ADMIN_URL}}</h1>

<h1>{{.Env "ADMIN_URL"}}</h1>

thank you

ok, seems like I had to reload config.
It seemed like templates were live reloading, perhaps for env’s extra kick was required.

{{env "ADMIN_URL"}} works fine

thanks

2 Likes