|
| 1 | +To integrate Caddy into your Docker Compose setup for dynamic subdomain routing without external configuration files, use the `caddy-docker-proxy` solution. This approach leverages Docker labels for service discovery and environment variables for global settings, eliminating the need for a standalone Caddyfile. |
| 2 | + |
| 3 | +### ✅ Key Features |
| 4 | +- **Zero external config files** (no Caddyfile) |
| 5 | +- **Automatic HTTPS via Let's Encrypt + Cloudflare DNS** |
| 6 | +- **Dynamic service discovery** (add/remove services via Docker labels) |
| 7 | +- **ARM64 compatible** |
| 8 | +- **Single Docker Compose file** |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +### 🧩 Docker Compose Configuration |
| 13 | + |
| 14 | +```yaml |
| 15 | +version: '3.8' |
| 16 | + |
| 17 | +networks: |
| 18 | + app-network: |
| 19 | + driver: bridge |
| 20 | + |
| 21 | +services: |
| 22 | + # Caddy Reverse Proxy with Docker Integration |
| 23 | + caddy: |
| 24 | + image: lucaslorentz/caddy-docker-proxy:latest |
| 25 | + environment: |
| 26 | + - CADDY_DOCKER_PROXY_ACME_DNS=cloudflare ${CF_API_TOKEN} |
| 27 | + |
| 28 | + - DOMAIN=example.com |
| 29 | + volumes: |
| 30 | + - /var/run/docker.sock:/var/run/docker.sock |
| 31 | + ports: |
| 32 | + - "80:80" |
| 33 | + - "443:443" |
| 34 | + networks: |
| 35 | + - app-network |
| 36 | + |
| 37 | + # Example Microservice 1: API Service |
| 38 | + api: |
| 39 | + image: your-api-image:latest |
| 40 | + labels: |
| 41 | + - caddy=${API_SUBDOMAIN}.${DOMAIN} |
| 42 | + - caddy.reverse_proxy={{upstreams 8080}} |
| 43 | + networks: |
| 44 | + - app-network |
| 45 | + |
| 46 | + # Example Microservice 2: Web App |
| 47 | + web: |
| 48 | + image: your-web-app:latest |
| 49 | + labels: |
| 50 | + - caddy=${WEB_SUBDOMAIN}.${DOMAIN} |
| 51 | + - caddy.reverse_proxy={{upstreams 3000}} |
| 52 | + networks: |
| 53 | + - app-network |
| 54 | + |
| 55 | + # Example Microservice 3: Auth Service |
| 56 | + auth: |
| 57 | + image: your-auth-service:latest |
| 58 | + labels: |
| 59 | + - caddy=${AUTH_SUBDOMAIN}.${DOMAIN} |
| 60 | + - caddy.reverse_proxy={{upstreams 4000}} |
| 61 | + networks: |
| 62 | + - app-network |
| 63 | +``` |
| 64 | +
|
| 65 | +--- |
| 66 | +
|
| 67 | +### 📁 Environment Variables (`.env` file) |
| 68 | + |
| 69 | +```env |
| 70 | +DOMAIN=example.com |
| 71 | +CF_API_TOKEN=your_cloudflare_api_token |
| 72 | +API_SUBDOMAIN=api |
| 73 | +WEB_SUBDOMAIN=app |
| 74 | +AUTH_SUBDOMAIN=auth |
| 75 | +``` |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +### 🔐 How It Works |
| 80 | + |
| 81 | +- **Dynamic Configuration**: The `caddy-docker-proxy` image automatically detects containers with `caddy.*` labels. |
| 82 | +- **HTTPS Automation**: Uses Cloudflare DNS for Let's Encrypt challenges via the `CADDY_DOCKER_PROXY_ACME_DNS` environment variable. |
| 83 | +- **Service Discovery**: The `{{upstreams PORT}}` template routes traffic to the correct container based on exposed ports. |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +### 🧪 Adding New Services |
| 88 | + |
| 89 | +To deploy a new service (e.g., `dashboard`), simply add it to your Docker Compose with the appropriate labels: |
| 90 | + |
| 91 | +```yaml |
| 92 | +dashboard: |
| 93 | + image: your-dashboard:latest |
| 94 | + labels: |
| 95 | + - caddy=dashboard.example.com |
| 96 | + - caddy.reverse_proxy={{upstreams 8000}} |
| 97 | + networks: |
| 98 | + - app-network |
| 99 | +``` |
| 100 | + |
| 101 | +No need to restart Caddy or edit any configuration files — it auto-reloads! |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +### 🛡️ Security Notes |
| 106 | + |
| 107 | +- **Docker Socket**: Mounting `/var/run/docker.sock` gives Caddy access to Docker events. Ensure your system is secured (e.g., restricted access to Docker). |
| 108 | +- **Cloudflare Token**: Use a **scoped token** with only DNS write permissions for `example.com`. |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +### 📦 Resource Usage |
| 113 | + |
| 114 | +- **Memory**: ~40–80MB baseline |
| 115 | +- **Performance**: Sufficient for most microservices (3,750+ req/s) |
| 116 | +- **ARM Compatibility**: Works on Raspberry Pi 3+ and newer ARM64 devices |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +### ✅ Why This Works |
| 121 | + |
| 122 | +- **No Caddyfile needed** — all config via environment variables and Docker labels. |
| 123 | +- **Fully declarative** — everything defined in `docker-compose.yml`. |
| 124 | +- **Scalable** — add new services with minimal effort. |
| 125 | +- **Secure** — automatic HTTPS via DNS-01 with Cloudflare. |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +### 🧼 Cleanup (Optional) |
| 130 | + |
| 131 | +If you want to reset SSL certificates or force Caddy to re-fetch config: |
| 132 | + |
| 133 | +```bash |
| 134 | +docker-compose down -v |
| 135 | +``` |
| 136 | + |
| 137 | +This removes persistent data (e.g., certificates), and a fresh setup will occur on next `up`. |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +### 📌 Summary |
| 142 | + |
| 143 | +This solution offers the **best balance of simplicity, flexibility, and security** for microservices on resource-constrained systems. It avoids vendor lock-in (unlike Cloudflare Tunnels), keeps memory usage low (unlike Traefik), and provides **zero-config deployment** with full HTTPS automation. |
0 commit comments