When to use this stack
Caddy 2 is a small web server that can serve static files and forward selected paths to another container, all from one readable configuration file. This stack solves a common layout on a single VPS: a static site (HTML, CSS and JavaScript produced by a generator or a build step) served at the root path, and an API container served under /api. When you give Caddy a real domain name in SITE_ADDRESS, it obtains and renews TLS certificates from Let’s Encrypt on its own, so you never handle certificate files by hand.
Use this stack when you have one or two public sites, one backend, and you prefer a plain text config over label-based routing. It fits landing pages with a small API, documentation sites, and front ends that want a same-origin API, which removes most CORS configuration. Do not use it when you need many services discovered automatically from Docker labels; for that, see the Traefik stack. Do not use it when your team wants a web panel to edit hosts, since Caddy is configured only through files. Caddy also does not replace a firewall. It only serves the ports you publish, so you still need to close everything else on the host.
docker-compose.yml
services:
caddy:
image: caddy:2.8-alpine
restart: unless-stopped
environment:
SITE_ADDRESS: ${SITE_ADDRESS}
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile:ro
- ./site:/srv/site:ro
- caddy_data:/data
- caddy_config:/config
depends_on:
- whoami
whoami:
image: traefik/whoami:v1.10
restart: unless-stopped
expose:
- "80"
volumes:
caddy_data:
caddy_config:# Address Caddy serves. Use your real domain for automatic HTTPS,
# or :80 for plain HTTP on a local test machine.
SITE_ADDRESS=CHANGE_ME_yourdomain.example.com.env
# Address Caddy serves. Use your real domain for automatic HTTPS,
# or :80 for plain HTTP on a local test machine.
SITE_ADDRESS=CHANGE_ME_yourdomain.example.comStart and verify
# 1. Create the static site and the Caddyfile next to docker-compose.yml
mkdir -p site
cat > site/index.html <<'EOF'
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Hello</title></head>
<body><h1>Static site served by Caddy</h1></body>
</html>
EOF
cat > Caddyfile <<'EOF'
{$SITE_ADDRESS} {
encode zstd gzip
handle /api/* {
reverse_proxy whoami:80
}
handle {
root * /srv/site
file_server
}
}
EOF
# 2. Set SITE_ADDRESS in .env, validate, then start
docker compose config --quiet
docker compose up -d
docker compose ps
docker compose logs --tail=50 caddy
# 3. Verify (local test with SITE_ADDRESS=:80)
curl -I http://localhost/
curl -s http://localhost/api/ | head -n 5
# With a real domain, test over HTTPS instead:
# curl -I https://yourdomain.example.com/Production checklist
- Point a DNS A record to the server and set SITE_ADDRESS to that domain. Keep ports 80 and 443 open, because the ACME HTTP challenge uses port 80.
- Mount the Caddyfile and the site folder read-only (
:ro), so a compromised application cannot rewrite the static content or the routing. - Publish only ports 80 and 443. The whoami backend uses
exposeand stays private on the Docker network. - Back up the caddy_data volume. It holds the certificates and account keys, and recreating it repeatedly can hit Let’s Encrypt rate limits.
- Add security headers in the site block, such as Strict-Transport-Security, X-Content-Type-Options and Referrer-Policy, and check them with a header scanner.
- Limit upload size on the API route with
request_body { max_size 10MB }if the backend accepts files. - Enable a host firewall that allows only SSH, 80 and 443, and use key-based SSH logins.
- Pin the image (caddy:2.8-alpine) and upgrade on purpose after reading the release notes.
Common problems
- Symptom: The site stays on HTTP or the browser shows a certificate error. Cause: DNS does not yet point to this server, or port 80 is blocked, so the ACME challenge fails. Fix: Fix the A record and firewall, then search the logs with
docker compose logs caddyfor a line saying a certificate was obtained. - Symptom: Every path returns 404. Cause: The ./site folder is empty, or it did not exist when Compose started, so Docker created an empty directory. Fix: Run
docker compose exec caddy ls /srv/siteand add an index.html. - Symptom: /api returns 502 Bad Gateway. Cause: The backend container is not running, or the port in reverse_proxy is not the port the app listens on. Fix: Run
docker compose ps whoamiand match the port in the Caddyfile to the application. - Symptom: Caddy exits at startup with a parse error. Cause: Mixed tabs and spaces or a missing brace in the Caddyfile. Fix: Run
docker compose exec caddy caddy validate --config /etc/caddy/Caddyfileand fix the reported line. - Symptom: Changes to the Caddyfile have no effect. Cause: Caddy loads its configuration at start. Fix: Run
docker compose exec caddy caddy reload --config /etc/caddy/Caddyfile.
Variations
- Traefik instead of Caddy: Choose Traefik when you want routing defined by container labels across many services. See the Traefik stack in this series.
- Node or other app server: Replace the static handle block with
reverse_proxy app:3000when the front end is rendered by a Node process. - Nginx instead of Caddy: Use nginx:1.27-alpine when the team already maintains nginx.conf files. You then manage certificates yourself, because Caddy’s automatic HTTPS is no longer available.
- Several API replicas: Use a dynamic upstream such as
reverse_proxy { dynamic a { name api port 80 } }so Caddy balances across every replica the service name resolves to.
Questions
Do I need a Caddyfile for this stack?
Yes. Caddy reads its routing rules from /etc/caddy/Caddyfile, and this stack bind-mounts a Caddyfile from the project folder. The Start and verify block creates it for you.
Why does /api return text from whoami?
whoami is a tiny demo backend that echoes the request. Replace it with your own API image and keep the same reverse_proxy line.
Can I run this on a VPS without a domain?
Yes, set SITE_ADDRESS to :80 for plain HTTP. Browsers and most APIs expect HTTPS in production, so use a real domain before you go live.
Who renews the TLS certificates?
Caddy renews them automatically before they expire. Keep the caddy_data volume, because it stores the certificates and the ACME account keys.
Adapt this stack
Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.
Open in builderRelated stacks
Traefik v3 with Automatic HTTPS (Let's Encrypt)
Traefik v3 in Docker Compose with label-based routing, HTTP to HTTPS redirect and automatic Let's Encrypt certificates. Pinned images and .env configuration.
Gitea Git Server with PostgreSQL 16
Self-hosted Gitea with PostgreSQL 16 in Docker Compose: web Git hosting, SSH on port 2222, a healthcheck-gated database start and persistent volumes.
Loki, Promtail and Grafana Log Stack
Collect Linux host logs with Loki and Promtail in Docker Compose and query them in Grafana. Pinned images, persistent volumes and a localhost-only Grafana port.
Portainer CE Web UI for Docker Management
Run Portainer CE in Docker Compose as a web UI for containers, images and volumes. Bound to localhost on HTTPS 9443, with persistent data and a pinned image.
Browse more Infrastructure & Monitoring stacks or the full library.