Skip to content

Commit dff28d4

Browse files
authored
Refactor to allow multiple domains via environment variables #6
2 parents fd54a32 + 990581b commit dff28d4

File tree

5 files changed

+59
-72
lines changed

5 files changed

+59
-72
lines changed

.github/workflows/test.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ jobs:
2424
docker run -d --name upstream -p 3000:80 nginx:alpine
2525
sleep 2
2626
27-
- name: Create .env
27+
- name: Create env file
2828
run: |
29-
echo "DOMAIN=${{ env.DOMAIN }}" > .env
30-
echo "HTTP_PORT=${{ env.HTTP_PORT }}" >> .env
31-
echo "HTTPS_PORT=${{ env.HTTPS_PORT }}" >> .env
32-
echo "UPSTREAM_URL=${{ env.UPSTREAM_URL }}" >> .env
29+
echo "DOMAIN=${{ env.DOMAIN }}" > .env.${{ env.DOMAIN }}
30+
echo "HTTP_PORT=${{ env.HTTP_PORT }}" >> .env.${{ env.DOMAIN }}
31+
echo "HTTPS_PORT=${{ env.HTTPS_PORT }}" >> .env.${{ env.DOMAIN }}
32+
echo "UPSTREAM_URL=${{ env.UPSTREAM_URL }}" >> .env.${{ env.DOMAIN }}
3333
3434
- name: Add test domain to hosts
3535
run: echo "127.0.0.1 ${{ env.DOMAIN }}" | sudo tee -a /etc/hosts
3636

3737
- name: Build images
38-
run: docker compose build
38+
run: docker compose --env-file .env.${{ env.DOMAIN }} build
3939

4040
- name: Generate certificates
41-
run: docker compose --profile setup run --rm mkcert
41+
run: docker compose --env-file .env.${{ env.DOMAIN }} --profile setup run --rm mkcert
4242

4343
- name: Verify certificates exist
4444
run: |
@@ -47,13 +47,13 @@ jobs:
4747
test -f certs/${{ env.DOMAIN }}.rootCA.pem
4848
4949
- name: Start proxy
50-
run: docker compose up -d
50+
run: docker compose --env-file .env.${{ env.DOMAIN }} up -d
5151

5252
- name: Wait for Caddy to start
5353
run: sleep 3
5454

5555
- name: Check Caddy is running
56-
run: docker compose ps --status running --services | grep -q '^caddy$'
56+
run: docker compose --env-file .env.${{ env.DOMAIN }} ps --status running --services | grep -q '^caddy$'
5757

5858
- name: Test HTTP redirect
5959
run: |
@@ -64,10 +64,10 @@ jobs:
6464
curl -s --cacert certs/${{ env.DOMAIN }}.rootCA.pem https://${{ env.DOMAIN }}:${{ env.HTTPS_PORT }} | grep -q "nginx"
6565
- name: Show logs on failure
6666
if: failure()
67-
run: docker compose logs
67+
run: docker compose --env-file .env.${{ env.DOMAIN }} logs
6868

6969
- name: Stop proxy
7070
if: always()
7171
run: |
72-
docker compose down
72+
docker compose --env-file .env.${{ env.DOMAIN }} down
7373
docker rm -f upstream || true

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ certs/
55
.DS_Store
66

77
# env files
8-
.env
8+
.env*
9+
!.env.example
910

10-
# AI agents
11+
# Editors
1112
.claude/
13+
.idea/
14+
.vscode/

README.md

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,90 +11,74 @@ A Dockerized Caddy reverse proxy with automatic SSL certificate generation for l
1111

1212
## Quick Start
1313

14-
1. Configure your domain in `.env`:
14+
1. Create an env file for your domain (e.g., `.env.local.example.com`):
1515

1616
```
1717
DOMAIN=local.example.com
1818
UPSTREAM_URL=http://host.docker.internal:3000
19+
HTTP_PORT=8080
20+
HTTPS_PORT=8443
1921
```
2022

21-
> [!WARNING]
22-
> `UPSTREAM_URL` must include the scheme and port.
23+
> [!WARNING]
24+
> `UPSTREAM_URL` must include the scheme and port.
2325
24-
2. Add your domain to the hosts file:
25-
26-
**macOS/Linux:**
27-
Edit `/etc/hosts`
26+
2. Add your domain to hosts file:
2827

2928
```bash
29+
# macOS/Linux
3030
sudo sh -c 'echo "127.0.0.1 local.example.com" >> /etc/hosts'
31-
```
3231

33-
**Windows (PowerShell as Administrator):**
34-
Edit `C:\Windows\System32\drivers\etc\hosts`
35-
36-
```powershell
32+
# Windows (PowerShell as Admin)
3733
Add-Content -Path C:\Windows\System32\drivers\etc\hosts -Value "127.0.0.1 local.example.com"
3834
```
3935

40-
3. Generate certificates (first time only):
36+
3. Generate certificates:
4137

4238
```bash
43-
docker compose --profile setup run --rm mkcert
39+
docker compose --env-file .env.local.example.com --profile setup run --rm mkcert
4440
```
4541

46-
4. Install the CA certificate (one-time):
47-
48-
Replace `local.example.com` with your configured domain.
49-
50-
**macOS:**
42+
4. Install CA certificate (one-time per domain):
5143

5244
```bash
45+
# macOS
5346
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ./certs/local.example.com.rootCA.pem
54-
```
55-
56-
**Linux (Debian/Ubuntu):**
5747

58-
```bash
59-
sudo cp ./certs/local.example.com.rootCA.pem /usr/local/share/ca-certificates/local.example.com.crt
60-
sudo update-ca-certificates
61-
```
62-
63-
**Linux (Fedora/RHEL):**
64-
65-
```bash
66-
sudo cp ./certs/local.example.com.rootCA.pem /etc/pki/ca-trust/source/anchors/local.example.com.pem
67-
sudo update-ca-trust
68-
```
48+
# Linux (Debian/Ubuntu)
49+
sudo cp ./certs/local.example.com.rootCA.pem /usr/local/share/ca-certificates/local.example.com.crt && sudo update-ca-certificates
6950

70-
**Linux (Arch):**
51+
# Linux (Fedora/RHEL)
52+
sudo cp ./certs/local.example.com.rootCA.pem /etc/pki/ca-trust/source/anchors/local.example.com.pem && sudo update-ca-trust
7153

72-
```bash
54+
# Linux (Arch)
7355
sudo trust anchor ./certs/local.example.com.rootCA.pem
74-
```
7556

76-
**Windows (PowerShell as Administrator):**
77-
78-
```powershell
57+
# Windows (PowerShell as Admin)
7958
Import-Certificate -FilePath .\certs\local.example.com.rootCA.pem -CertStoreLocation Cert:\LocalMachine\Root
8059
```
8160

82-
If `.pem` import fails, convert to `.cer` first:
83-
84-
```powershell
85-
openssl x509 -in .\certs\local.example.com.rootCA.pem -out .\certs\local.example.com.rootCA.cer
86-
Import-Certificate -FilePath .\certs\local.example.com.rootCA.cer -CertStoreLocation Cert:\LocalMachine\Root
87-
```
88-
8961
5. Start the proxy:
9062

9163
```bash
92-
docker compose up -d
64+
docker compose --env-file .env.local.example.com up -d
9365
```
9466

9567
6. Visit: `https://local.example.com:8443`
9668

97-
Note (Linux): Requires Docker Engine 20.10+ for `host-gateway` support.
69+
> [!NOTE]
70+
> Linux requires Docker Engine 20.10+ for `host-gateway` support.
71+
72+
## Running Multiple Domains
73+
74+
Run multiple instances by creating separate env files with different ports:
75+
76+
```bash
77+
docker compose --env-file .env.local.example.com up -d
78+
docker compose --env-file .env.local.another.com up -d
79+
```
80+
81+
Each instance runs in its own project namespace based on the domain name.
9882

9983
## Configuration
10084

@@ -105,11 +89,6 @@ Note (Linux): Requires Docker Engine 20.10+ for `host-gateway` support.
10589
| `HTTPS_PORT` | `8443` | HTTPS port (proxy) |
10690
| `UPSTREAM_URL` | `http://host.docker.internal:3000` | URL for your local app |
10791

108-
## Ports
109-
110-
- `HTTP_PORT` (default `8080`) - HTTP (redirects to HTTPS)
111-
- `HTTPS_PORT` (default `8443`) - HTTPS
112-
11392
## Layout
11493

11594
```

docker-compose.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
name: ssl-proxy-${DOMAIN:-localhost}
12
services:
23
mkcert:
34
build:
45
context: .
56
dockerfile: Dockerfile.mkcert
6-
container_name: mkcert
7+
container_name: mkcert-${DOMAIN:-localhost}
8+
hostname: ${DOMAIN:-localhost}
79
profiles:
810
- setup
911
environment:
@@ -15,7 +17,7 @@ services:
1517
build:
1618
context: .
1719
dockerfile: Dockerfile.caddy
18-
container_name: ssl-proxy
20+
container_name: ssl-proxy-${DOMAIN:-localhost}
1921
ports:
2022
- "${HTTP_PORT:-8080}:80"
2123
- "${HTTPS_PORT:-8443}:443"

scripts/mkcert/entrypoint.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ KEY_FILE="/certs/${DOMAIN}.key.pem"
77
CA_FILE="/certs/${DOMAIN}.rootCA.pem"
88

99
if [ ! -f "$CERT_FILE" ]; then
10-
echo "Generating SSL certificate for ${DOMAIN}..."
10+
echo ""
11+
echo "Generating SSL certificate for ${DOMAIN}... 🔐"
12+
echo ""
1113
mkcert -install
1214
mkcert -cert-file "$CERT_FILE" \
1315
-key-file "$KEY_FILE" \
1416
"$DOMAIN"
1517
cp "$(mkcert -CAROOT)/rootCA.pem" "$CA_FILE"
16-
echo "=== Certificate generated ==="
18+
echo ""
19+
echo "Certificate generated ✅"
1720
else
18-
echo "Certificate already exists for ${DOMAIN}, skipping generation."
21+
echo "Certificate already exists for ${DOMAIN}, skipping generation ⏭️"
1922
fi
2023

2124
echo ""
22-
echo "=== Install CA certificate ==="
25+
echo "Install CA certificate 📋"
2326
echo ""
2427
echo "macOS:"
2528
echo " sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ./certs/${DOMAIN}.rootCA.pem"

0 commit comments

Comments
 (0)