Skip to content

Commit f054328

Browse files
authored
docs: add caddy docker proxy example (#24)
* Added example for a setup with caddy docker proxy * Removed uncessary line. * Added sample compose file and Dockerfile
1 parent c4f4844 commit f054328

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ARG CADDY_VERSION=2.10.2
2+
FROM caddy:${CADDY_VERSION}-builder AS builder
3+
4+
RUN xcaddy build \
5+
--with github.com/lucaslorentz/caddy-docker-proxy/v2 \
6+
--with github.com/sablierapp/sablier-caddy-plugin@v1.0.1 # x-release-please-version
7+
8+
FROM caddy:${CADDY_VERSION}-alpine
9+
10+
COPY --from=builder /usr/bin/caddy /usr/bin/caddy
11+
12+
CMD ["caddy", "docker-proxy"]
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Caddy Docker Proxy Plugin Example
2+
3+
This example demonstrates how to set up [Caddy](https://caddyserver.com/) with [Sablier](https://github.com/sablierapp/sablier), [Caddy Sablier Plugin](https://github.com/sablierapp/sablier-caddy-plugin) and [Caddy-Docker-Proxy](https://github.com/lucaslorentz/caddy-docker-proxy) so Caddy can be configured with docker labels just like Sablier.
4+
5+
## Getting Started
6+
7+
### Prerequisites
8+
9+
- Caddy built with both the Caddy Sablier Plugin and Caddy docker Proxy. For this you can use the xcaddy builder.
10+
11+
```
12+
ARG CADDY_VERSION=2.10.2
13+
FROM caddy:${CADDY_VERSION}-builder AS builder
14+
15+
RUN xcaddy build \
16+
--with github.com/lucaslorentz/caddy-docker-proxy/v2 \
17+
--with github.com/sablierapp/sablier-caddy-plugin@v1.0.1 # x-release-please-version
18+
19+
FROM caddy:${CADDY_VERSION}-alpine
20+
21+
COPY --from=builder /usr/bin/caddy /usr/bin/caddy
22+
23+
CMD ["caddy", "docker-proxy"]
24+
```
25+
- Caddy and Sablier installed and properly configured.
26+
- Having Caddy, Sablier and the service you want to manage with them in the same Docker network (or accessible to each other some other way).
27+
28+
### Caddy Docker Proxy Configuration
29+
30+
By default this plugin will only check for labels on running containers which causes an issue because Sablier will stop them making Caddy lose the reverse proxy settings for that container. That makes your service inaccessible so Sablier cannot start it back up when you try to access it.
31+
32+
The fix is simple, add the following env variable to your Caddy container:
33+
```
34+
CADDY_DOCKER_SCAN_STOPPED_CONTAINERS=true
35+
```
36+
Example compose file for this:
37+
```yaml
38+
services:
39+
caddy:
40+
image: caddy-plugins
41+
restart: unless-stopped
42+
ports:
43+
- "80:80"
44+
- "443:443"
45+
- "443:443/udp"
46+
volumes:
47+
- ./site:/srv
48+
- /var/run/docker.sock:/var/run/docker.sock
49+
- caddy_data:/data
50+
- ./logs:/logs
51+
environment:
52+
- CADDY_INGRESS_NETWORKS=caddy
53+
- CADDY_DOCKER_SCAN_STOPPED_CONTAINERS=true
54+
networks:
55+
- caddy
56+
extra_hosts:
57+
- host.docker.internal:host-gateway
58+
# This is important to avoid errors later on.
59+
labels:
60+
caddy.order: sablier before reverse_proxy
61+
62+
63+
sablier:
64+
image: sablierapp/sablier:1.11.1 # x-release-please-version
65+
container_name: sablier
66+
restart: unless-stopped
67+
depends_on:
68+
- caddy
69+
command:
70+
- start
71+
- --provider.name=docker
72+
volumes:
73+
- '/var/run/docker.sock:/var/run/docker.sock'
74+
networks:
75+
- caddy
76+
77+
networks:
78+
caddy:
79+
external: true
80+
81+
volumes:
82+
caddy_data:
83+
```
84+
**_NOTE:_** Since the `caddy` network is external, you need to create it first by running `$ docker network create caddy`.
85+
86+
then start the compose stack with:
87+
88+
```bash
89+
docker compose up -d
90+
```
91+
### Adding a Service
92+
93+
Now that you have Caddy and Sablier running, adding a new service is easy and requires no editing of the Caddyfile. For example:
94+
```yaml
95+
services:
96+
mimic:
97+
image: sablierapp/mimic:v0.3.1
98+
healthcheck:
99+
test: [ "CMD", "/mimic", "healthcheck"]
100+
interval: 5s
101+
labels:
102+
sablier.enable: true
103+
sablier.group: mimic
104+
caddy: mimic.example.com
105+
caddy.reverse_proxy: "{{upstreams 80}}"
106+
caddy.sablier: "http://sablier:10000"
107+
caddy.sablier.group: mimic
108+
caddy.sablier.session_duration: 10m
109+
caddy.sablier.dynamic:
110+
networks:
111+
- caddy
112+
113+
networks:
114+
caddy:
115+
external: true
116+
117+
118+
```
119+
120+
Now you can open your browser and access (no need to reload Caddy):
121+
```
122+
https://mimic.example.com
123+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# This is an example compose file with all services together, it is preferable to have only sablier
2+
# and caddy in the same compose then separate files for your services when using caddy docker proxy.
3+
4+
services:
5+
caddy:
6+
image: caddy-plugins
7+
build:
8+
context: .
9+
dockerfile: Dockerfile
10+
restart: unless-stopped
11+
ports:
12+
- "80:80"
13+
- "443:443"
14+
- "443:443/udp"
15+
volumes:
16+
- ./site:/srv
17+
- /var/run/docker.sock:/var/run/docker.sock
18+
- caddy_data:/data
19+
- ./logs:/logs
20+
environment:
21+
- CADDY_DOCKER_SCAN_STOPPED_CONTAINERS=true
22+
# This is important to avoid errors later on.
23+
labels:
24+
caddy.order: sablier before reverse_proxy
25+
26+
sablier:
27+
image: sablierapp/sablier:1.10.1
28+
restart: unless-stopped
29+
command:
30+
- start
31+
- --provider.name=docker
32+
volumes:
33+
- '/var/run/docker.sock:/var/run/docker.sock'
34+
35+
mimic:
36+
image: sablierapp/mimic:v0.3.1
37+
healthcheck:
38+
test: [ "CMD", "/mimic", "healthcheck"]
39+
interval: 5s
40+
labels:
41+
sablier.enable: true
42+
sablier.group: mimic
43+
caddy: :80
44+
caddy.route: /*
45+
caddy.route.sablier: "http://sablier:10000"
46+
caddy.route.sablier.group: mimic
47+
caddy.route.sablier.session_duration: 1m
48+
caddy.route.sablier.dynamic.display_name: Mimic
49+
caddy.route.sablier.dynamic.theme: hacker-terminal
50+
caddy.route.reverse_proxy: mimic:80

0 commit comments

Comments
 (0)