This repository contains a custom Caddy Docker image that supports both TCP and HTTP/HTTPS proxying with Docker Swarm label-based configuration.
- Docker Label Discovery: Automatically configures routes based on Docker service labels
- TCP Proxying: Support for Layer 4 TCP connections using caddy-l4 module
- HTTP/HTTPS Proxying: Standard reverse proxy functionality
- Automatic HTTPS: Built-in TLS certificate management
- Docker Swarm Integration: Works seamlessly with Docker Swarm deployments
Uses Go 1.24 and the latest module versions:
docker build -t myimage/caddy-proxy .If you encounter Go version compatibility issues, use the simple Dockerfile:
docker build -f Dockerfile.simple -t myimage/caddy-proxy .
cp Caddyfile.simple CaddyfileNote: The v2 docker-proxy module requires Go 1.24+. If your environment doesn't support this, use the simple version with v0.3.6 of the docker-proxy module.
The image supports the label syntax shown in your example:
dev-authcache:
image: myimage/test
networks:
- caddy
- default
logging:
driver: json-file
deploy:
labels:
caddy_0: test.mydomain.com
caddy_0.reverse_proxy: tcp://:7102
caddy_0.tls_internal_ask: 'true'
caddy_1: admin.test.mydomain.com
caddy_1.reverse_proxy: '{{upstreams 9102}}'
placement:
constraints:
- node.hostname == sd-165633labels:
caddy: example.com
caddy.reverse_proxy: '{{upstreams 8080}}'labels:
caddy: tcp.example.com
caddy.reverse_proxy: tcp://:3306labels:
caddy: secure.example.com
caddy.reverse_proxy: '{{upstreams 8080}}'
caddy.tls_internal_ask: 'true'labels:
caddy_0: api.example.com
caddy_0.reverse_proxy: '{{upstreams 8080}}'
caddy_1: admin.example.com
caddy_1.reverse_proxy: '{{upstreams 9090}}'
caddy_1.tls_internal_ask: 'true'Make sure to create the caddy network:
docker network create caddy-
Build the image:
./build.sh
-
Deploy with Docker Compose:
docker-compose up -d
-
Test the setup:
./test.sh
For Docker Swarm:
# Initialize swarm if not already done
docker swarm init
# Deploy using the deployment script
./deploy.shThe repository includes a GitHub Actions workflow that:
- Tests: Validates Caddy configuration and runs container tests
- Builds: Creates and pushes Docker images to Docker Hub
- Deploys: Automatically deploys to Swarmpit-managed environments
Required Secrets:
DOCKER_USERNAME: Docker Hub usernameDOCKER_PASSWORD: Docker Hub passwordSWARMPIT_API_URL: Swarmpit API endpoint (optional)SWARMPIT_TOKEN: Swarmpit authentication token (optional)
Deployment Triggers:
mainbranch: Deploys to productiondevelopmentbranch: Deploys to development environment- Feature branches: Runs tests only
export DOCKER_IMAGE="myimage/caddy-proxy:latest"
export STACK_NAME="caddy-stack"
export ENVIRONMENT="production"
export SWARMPIT_API_URL="https://your-swarmpit.com/api"
export SWARMPIT_TOKEN="your-token"
./deploy.sh- caddy-docker-proxy: Enables Docker label-based configuration
- caddy-l4: Provides Layer 4 (TCP/UDP) proxy capabilities
80: HTTP traffic443: HTTPS traffic2019: Caddy admin API7102: Example TCP proxy port
Dockerfile: Multi-stage build with required Caddy modulesCaddyfile: Base configuration with Docker proxy enableddocker-compose.yml: Example deployment configuration
- The Docker socket must be mounted for label discovery to work
- Services must be on the same network as Caddy for proxying
- TCP proxying requires the caddy-l4 module
- Internal TLS certificates are automatically generated when
tls_internal_askis used