Skip to content

Commit 23cbdc7

Browse files
author
Tom Softreck
committed
caddy multiservice with caddy embedded on docker
1 parent 24476db commit 23cbdc7

32 files changed

+1776
-67
lines changed

caddy-demo/1-caddyfile/Caddyfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
debug
3+
}
4+
5+
:80 {
6+
redir https://{host}{uri} permanent
7+
}
8+
9+
:443 {
10+
tls /etc/caddy/certs/localhost.crt /etc/caddy/certs/localhost.key
11+
12+
root * /usr/share/caddy
13+
file_server
14+
15+
log {
16+
output stderr
17+
format console
18+
level INFO
19+
}
20+
}

caddy-demo/1-caddyfile/Makefile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.PHONY: up down restart logs clean certs help
2+
3+
# Default target
4+
help:
5+
@echo "Available commands:"
6+
@echo " make up - Start all services in detached mode"
7+
@echo " make down - Stop and remove all containers"
8+
@echo " make logs - View container logs"
9+
@echo " make restart - Restart all services"
10+
@echo " make certs - Generate SSL certificates"
11+
@echo " make clean - Remove containers and volumes"
12+
13+
# Start services
14+
up:
15+
@echo "Starting services..."
16+
docker-compose up -d
17+
18+
# Stop and remove containers
19+
down:
20+
@echo "Stopping services..."
21+
docker-compose down
22+
23+
# View logs
24+
logs:
25+
docker-compose logs -f
26+
27+
# Restart services
28+
restart: down up
29+
30+
# Generate SSL certificates
31+
certs:
32+
@if ! command -v mkcert &> /dev/null; then \
33+
echo "Error: mkcert is not installed. Please install it first:"; \
34+
echo "https://github.com/FiloSottile/mkcert#installation"; \
35+
exit 1; \
36+
fi
37+
@mkdir -p certs
38+
@echo "Generating local certificates..."
39+
mkcert -install
40+
mkcert -cert-file certs/localhost.crt -key-file certs/localhost.key "localhost"
41+
@echo "Certificates generated in certs/"
42+
43+
# Clean up
44+
clean: down
45+
@echo "Removing volumes..."
46+
@docker volume rm -f 1-caddyfile_caddy_data 1-caddyfile_caddy_config 2>/dev/null || true
47+
@echo "Cleanup complete"

caddy-demo/1-caddyfile/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Caddy Static Website Demo
2+
3+
This project demonstrates a simple static website served by Caddy using a Caddyfile for configuration.
4+
5+
## Features
6+
7+
- Static file serving
8+
- Automatic HTTPS with local certificates
9+
- HTTP/2 enabled
10+
- Security headers
11+
- Gzip compression
12+
13+
## Prerequisites
14+
15+
- Docker and Docker Compose
16+
- mkcert (for local certificates)
17+
18+
## Quick Start
19+
20+
1. Generate local certificates:
21+
```bash
22+
make certs
23+
```
24+
25+
2. Start the services:
26+
```bash
27+
make up
28+
```
29+
30+
3. Open in your browser: [https://localhost](https://localhost)
31+
32+
## Available Commands
33+
34+
- `make up` - Start all services in detached mode
35+
- `make down` - Stop and remove all containers
36+
- `make logs` - View container logs
37+
- `make restart` - Restart all services
38+
- `make certs` - Generate SSL certificates
39+
- `make clean` - Remove containers and volumes
40+
41+
## Project Structure
42+
43+
```
44+
.
45+
├── Caddyfile # Caddy configuration
46+
├── docker-compose.yml # Docker Compose configuration
47+
├── html/ # Static website files
48+
│ └── index.html
49+
├── certs/ # SSL certificates (generated)
50+
└── Makefile # Project commands
51+
```
52+
53+
## Customization
54+
55+
- Edit `html/index.html` to modify the website content
56+
- Update `Caddyfile` for custom routing or headers
57+
- Add more static files to the `html/` directory
58+
59+
## Troubleshooting
60+
61+
- **Certificate warnings**: Make sure to install the local CA:
62+
```bash
63+
mkcert -install
64+
```
65+
- **Port conflicts**: Stop other services using ports 80/443 or update `docker-compose.yml`
66+
67+
## License
68+
69+
MIT
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3.8'
2+
3+
services:
4+
caddy:
5+
image: caddy:latest
6+
ports:
7+
- "80:80"
8+
- "443:443"
9+
volumes:
10+
- ./Caddyfile:/etc/caddy/Caddyfile
11+
- ./html:/usr/share/caddy
12+
- ./certs:/etc/caddy/certs
13+
restart: unless-stopped
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Caddyfile Demo</title>
5+
<style>
6+
body {
7+
font-family: Arial, sans-serif;
8+
text-align: center;
9+
margin: 0;
10+
padding: 20px;
11+
min-height: 100vh;
12+
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
13+
}
14+
.container {
15+
max-width: 800px;
16+
margin: 50px auto;
17+
padding: 2rem;
18+
background: white;
19+
border-radius: 10px;
20+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
21+
}
22+
h1 {
23+
color: #2c3e50;
24+
margin-bottom: 1.5rem;
25+
}
26+
.btn {
27+
display: inline-block;
28+
padding: 0.8rem 1.5rem;
29+
background: #3498db;
30+
color: white;
31+
text-decoration: none;
32+
border-radius: 5px;
33+
margin-top: 1rem;
34+
transition: background 0.3s;
35+
}
36+
.btn:hover {
37+
background: #2980b9;
38+
}
39+
</style>
40+
</head>
41+
<body>
42+
<div class="container">
43+
<h1>Welcome to Caddyfile Demo</h1>
44+
<p>This is a static website served by Caddy using a Caddyfile configuration.</p>
45+
<p>Features:</p>
46+
<ul style="text-align: left; max-width: 300px; margin: 0 auto 1.5rem auto;">
47+
<li>Automatic HTTPS</li>
48+
<li>HTTP/2 enabled</li>
49+
<li>Gzip compression</li>
50+
<li>Security headers</li>
51+
</ul>
52+
<a href="https://caddyserver.com/docs/" class="btn" target="_blank">Caddy Documentation</a>
53+
</div>
54+
</body>
55+
</html>

caddy-demo/1-caddyfile/setup.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Check if mkcert is installed
5+
if ! command -v mkcert &> /dev/null; then
6+
echo "Error: mkcert is not installed. Please install it first:"
7+
echo "https://github.com/FiloSottile/mkcert#installation"
8+
exit 1
9+
fi
10+
11+
# Create certs directory if it doesn't exist
12+
mkdir -p certs
13+
14+
# Generate certificates
15+
echo "Generating local certificates..."
16+
mkcert -install
17+
mkcert -cert-file certs/localhost.crt -key-file certs/localhost.key "localhost"
18+
19+
echo "Setup complete! You can now start the services with:"
20+
echo "docker-compose up -d"
21+
echo ""
22+
echo "Then open: https://localhost in your browser"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
.PHONY: up down restart logs setup clean help
2+
3+
# Default target
4+
help:
5+
@echo "Available commands:"
6+
@echo " make up - Start all services in detached mode"
7+
@echo " make down - Stop and remove all containers"
8+
@echo " make logs - View container logs"
9+
@echo " make restart - Restart all services"
10+
@echo " make setup - Initial setup (modifies /etc/hosts)"
11+
@echo " make clean - Remove containers, networks, and volumes"
12+
13+
# Start services
14+
up:
15+
@echo "Building and starting services..."
16+
docker-compose up -d --build
17+
@echo "\nServices available at:"
18+
@echo "- Web: https://web.demo.local"
19+
@echo "- API: https://api.demo.local"
20+
21+
# Stop and remove containers
22+
down:
23+
@echo "Stopping services..."
24+
docker-compose down
25+
26+
# View logs
27+
logs:
28+
docker-compose logs -f
29+
30+
# Restart services
31+
restart: down up
32+
33+
# Initial setup
34+
setup:
35+
@if [ "$(shell id -u)" -ne 0 ]; then \
36+
echo "Error: This command requires root privileges. Please run with sudo."; \
37+
exit 1; \
38+
fi
39+
@echo "Adding entries to /etc/hosts..."
40+
@if ! grep -q "web.demo.local" /etc/hosts; then \
41+
echo "127.0.0.1 web.demo.local api.demo.local" >> /etc/hosts; \
42+
echo "Added entries to /etc/hosts"; \
43+
else \
44+
echo "Entries already exist in /etc/hosts"; \
45+
fi
46+
@if ! docker network inspect caddy_network >/dev/null 2>&1; then \
47+
echo "Creating Docker network..."; \
48+
docker network create caddy_network; \
49+
fi
50+
@echo "\nSetup complete! You can now start the services with:"
51+
@echo " make up"
52+
53+
# Clean up everything
54+
clean: down
55+
@echo "Removing volumes and networks..."
56+
@docker volume rm -f 2-docker-labels_caddy_data 2-docker-labels_caddy_config 2>/dev/null || true
57+
@docker network rm caddy_network 2>/dev/null || true
58+
@echo "Cleanup complete"
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Caddy Docker Proxy Demo
2+
3+
This project demonstrates how to use Docker labels to dynamically configure Caddy as a reverse proxy for multiple services.
4+
5+
## Features
6+
7+
- Automatic service discovery
8+
- Dynamic reverse proxy configuration
9+
- Multiple services with automatic HTTPS
10+
- Global middleware support
11+
- Health checks
12+
13+
## Prerequisites
14+
15+
- Docker and Docker Compose
16+
- sudo access (for modifying /etc/hosts)
17+
18+
## Quick Start
19+
20+
1. Run the setup script (requires sudo):
21+
```bash
22+
sudo make setup
23+
```
24+
25+
2. Start the services:
26+
```bash
27+
make up
28+
```
29+
30+
3. Access the services:
31+
- Web: [https://web.demo.local](https://web.demo.local)
32+
- API: [https://api.demo.local](https://api.demo.local)
33+
34+
## Available Commands
35+
36+
- `make up` - Start all services in detached mode
37+
- `make down` - Stop and remove all containers
38+
- `make logs` - View container logs
39+
- `make restart` - Restart all services
40+
- `make setup` - Initial setup (modifies /etc/hosts)
41+
- `make clean` - Remove containers, networks, and volumes
42+
43+
## Project Structure
44+
45+
```
46+
.
47+
├── docker-compose.yml # Docker Compose configuration
48+
├── api/ # Python API service
49+
│ ├── app.py
50+
│ ├── Dockerfile
51+
│ └── requirements.txt
52+
├── web/ # Static web files
53+
│ └── index.html
54+
└── Makefile # Project commands
55+
```
56+
57+
## Adding New Services
58+
59+
To add a new service, add it to `docker-compose.yml` with the appropriate labels:
60+
61+
```yaml
62+
services:
63+
my-service:
64+
image: nginx:alpine
65+
labels:
66+
- caddy=myservice.demo.local
67+
- caddy.reverse_proxy={{upstreams 80}}
68+
- caddy.@default # Apply global middleware
69+
networks:
70+
- caddy_network
71+
```
72+
73+
## Troubleshooting
74+
75+
### Certificate Warnings
76+
For local development, you'll need to accept the self-signed certificate in your browser.
77+
78+
### Hosts File
79+
If you can't access the services:
80+
1. Verify the entries in `/etc/hosts`:
81+
```
82+
127.0.0.1 web.demo.local api.demo.local
83+
```
84+
2. Make sure you ran `sudo make setup`
85+
86+
### Port Conflicts
87+
If ports 80/443 are in use:
88+
1. Stop other web servers
89+
2. Or update the port mappings in `docker-compose.yml`
90+
91+
## License
92+
93+
MIT

0 commit comments

Comments
 (0)