Caddy Proxy Manager Sidecar
The sidecar automates synchronization between Docker containers and proxy hosts in Caddy Proxy Manager (CPM). It runs as a long-lived process: it first synchronizes already running containers and then listens for Docker events.
Features · Requirements · Getting started · Configuration · Usage · Architecture · Limitations · Documentation · Credits · License
Features
Synchronizes running Docker containers with Caddy Proxy Manager using container labels and Docker lifecycle events.
Requirements
- Docker Engine with Docker Compose.
- A running Caddy Proxy Manager instance and API token.
- An existing Docker network shared by CPM, the sidecar, and proxied services.
Getting started
The docker-compose.yml file builds the sidecar image and runs it with the restart: unless-stopped policy. The container receives:
- read-only access to the Docker socket for reading containers and events,
- the
main.pyfile mounted as read-only, - the external
caddy-proxy-manager_caddy-networknetwork to connect to CPM.
Before starting the sidecar, create a local environment file from the example and set the CPM API token:
cp .env.example .env
Edit .env with the URL, token, domain suffixes, and Docker network name used by your installation. The .env file is excluded from version control; commit .env.example instead.
Start the sidecar:
docker compose up -d --build
View logs:
docker compose logs -f cpm-sidecar
Configuration
Environment variables
| Variable | Default | Description |
|---|---|---|
CPM_API_URL |
http://cpm:3000/api/v1 |
Base URL of the CPM API. |
CPM_API_TOKEN |
none | Bearer token required to communicate with CPM. |
DOCKER_SOCK |
unix://var/run/docker.sock |
Docker API socket address. |
LOG_LEVEL |
INFO |
Log level, for example DEBUG, INFO, or WARNING. |
CPM_BASE_DOMAIN |
.local.labus.uk |
Domain suffix for configurations using cpm.port. |
CPM_WAN_DOMAIN |
.labus.uk |
Alias suffix added by cpm.nat=true. |
CPM_NETWORK_NAME |
caddy-proxy-manager_caddy-network |
Existing Docker network shared with CPM. |
Docker labels
A minimal configuration can use the cpm.port shorthand:
services:
app:
image: example/app:latest
labels:
cpm.enable: "true"
cpm.port: "8080"
For this configuration:
- the proxy host name is
cpm.nameor the container name, - the domain is
<name><CPM_BASE_DOMAIN>, - the upstream is
<name>:<cpm.port>.
⚠️ IMPORTANT: When using the
cpm.portshorthand, the upstream hostname becomes exactly what is defined incpm.name(or the container name ifcpm.nameis missing). If you overridecpm.nameto be different from the actual container name (e.g.cpm.name=newsblurbut the container isnewsblur_haproxy), Caddy will try to route traffic tonewsblur:<cpm.port>, which will fail because Docker DNS cannot resolve it. In such cases, you must use the full configuration with an explicitly definedcpm.upstream.
A full configuration can specify the domain and upstream directly:
services:
app:
image: example/app:latest
labels:
cpm.enable: "true"
cpm.name: "my-app"
cpm.domain: "app.example.com,app.internal.example.com"
cpm.aliases: "app-alias.example.com"
cpm.upstream: "app:8080"
cpm.tls_insecure_skip_verify: "false"
cpm.nat: "false"
Usage
Simplified configuration
Use cpm.port when the proxy host should use the default local domain and the container name as the upstream hostname:
services:
radiosync:
image: example/radiosync:latest
labels:
- "cpm.enable=true"
- "cpm.name=radiosync"
- "cpm.port=8000"
networks:
- cpm_network
- default
networks:
cpm_network:
external: true
name: caddy-proxy-manager_caddy-network
This creates a proxy host named radiosync for radiosync.local.labus.uk and forwards traffic to radiosync:8000.
Full configuration
Use explicit domains and an upstream when the service needs a custom hostname or an alias:
services:
immich_server:
image: example/immich-server:latest
labels:
- "cpm.enable=true"
- "cpm.domain=immich.local.labus.uk"
- "cpm.aliases=immich.labus.uk"
- "cpm.upstream=immich_server:2283"
- "cpm.tls_insecure_skip_verify=false"
networks:
- cpm_network
- default
networks:
cpm_network:
external: true
name: caddy-proxy-manager_caddy-network
This creates a proxy host for immich.local.labus.uk with immich.labus.uk as an alias and forwards traffic to immich_server:2283. DNS provider and certificate settings are managed separately in CPM.
Available labels:
| Label | Meaning |
|---|---|
cpm.enable |
Enables container handling; must be set to true. |
cpm.name |
Proxy host name; defaults to the container name. |
cpm.port |
Upstream port used by the shorthand configuration. |
cpm.domain |
One or more comma-separated domains. Required when cpm.port is not provided. |
cpm.aliases |
Additional comma-separated domains. |
cpm.upstream |
Upstream address, for example app:8080. Required when cpm.port is not provided. |
cpm.tls_insecure_skip_verify |
Sets skipHttpsHostnameValidation=true. Defaults to false. |
cpm.nat |
Adds the <name><CPM_WAN_DOMAIN> alias. |
Domain and alias values are trimmed, and duplicates are removed while preserving their original order.
Architecture
- On startup, it checks whether
CPM_API_TOKENis set. - It connects to Docker through the socket specified by
DOCKER_SOCKand runs apingcheck. - It retrieves all running containers and processes each one.
- It listens for container events:
startcreates a missing proxy host or updates an existing one,dieanddestroydisable the corresponding proxy host in CPM.
- HTTP connection errors are retried up to five times for
502,503, and504responses, with increasing delays.
Containers without the cpm.enable=true label are ignored. When a container stops, the sidecar does not delete its CPM host; it sets enabled=false, keeping the configuration available in the CPM interface.
Limitations
- Each API request has a 10-second timeout. Transient gateway errors (
502,503, and504) are retried up to five times with backoff. - If an API operation still fails, the error is logged and the sidecar continues processing other containers and events. This keeps the Docker event listener alive while CPM is temporarily unavailable.
- The Docker socket is mounted read-only because the sidecar needs Docker API access to list containers and receive lifecycle events. For a stricter deployment, place a Docker socket proxy between the sidecar and Docker, and allow only the required read and event endpoints.
- The image uses
watchmedo auto-restart, so changes to the mountedmain.pycause the script to restart.
Documentation
Credits
The README was created with assistance from GitHub Copilot. The scripts were created with assistance from Gemini Antigravity.
License
No project-level license file is currently included in this repository. Third-party components retain their own licenses.