← Knowledge Base
DockerFrom my setup2 min read · updated 2026-11-27

Automatic HTTPS for a static site with Docker and Caddy

The small Docker Compose and Caddyfile setup behind this website: Caddy gets and renews Let's Encrypt certificates by itself and serves a static site, with security headers, redirects and log rotation.

caddydockerhttpsletsencryptnginxdns

This website runs on a very small VM, so the stack has to be light. It is two containers: Caddy in front (HTTPS, headers, caching, redirects) and nginx serving the static files. Caddy obtains and renews the Let's Encrypt certificate on its own; there is no certbot, no cron job and no renewal script.

What you need first

  • a DNS name that points at the server's public IP (I use a free DuckDNS name)
  • ports 80 and 443 open in every firewall layer. Port 80 is used for the HTTP redirect and port 443 for the TLS validation.

docker-compose.yml

x-logging: &logging
  driver: json-file
  options: { max-size: "10m", max-file: "3" }

services:
  caddy:
    image: caddy:2
    restart: unless-stopped
    ports: ["80:80", "443:443"]
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data          # certificates live here: do not delete
      - caddy_config:/config
    networks: [web]
    logging: *logging

  site:
    image: nginx:alpine
    restart: unless-stopped
    volumes:
      - ./site:/usr/share/nginx/html:ro
    networks: [web]
    logging: *logging

networks:
  web: {}

volumes:
  caddy_data:
  caddy_config:

The caddy_data volume holds the certificate and the ACME account. If it is deleted, Caddy simply requests a new certificate, but doing that repeatedly can hit Let's Encrypt rate limits. Include it in your backups.

Caddyfile

A reusable snippet keeps every site block short:

(common) {
    encode zstd gzip
    header {
        X-Content-Type-Options nosniff
        X-Frame-Options SAMEORIGIN
        Referrer-Policy strict-origin-when-cross-origin
        Permissions-Policy "camera=(), microphone=(), geolocation=()"
        -Server
    }
}

example.duckdns.org {
    import common
    header Strict-Transport-Security "max-age=15552000"
    @static path /assets/* /img/*
    header @static Cache-Control "public, max-age=86400"
    reverse_proxy site:80
}

Notes on the choices:

  • encode zstd gzip compresses responses; on a small VM this is worth it.
  • -Server removes the server banner.
  • HSTS tells browsers to use HTTPS only. Start with a modest max-age and raise it once you are sure everything works over HTTPS.
  • Names in the Caddyfile that are real domains get a certificate automatically. A site address that is just :80 does not.

Day-to-day commands

Validate before you reload, so a typo cannot take the site down:

docker compose exec caddy caddy validate --config /etc/caddy/Caddyfile
docker compose exec caddy caddy reload   --config /etc/caddy/Caddyfile
docker logs <project>-caddy-1 2>&1 | grep -i "certificate obtained"

Permissions trap

If you upload the site with scp from another machine, folders can arrive with mode 700, and nginx (which runs as a different user inside its container) answers 403 Forbidden. Fix it after every upload:

chmod -R a+rX ~/stack/site

Adding another site

Add another block to the Caddyfile with its own name and upstream, make sure the name's DNS record points at the server, and reload. Caddy requests the new certificate automatically.