|
| 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 | +``` |
0 commit comments