# Traefik + Cert Manager + Dashboard

> Modern reverse proxy with automatic HTTPS.

- Category: Web Servers & Reverse Proxies
- Difficulty: intermediate
- Components: traefik, whoami
- Tags: traefik, reverse-proxy, https, letsencrypt, load-balancer
- Source: https://docker.recipes/web-servers/traefik-complete

## Overview

Traefik is a cloud-native edge router and HTTP reverse proxy that revolutionized service discovery with its automatic configuration capabilities. Born from the need to simplify microservices networking, Traefik eliminates the traditional pain points of manual load balancer configuration by dynamically discovering services from Docker containers, Kubernetes clusters, and other orchestrators. Unlike traditional reverse proxies that require static configuration files, Traefik reads service metadata directly from your infrastructure and automatically routes traffic accordingly.

This stack combines Traefik's automatic service discovery with integrated Let's Encrypt certificate management and a real-time monitoring dashboard. The Traefik instance automatically detects new Docker containers through Docker socket access, provisions SSL certificates via ACME HTTP challenges, and provides instant visibility into traffic patterns through its built-in web interface. The whoami service demonstrates Traefik's label-based routing system, showing how containers can self-register their routing rules through Docker labels.

This configuration is ideal for developers and DevOps teams managing multiple web applications who want automatic HTTPS without certificate management overhead. The combination of service discovery, certificate automation, and traffic monitoring makes this stack particularly valuable for dynamic environments where services frequently start, stop, or scale. Organizations running microservices, development platforms, or multi-tenant applications benefit from Traefik's ability to handle routing complexity while maintaining security through automatic certificate renewal.

## Features

- Automatic Docker container discovery through label-based service registration
- Integrated Let's Encrypt ACME certificate provisioning with HTTP challenge validation
- Real-time traffic monitoring dashboard with request metrics and backend health status
- Dynamic routing configuration without service restarts or manual configuration reloads
- HTTP to HTTPS automatic redirect capability for enhanced security
- Multi-protocol support including HTTP/2, WebSocket, and gRPC traffic handling
- Built-in load balancing with health checks for container-based services
- Middleware system for authentication, rate limiting, and request transformation

## Use cases

- Development teams running multiple web applications requiring individual SSL certificates
- Staging environments where services dynamically scale up and down based on testing needs
- Small to medium businesses hosting multiple client websites on a single server
- Microservices architectures requiring intelligent traffic routing between container services
- Home lab enthusiasts managing self-hosted applications with professional-grade reverse proxy
- Agencies managing multiple client applications requiring separate domains and certificates
- DevOps teams implementing blue-green deployments with traffic switching capabilities

## Prerequisites

- Docker Engine 20.10+ with Docker Compose v2 support
- Minimum 512MB RAM available for Traefik instance and certificate management
- Public domain name with DNS A records pointing to your server for certificate validation
- Ports 80, 443, and 8080 available and not conflicting with existing services
- Valid email address for Let's Encrypt certificate registration and notifications
- Basic understanding of Docker labels and container networking concepts

## docker-compose.yml

```yaml
services:
  traefik:
    image: traefik:latest
    command:
      - --api.dashboard=true
      - --api.insecure=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80
      - --entrypoints.websecure.address=:443
      - --certificatesresolvers.letsencrypt.acme.email=${ACME_EMAIL}
      - --certificatesresolvers.letsencrypt.acme.storage=/letsencrypt/acme.json
      - --certificatesresolvers.letsencrypt.acme.httpchallenge.entrypoint=web
      - --log.level=INFO
      - --accesslog=true
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - traefik-letsencrypt:/letsencrypt
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    networks:
      - traefik-network
    restart: unless-stopped

  whoami:
    image: traefik/whoami
    labels:
      - traefik.enable=true
      - traefik.http.routers.whoami.rule=Host(`whoami.localhost`)
      - traefik.http.routers.whoami.entrypoints=web
    networks:
      - traefik-network
    restart: unless-stopped

volumes:
  traefik-letsencrypt:

networks:
  traefik-network:
    driver: bridge
```

## .env

```bash
# Traefik
ACME_EMAIL=admin@example.com

# Dashboard at http://localhost:8080
# Use labels on containers for routing
```

## Usage notes

- Dashboard at http://localhost:8080
- Add labels to containers
- Automatic HTTPS via Let's Encrypt
- Docker provider auto-discovery
- HTTP to HTTPS redirect available

## Troubleshooting

- Certificate generation fails with 'no such host' error: Verify DNS A record points to server and port 80 is accessible from internet
- Dashboard shows 404 for services: Check that target containers have 'traefik.enable=true' label and are on the same Docker network
- Let's Encrypt rate limit exceeded: Use staging server during testing by adding '--certificatesresolvers.letsencrypt.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory'
- Services not auto-discovered: Ensure Docker socket mount is present and Traefik container has access to /var/run/docker.sock
- SSL certificate not applying: Verify ACME_EMAIL environment variable is set and certificate resolver name matches in service labels
- Dashboard inaccessible on port 8080: Check for port conflicts and ensure '--api.insecure=true' flag is present in Traefik command

