$ show lucaslorentz/caddy-docker-proxy
This repository implements a Caddy plugin that dynamically generates Caddy reverse proxy configurations from Docker container and Swarm service labels. It runs as a Caddy module, monitoring Docker events and polling Docker APIs to produce an in-memory Caddyfile, then loading it into Caddy with zero-downtime reloads. The system supports three operational modes: standalone (controller and server in one process), controller (generates config and pushes to remote Caddy servers via admin API), and server (receives config remotely). The core architecture comprises a Docker client abstraction, a Caddyfile generator that translates Docker labels into Caddyfile blocks, and a loader that manages event-driven updates. The codebase is well-structured with clear separation of concerns, extensive unit tests, and integration test suites using Docker Compose stacks. It demonstrates solid engineering practices with modular packages, concurrent-safe state management, and thorough error handling.
The LLM identified 4 files worth highlighting for reuse:
Implements a merge algorithm for Caddyfile blocks that intelligently combines directives from multiple sources (e.g., multiple containers sharing the same site). It handles special cases for reverse_proxy and php_fastcgi by merging upstream targets and subdirectives when matchers are compatible. The merge logic is isolated in a self-contained package with no external dependencies, making it reusable for any Caddyfile generation tool that needs to aggregate configuration fragments.
Provides bidirectional marshaling between the internal Block/Container representation and the Caddyfile text format. It includes a stable sort for blocks (global options first, then snippets, then matchers, then by order/key), proper quoting of values with whitespace or special characters, and a parser (Unmarshal) that constructs the tree from tokenized input. This is a self-contained utility that can be extracted to build, manipulate, or validate Caddyfiles programmatically.
Implements a Caddyfile processor that validates server blocks against the Caddy adapter and removes any invalid blocks, logging errors. It incrementally adds blocks and tests them with the adapter, dropping those that cause parse failures. This pattern is useful for sanitizing user-provided or auto-generated Caddyfile content before loading, and can be reused in other Caddyfile-related tools to ensure only valid configurations are applied.
Provides a utility to detect the current container's ID by reading /proc/self/cgroup and /proc/self/mountinfo on Linux, falling back to hostname. It uses regex to extract the 64-character container ID from various cgroup formats (systemd, Docker, Kubernetes, ECS, rootless). This is a common but nontrivial pattern for Docker-in-Docker or container-aware applications, and the implementation is clean and tested with multiple input formats.