Skip to content

Commit 6f0b6a7

Browse files
author
Tom Softreck
committed
first draft
1 parent a87c462 commit 6f0b6a7

File tree

9 files changed

+446
-53
lines changed

9 files changed

+446
-53
lines changed

caddy/Caddyfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
api.naszed.de {
2+
reverse_proxy myapi:8080
3+
tls {
4+
dns cloudflare {env.CLOUDFLARE_API_TOKEN}
5+
}
6+
}

caddy/README.md

Lines changed: 160 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,204 @@
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+
---
337

4-
echo "🐋 Building and starting containers..."
5-
docker-compose --env-file .env up -d --build
6-
docker-compose up -d
38+
## 📋 Prerequisites
739

8-
```
40+
* ✅ Docker & Docker Compose
41+
* ✅ Cloudflare-managed domain
42+
* ✅ Cloudflare API Token with DNS edit permission
943

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+
```
1054

11-
lucaslorentz/caddy-docker-proxy: Obraz Caddy, który dynamicznie generuje konfigurację na podstawie Docker metadata (labels).
55+
2. Create a Docker network (once):
1256

13-
Brak Caddyfile: Wszystko robisz przez labelsy, co spełnia Twoje wymaganie jednoplikowej konfiguracji.
57+
```bash
58+
docker network create web || true
59+
```
1460

15-
Certyfikaty SSL: W pełni automatyczne dzięki integracji z Cloudflare przez caddy.tls.dns.
61+
3. Add your `.env` file:
1662

63+
```env
64+
CLOUDFLARE_API_TOKEN=your_cloudflare_token_here
65+
DOMAIN=example.com
66+
67+
```
1768

69+
4. Start everything:
70+
71+
```bash
72+
make up
73+
```
74+
75+
---
1876

77+
## 📦 Installation
78+
79+
You can also use the provided installation script:
80+
81+
```bash
82+
chmod +x install.sh
83+
./install.sh
84+
```
1985

86+
The script will:
2087

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
2293

2394
---
2495

25-
## ✅ 1. **Log Viewing Command**
96+
## 🛠️ Usage (Makefile)
2697

27-
To show logs for your API service (`myapi`):
98+
Unified CLI control with:
2899

29100
```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
31110
```
32111

33-
To show logs for **Caddy reverse proxy**:
112+
---
113+
114+
## 🧪 Developer Tools
115+
116+
You can also use these commands:
34117

35118
```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)
37122
```
38123

39-
> Add `--tail=100` to limit to the last 100 lines.
124+
✅ These run inside temporary containers – you don't need local Python installed.
40125

41126
---
42127

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+
---
44163
45-
This script will:
164+
## 🔧 Optional Enhancements
46165
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
53167
54-
## 🚀 How to use:
168+
Makefile includes:
55169
56170
```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
59173
```
60174

175+
### ♻️ Auto-reload
176+
177+
FastAPI auto-reload enabled via:
61178

179+
```yaml
180+
command: uvicorn app.main:app --host 0.0.0.0 --port 8080 --reload
181+
volumes:
182+
- ./app:/app/app:ro
183+
```
62184
63-
## 📦 How to use
185+
### 📦 Multi-service support
64186
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:
68188
69189
```bash
70-
make up
71-
make logs
72-
make down
73-
make restart
190+
make logs SERVICE=auth
191+
make shell SERVICE=web
74192
```
75193

194+
`Makefile` dynamically uses `$(SERVICE)` to manage different containers.
195+
76196
---
77197

78-
### ✅ Optional Enhancements:
198+
## ℹ️ Notes
79199

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.
83204

84-
Let me know if you'd like those included too.

caddy/docker-compose.yml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
services:
22
caddy:
3-
image: lucaslorentz/caddy-docker-proxy:2.8.0
3+
image: caddy:2.8.0
44
container_name: caddy
55
ports:
66
- "80:80"
77
- "443:443"
88
volumes:
9-
- /var/run/docker.sock:/var/run/docker.sock:ro
9+
- ./Caddyfile:/etc/caddy/Caddyfile
10+
- caddy_data:/data
11+
- caddy_config:/config
1012
environment:
11-
- CADDY_INGRESS_NETWORK=web
12-
- CLOUDFLARE_API_TOKEN=\${CF_API_TOKEN}
13+
- CLOUDFLARE_API_TOKEN=${CLOUDFLARE_API_TOKEN}
1314
networks:
1415
- web
1516
restart: unless-stopped
1617

1718
myapi:
1819
build: .
1920
container_name: myapi
20-
labels:
21-
caddy: $DOMAIN
22-
caddy.reverse_proxy: "{{upstreams 8080}}"
23-
caddy.tls.dns: cloudflare \${CF_API_TOKEN}
21+
expose:
22+
- "8080"
2423
networks:
2524
- web
25+
command: uvicorn app.main:app --host 0.0.0.0 --port 8080 --reload
2626

2727
networks:
2828
web:
29-
external: true
29+
external: true
30+
31+
volumes:
32+
caddy_data:
33+
caddy_config:

caddy/generate-cloudflare-token.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ echo "Ustaw zakres (Zone Resources):"
2424
echo "→ Include → Specific zone → wybierz swoją domenę"
2525
echo ""
2626
echo "🔑 Po wygenerowaniu tokenu, dodaj go do pliku .env jako:"
27-
echo "CF_API_TOKEN=twój_token_tutaj"
27+
echo "CLOUDFLARE_API_TOKEN=twój_token_tutaj"
2828

2929
curl "https://api.cloudflare.com/client/v4/user/tokens/verify" \
30-
-H "Authorization: Bearer ${CF_API_TOKEN}"
30+
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}"

0 commit comments

Comments
 (0)