2026-09-03 16:22:14 +02:00
2026-09-03 16:22:14 +02:00
2026-09-03 16:22:14 +02:00
2026-09-03 16:22:14 +02:00
2026-09-03 16:22:14 +02:00
2026-09-03 16:22:14 +02:00

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.py file mounted as read-only,
  • the external caddy-proxy-manager_caddy-network network 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.name or the container name,
  • the domain is <name><CPM_BASE_DOMAIN>,
  • the upstream is <name>:<cpm.port>.

⚠️ IMPORTANT: When using the cpm.port shorthand, the upstream hostname becomes exactly what is defined in cpm.name (or the container name if cpm.name is missing). If you override cpm.name to be different from the actual container name (e.g. cpm.name=newsblur but the container is newsblur_haproxy), Caddy will try to route traffic to newsblur:<cpm.port>, which will fail because Docker DNS cannot resolve it. In such cases, you must use the full configuration with an explicitly defined cpm.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

  1. On startup, it checks whether CPM_API_TOKEN is set.
  2. It connects to Docker through the socket specified by DOCKER_SOCK and runs a ping check.
  3. It retrieves all running containers and processes each one.
  4. It listens for container events:
    • start creates a missing proxy host or updates an existing one,
    • die and destroy disable the corresponding proxy host in CPM.
  5. HTTP connection errors are retried up to five times for 502, 503, and 504 responses, 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, and 504) 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 mounted main.py cause the script to restart.

Documentation

README style guide.

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.

S
Description
No description provided
Readme 41 KiB
Languages
Python 98%
Dockerfile 2%