|
1 | | -```bash |
2 | | -docker network create web |
| 1 | +# 🚀 Caddy Reverse Proxy with Docker + FastAPI |
| 2 | + |
| 3 | +A lightweight, automated, and production-ready reverse proxy setup using **Caddy** with **Docker**, featuring: |
| 4 | + |
| 5 | +* ✅ Automatic SSL via **Cloudflare DNS** |
| 6 | +* ✅ Dynamic reverse proxy via Docker **labels** |
| 7 | +* ✅ No manual Caddyfile needed |
| 8 | +* ✅ **FastAPI** backend with auto-reload support |
| 9 | +* ✅ Unified control with **Makefile** |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## 📋 Table of Contents |
| 14 | + |
| 15 | +* [✨ Features](#-features) |
| 16 | +* [📋 Prerequisites](#-prerequisites) |
| 17 | +* [🚀 Quick Start](#-quick-start) |
| 18 | +* [📦 Installation](#-installation) |
| 19 | +* [🛠️ Usage (Makefile)](#️-usage-makefile) |
| 20 | +* [📜 Logs](#-logs) |
| 21 | +* [⚙️ Configuration](#-configuration) |
| 22 | +* [🔧 Optional Enhancements](#-optional-enhancements) |
| 23 | +* [ℹ️ Notes](#️-notes) |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## ✨ Features |
| 28 | + |
| 29 | +* 🔐 Automatic HTTPS with Cloudflare DNS |
| 30 | +* 🐳 Dockerized FastAPI microservice |
| 31 | +* 🔄 Auto-reload during development |
| 32 | +* ⚙️ Declarative service routing via Docker labels |
| 33 | +* 🧼 Clean, one-file `Makefile` interface |
| 34 | +* 🧪 Ready for testing and CI workflows |
| 35 | + |
| 36 | +--- |
3 | 37 |
|
4 | | -echo "🐋 Building and starting containers..." |
5 | | -docker-compose --env-file .env up -d --build |
6 | | -docker-compose up -d |
| 38 | +## 📋 Prerequisites |
7 | 39 |
|
8 | | -``` |
| 40 | +* ✅ Docker & Docker Compose |
| 41 | +* ✅ Cloudflare-managed domain |
| 42 | +* ✅ Cloudflare API Token with DNS edit permission |
9 | 43 |
|
| 44 | +--- |
| 45 | + |
| 46 | +## 🚀 Quick Start |
| 47 | + |
| 48 | +1. Clone repo & navigate: |
| 49 | + |
| 50 | + ```bash |
| 51 | + git clone https://your-repo-url |
| 52 | + cd your-repo |
| 53 | + ``` |
10 | 54 |
|
11 | | -lucaslorentz/caddy-docker-proxy: Obraz Caddy, który dynamicznie generuje konfigurację na podstawie Docker metadata (labels). |
| 55 | +2. Create a Docker network (once): |
12 | 56 |
|
13 | | -Brak Caddyfile: Wszystko robisz przez labelsy, co spełnia Twoje wymaganie jednoplikowej konfiguracji. |
| 57 | + ```bash |
| 58 | + docker network create web || true |
| 59 | + ``` |
14 | 60 |
|
15 | | -Certyfikaty SSL: W pełni automatyczne dzięki integracji z Cloudflare przez caddy.tls.dns. |
| 61 | +3. Add your `.env` file: |
16 | 62 |
|
| 63 | + ```env |
| 64 | + CLOUDFLARE_API_TOKEN=your_cloudflare_token_here |
| 65 | + DOMAIN=example.com |
| 66 | + |
| 67 | + ``` |
17 | 68 |
|
| 69 | +4. Start everything: |
| 70 | + |
| 71 | + ```bash |
| 72 | + make up |
| 73 | + ``` |
| 74 | + |
| 75 | +--- |
18 | 76 |
|
| 77 | +## 📦 Installation |
| 78 | + |
| 79 | +You can also use the provided installation script: |
| 80 | + |
| 81 | +```bash |
| 82 | +chmod +x install.sh |
| 83 | +./install.sh |
| 84 | +``` |
19 | 85 |
|
| 86 | +The script will: |
20 | 87 |
|
21 | | -Absolutely! Below you'll find: |
| 88 | +* Scaffold your project |
| 89 | +* Download Docker images |
| 90 | +* Configure FastAPI app |
| 91 | +* Set up Caddy reverse proxy |
| 92 | +* Start services |
22 | 93 |
|
23 | 94 | --- |
24 | 95 |
|
25 | | -## ✅ 1. **Log Viewing Command** |
| 96 | +## 🛠️ Usage (Makefile) |
26 | 97 |
|
27 | | -To show logs for your API service (`myapi`): |
| 98 | +Unified CLI control with: |
28 | 99 |
|
29 | 100 | ```bash |
30 | | -docker-compose logs -f myapi |
| 101 | +make up # Build and run all containers |
| 102 | +make down # Stop containers |
| 103 | +make restart # Restart containers |
| 104 | +make logs # Show combined logs (API + Caddy) |
| 105 | +make logs-api # Show only API logs |
| 106 | +make logs-caddy # Show only Caddy logs |
| 107 | +make shell # Open shell in the API container |
| 108 | +make clean # Remove all containers, volumes, and dangling images |
| 109 | +make health # Check public HTTP health of the service |
31 | 110 | ``` |
32 | 111 |
|
33 | | -To show logs for **Caddy reverse proxy**: |
| 112 | +--- |
| 113 | + |
| 114 | +## 🧪 Developer Tools |
| 115 | + |
| 116 | +You can also use these commands: |
34 | 117 |
|
35 | 118 | ```bash |
36 | | -docker-compose logs -f caddy |
| 119 | +make lint # Run linter (flake8) |
| 120 | +make test # Run tests (pytest) |
| 121 | +make deploy # (alias for up, or hook for real deploy) |
37 | 122 | ``` |
38 | 123 |
|
39 | | -> Add `--tail=100` to limit to the last 100 lines. |
| 124 | +✅ These run inside temporary containers – you don't need local Python installed. |
40 | 125 |
|
41 | 126 | --- |
42 | 127 |
|
43 | | -## 🛠️ 2. **Installation Script** — `install.sh` |
| 128 | +## 📜 Logs |
| 129 | + |
| 130 | +```bash |
| 131 | +# Combined logs |
| 132 | +make logs |
| 133 | + |
| 134 | +# Individual service logs |
| 135 | +make logs-api |
| 136 | +make logs-caddy |
| 137 | +``` |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +## ⚙️ Configuration |
| 142 | + |
| 143 | +1. `.env` file for secrets (required): |
| 144 | + |
| 145 | +```env |
| 146 | +CLOUDFLARE_API_TOKEN=your_cloudflare_token |
| 147 | + |
| 148 | +DOMAIN=example.com |
| 149 | +``` |
| 150 | + |
| 151 | +2. Routing is defined via labels in `docker-compose.yml`: |
| 152 | + |
| 153 | +```yaml |
| 154 | +labels: |
| 155 | + caddy: api.${DOMAIN} |
| 156 | + caddy.reverse_proxy: "{{upstreams 8080}}" |
| 157 | + caddy.tls.dns: "cloudflare {env.CLOUDFLARE_API_TOKEN}" |
| 158 | +``` |
| 159 | +
|
| 160 | +No need to manually write a Caddyfile — Caddy dynamically reads this. |
| 161 | +
|
| 162 | +--- |
44 | 163 |
|
45 | | -This script will: |
| 164 | +## 🔧 Optional Enhancements |
46 | 165 |
|
47 | | -* Clone or create your project folder |
48 | | -* Set up FastAPI app |
49 | | -* Write Dockerfile and `docker-compose.yml` |
50 | | -* Create `.env` file (you just need to add your Cloudflare token) |
51 | | -* Pull Docker images |
52 | | -* Build and run the services |
| 166 | +### ✅ Lint & Test |
53 | 167 |
|
54 | | -## 🚀 How to use: |
| 168 | +Makefile includes: |
55 | 169 |
|
56 | 170 | ```bash |
57 | | -chmod +x install.sh |
58 | | -./install.sh |
| 171 | +make lint # Runs flake8 on your Python code |
| 172 | +make test # Runs pytest inside container |
59 | 173 | ``` |
60 | 174 |
|
| 175 | +### ♻️ Auto-reload |
| 176 | + |
| 177 | +FastAPI auto-reload enabled via: |
61 | 178 |
|
| 179 | +```yaml |
| 180 | +command: uvicorn app.main:app --host 0.0.0.0 --port 8080 --reload |
| 181 | +volumes: |
| 182 | + - ./app:/app/app:ro |
| 183 | +``` |
62 | 184 |
|
63 | | -## 📦 How to use |
| 185 | +### 📦 Multi-service support |
64 | 186 |
|
65 | | -1. Save as `Makefile` in your project root (same dir as `docker-compose.yml`) |
66 | | -2. Ensure `.env` exists (with `CF_API_TOKEN`) |
67 | | -3. Run commands like: |
| 187 | +Support multiple services using: |
68 | 188 |
|
69 | 189 | ```bash |
70 | | -make up |
71 | | -make logs |
72 | | -make down |
73 | | -make restart |
| 190 | +make logs SERVICE=auth |
| 191 | +make shell SERVICE=web |
74 | 192 | ``` |
75 | 193 |
|
| 194 | +`Makefile` dynamically uses `$(SERVICE)` to manage different containers. |
| 195 | + |
76 | 196 | --- |
77 | 197 |
|
78 | | -### ✅ Optional Enhancements: |
| 198 | +## ℹ️ Notes |
79 | 199 |
|
80 | | -* Add `lint`, `test`, or `deploy` commands |
81 | | -* Auto-reload support for dev via volumes |
82 | | -* Multiple service support: `PROJECT_NAME ?= $(SERVICE)` via arguments |
| 200 | +* Uses `lucaslorentz/caddy-docker-proxy` for dynamic reverse proxy configuration. |
| 201 | +* All SSL certificates are auto-managed via Cloudflare DNS. |
| 202 | +* Works well on ARM (Raspberry Pi), VPS, and dev machines. |
| 203 | +* Recommended for small production deployments with minimal overhead. |
83 | 204 |
|
84 | | -Let me know if you'd like those included too. |
|
0 commit comments