This repository demonstrates a practical implementation of Traefik as a reverse proxy with Docker, featuring automatic SSL certificate management through Let's Encrypt and Cloudflare integration.
Traefik is a modern HTTP reverse proxy and load balancer that makes deploying microservices easy. This setup includes:
- Automatic SSL certificate generation and renewal
- Docker integration
- Cloudflare DNS integration
- Secure headers configuration
- HTTP to HTTPS redirection
- Docker and Docker Compose installed
- A domain name configured with Cloudflare
- Cloudflare API credentials
traefik-reverse-proxy/
├── traefik-config/
│ ├── config/
│ │ ├── traefik.yml
│ │ ├── config.yml
│ │ └── acme.json
│ └── docker-compose.yml
└── docker-compose.yml
-
First, create the required network for Docker:
docker network create backend
-
Set up the ACME (Let's Encrypt) configuration:
touch traefik-config/config/acme.json chmod 600 traefik-config/config/acme.json
-
Configure Cloudflare credentials:
- Open
traefik-config/docker-compose.yml
- Update the following environment variables:
CF_API_EMAIL: your_email@example.com CF_API_KEY: your_api_key # or CF_DNS_API_TOKEN: your_dns_api_token
- Open
-
Start the Traefik reverse proxy:
docker compose -f traefik-config/docker-compose.yml up -d docker compose -f traefik-config/docker-compose.yml logs -f
-
Deploy your application:
docker compose up -d docker compose logs -f
The main application is configured with Traefik labels for:
- Automatic HTTPS redirection
- SSL certificate management
- Secure headers
- Load balancing
The Traefik service is configured with:
- Port mappings (80, 443)
- Docker socket access
- SSL certificate management
- Cloudflare integration
- Important: Before using this setup, make sure to change the email address in
traefik-config/config/traefik.yml
to your own email address. This email is used for Let's Encrypt SSL certificate notifications.
Traefik offers several advantages over Nginx:
- Automatic Service Discovery: Traefik automatically detects new services and creates routes without manual configuration.
- Dynamic Configuration: No need to reload the proxy when configuration changes.
- Modern Dashboard: Built-in web UI for monitoring and management.
- Docker-First: Native Docker integration with automatic container discovery.
- Let's Encrypt Integration: Built-in automatic SSL certificate management.
- Middleware Support: Easy to add headers, authentication, and other middleware.
- Real-Time Configuration: Changes take effect immediately without service interruption.
- The
acme.json
file permissions are set to 600 to ensure only the owner can read/write - Secure headers are enabled by default
- HTTP to HTTPS redirection is enforced
- No new privileges are allowed for the container
To add more services, follow the pattern in the main docker-compose.yml
:
- Add your service configuration
- Configure appropriate Traefik labels
- Connect to the
backend
network
-
Check Traefik logs:
docker logs traefik
-
Verify network connectivity:
docker network inspect backend
-
Ensure all containers are running:
docker ps
Feel free to submit issues and enhancement requests!