Run Caddy as a standalone reverse proxy in Docker for all your sites

Published

Caddy is a web server and reverse proxy that obtains and renews HTTPS certificates on its own. A reverse proxy sits in front of your apps, receives all web traffic on ports 80 and 443, and forwards each request to the right backend based on the hostname.

Many projects ship a Compose file with Caddy bundled in. That works for one app, but it falls apart as soon as you run a second one, because only one container can own ports 80 and 443. The cleaner design is a standalone Caddy stack that every other app connects to. This guide builds exactly that, and uses example.com names throughout. Replace them with your own.

The design

  • One Caddy container is the only thing that publishes ports 80 and 443.
  • A shared Docker network called proxy connects Caddy to every app that lives on the same Docker host. Those apps publish no ports.
  • One small file per site in a sites folder keeps the configuration easy to read and edit.
  • Backends on other machines work the same way. Caddy just proxies to their IP address and port.
  • Persistent folders on the host hold the certificates, so rebuilding the container never loses them.

Requirements

  • A Linux host, VM or LXC container with Docker and Docker Compose. In an unprivileged LXC container, enable nesting in the container options so Docker can run.
  • DNS records for your hostnames pointing at the machine's public IP.
  • Ports 80 and 443 reachable from the internet (forwarded on your router if you are at home). Caddy needs them to prove ownership of the domain and get certificates.

1. Folder layout

sudo mkdir -p /opt/caddy/caddy/{sites,data,config,logs}
cd /opt/caddy

The result:

/opt/caddy/
├── compose.yaml
├── Caddyfile
├── .env
└── caddy/
    ├── sites/    one .caddy file per site
    ├── data/     certificates and keys (back this up)
    ├── config/   Caddy's autosaved config
    └── logs/     access logs

2. Create the shared network

docker network create proxy

The network is created once. Every stack that needs the proxy will join it as an external network.

3. The Compose file

Create /opt/caddy/compose.yaml:

services:
  caddy:
    image: caddy:2
    container_name: caddy
    restart: unless-stopped
    env_file: .env
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"        # HTTP/3
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - ./caddy/sites:/etc/caddy/sites:ro
      - ./caddy/data:/data
      - ./caddy/config:/config
      - ./caddy/logs:/var/log/caddy
    networks:
      - proxy

networks:
  proxy:
    external: true

Why bind mounts instead of named volumes? You can see the files, copy them, and back them up with ordinary tools. The data folder matters most, because it holds your certificates and account keys.

4. The main Caddyfile

Create /opt/caddy/Caddyfile:

{
    email [email protected]
}

# shared header block, reused by any site
(secure_headers) {
    header {
        Strict-Transport-Security "max-age=31536000"
        X-Content-Type-Options nosniff
        Referrer-Policy strict-origin-when-cross-origin
        -Server
    }
}

import /etc/caddy/sites/*.caddy

The email address is used by the certificate authority to warn you about expiring certificates. The import line loads every file in the sites folder, so adding a site never touches this file.

The .env file can start empty. Anything you put in it is available inside the Caddyfile as {$NAME}, which is handy for hostnames you want to keep out of the config:

APP_HOST=app.example.com

5. Add sites

Each site is one file in /opt/caddy/caddy/sites/.

An app on the same Docker host

The app joins the proxy network and publishes no ports. In its own compose file:

services:
  app:
    image: your/app:latest
    networks:
      - proxy

networks:
  proxy:
    external: true

Then sites/app.caddy:

app.example.com {
    import secure_headers
    reverse_proxy app:8080
}

app is the container name (or service name), and 8080 is the port the app listens on inside its container. Docker's built-in DNS resolves the name.

A backend on another machine

For a service running on a different VM or server, use its address instead of a container name. sites/media.caddy:

media.example.com {
    import secure_headers
    reverse_proxy 192.168.1.50:8096
}

Make sure the backend's firewall allows connections from the Caddy machine.

A backend that uses HTTPS with a self-signed certificate

Some appliances only speak HTTPS with their own certificate. Tell Caddy to connect that way and skip verification for that one backend:

admin.example.com {
    reverse_proxy https://192.168.1.60:8006 {
        transport http {
            tls_insecure_skip_verify
        }
    }
}

Only do this for a backend on a network you trust, because it disables certificate checking for that connection.

Hostnames from the environment file

{$APP_HOST} {
    reverse_proxy app:8080
}

6. Start it and check the config

cd /opt/caddy
docker compose up -d
docker logs caddy

Validate and reload after any change to a site file:

docker exec caddy caddy validate --config /etc/caddy/Caddyfile
docker exec caddy caddy reload --config /etc/caddy/Caddyfile

A reload applies the new config without dropping connections. The first request to each new hostname triggers certificate issuance, which usually takes a few seconds. Plain HTTP requests are redirected to HTTPS automatically, so you do not need to configure that.

7. Access logs

Logs matter once you want to see who is hitting your services, or feed them to a tool like fail2ban. Add a log block to a site:

media.example.com {
    log {
        output file /var/log/caddy/access.log {
            roll_size 10MiB
            roll_keep 5
        }
        format json
    }
    reverse_proxy 192.168.1.50:8096
}

Several sites can write to the same file, which makes tools simpler. The rotation settings stop the file from growing forever. The logs appear on the host in /opt/caddy/caddy/logs/.

8. Moving from a Caddy bundled with an app

If Caddy currently runs inside an app's Compose file with a named volume, move it without losing certificates:

  1. Stop the old stack so ports 80 and 443 are free.
  2. Copy the certificates from the old volume into the new data folder:
docker run --rm -v OLD_VOLUME_NAME:/from -v /opt/caddy/caddy/data:/to alpine cp -a /from/. /to/
  1. Write a site file for the app, remove Caddy from the app's compose file, and attach the app to the proxy network.
  2. Start the standalone Caddy stack, then the app.

Keep the old volume until the new setup has been working for a while. It is your backup.

9. Using Cloudflare in front

If your DNS is on Cloudflare with the orange cloud (proxy) turned on, set the SSL/TLS mode to Full (strict). The other modes cause redirect loops or send traffic unencrypted between Cloudflare and your server. Caddy's real certificate satisfies strict mode.

Behind Cloudflare, Caddy logs Cloudflare's addresses unless you tell it which proxies to trust, which matters for any IP-based blocking.

Hostnames that only exist on your local network, such as an admin page, cannot get certificates through the normal public challenge. For those, use Caddy's DNS challenge, which needs a Caddy build that includes your DNS provider's plugin.

10. Security checklist

  • Only Caddy publishes ports 80 and 443. Apps stay private on the Docker network.
  • Never publish an app's own port just to test it. Use the proxy.
  • Restrict sensitive pages, such as admin interfaces, to private address ranges:
@internal remote_ip private_ranges
handle @internal {
    reverse_proxy app:8080
}
handle {
    respond "Forbidden" 403
}
  • Keep the image updated and back up /opt/caddy, especially the caddy/data folder.

Updating and backups

cd /opt/caddy
docker compose pull
docker compose up -d

Back up the whole /opt/caddy folder. If you lose caddy/data, Caddy simply requests new certificates, but doing that repeatedly can hit the certificate authority's rate limits.

Troubleshooting

  • 502 Bad Gateway: Caddy cannot reach the backend. Check the container name, the internal port, and that both containers are on the proxy network (docker network inspect proxy). For a backend on another machine, test with curl from the Caddy host.
  • Certificate not issued: the hostname must resolve to your server, and ports 80 and 443 must be reachable from outside. Read docker logs caddy for the exact reason.
  • "Address already in use": something else owns port 80 or 443, often an old Caddy, Apache or nginx. Check with sudo ss -tlnp | grep -E ':80 |:443 '.
  • Redirect loop behind Cloudflare: the SSL mode is on Flexible. Switch it to Full (strict).
  • Changes not applied: run caddy validate first, then reload. A syntax error leaves the old config running.
  • A site file is ignored: it must end in .caddy and sit in the sites folder you mounted.

Summary

A standalone Caddy gives every app on your server automatic HTTPS and a single front door. Create the proxy network, run Caddy with persistent folders, and add one small file per site, whether the backend is a container on the same host or a service on another machine. From there, logging, access restrictions and extra sites are small changes to the same pattern.

← All guides