Skip to content
e-dwayPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Custom Caddy Docker Image with TCP and HTTP/HTTPS Proxy Support

This repository contains a custom Caddy Docker image that supports both TCP and HTTP/HTTPS proxying with Docker Swarm label-based configuration.

Features

  • 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

Building the Image

Option 1: Latest Versions (Recommended)

Uses Go 1.24 and the latest module versions:

docker build -t myimage/caddy-proxy .

Option 2: Compatible Versions (Fallback)

If you encounter Go version compatibility issues, use the simple Dockerfile:

docker build -f Dockerfile.simple -t myimage/caddy-proxy .
cp Caddyfile.simple Caddyfile

Note: 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.

Usage with Docker Compose

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-165633

Label Configuration

Basic HTTP/HTTPS Proxy

labels:
  caddy: example.com
  caddy.reverse_proxy: '{{upstreams 8080}}'

TCP Proxy

labels:
  caddy: tcp.example.com
  caddy.reverse_proxy: tcp://:3306

HTTPS with Internal TLS

labels:
  caddy: secure.example.com
  caddy.reverse_proxy: '{{upstreams 8080}}'
  caddy.tls_internal_ask: 'true'

Multiple Routes

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'

Required Docker Networks

Make sure to create the caddy network:

docker network create caddy

Deployment

Local Development

  1. Build the image:

    ./build.sh
  2. Deploy with Docker Compose:

    docker-compose up -d
  3. Test the setup:

    ./test.sh

Production Deployment

Manual Deployment

For Docker Swarm:

# Initialize swarm if not already done
docker swarm init

# Deploy using the deployment script
./deploy.sh

CI/CD Pipeline

The repository includes a GitHub Actions workflow that:

  1. Tests: Validates Caddy configuration and runs container tests
  2. Builds: Creates and pushes Docker images to Docker Hub
  3. Deploys: Automatically deploys to Swarmpit-managed environments

Required Secrets:

  • DOCKER_USERNAME: Docker Hub username
  • DOCKER_PASSWORD: Docker Hub password
  • SWARMPIT_API_URL: Swarmpit API endpoint (optional)
  • SWARMPIT_TOKEN: Swarmpit authentication token (optional)

Deployment Triggers:

  • main branch: Deploys to production
  • development branch: Deploys to development environment
  • Feature branches: Runs tests only

Environment Variables for Deployment

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

Modules Included

  • caddy-docker-proxy: Enables Docker label-based configuration
  • caddy-l4: Provides Layer 4 (TCP/UDP) proxy capabilities

Ports

  • 80: HTTP traffic
  • 443: HTTPS traffic
  • 2019: Caddy admin API
  • 7102: Example TCP proxy port

Configuration Files

  • Dockerfile: Multi-stage build with required Caddy modules
  • Caddyfile: Base configuration with Docker proxy enabled
  • docker-compose.yml: Example deployment configuration

Notes

  • 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_ask is used

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages