Traefik on Docker: Reverse Proxy with Automatic SSL
We’ve all been there. You spin up a new container, edit a nginx.conf, reload the service, request a certificate with certbot, and three months later you discover the renewal failed and your browser is flashing a red warning for your app. It’s repetitive, fragile work—and it’s completely avoidable.
Traefik tackles this from a different angle: it watches Docker in real time, discovers your containers, and sets up routes and certificates without you touching a single config file when you deploy a new service. In this guide you’ll build a modern reverse proxy with automatic HTTPS via Let’s Encrypt, using a setup you can take straight to production.
Why Traefik fits so well with Docker
Traefik is a reverse proxy and load balancer built for dynamic environments. Its key difference from traditional solutions is the idea of providers: instead of reading a static config, it queries a source (in our case, the Docker API) and rebuilds its routes every time a container appears or disappears.
That brings some real advantages:
- Configuration lives in the container. Routing rules sit in each service’s
labels, right next to its Docker Compose definition. - No reloads. Adding or removing a service requires no proxy restart and causes no downtime.
- HTTPS built in. Traefik requests, validates, and renews Let’s Encrypt certificates all by itself.
- Dashboard. A control panel shows active routers, services, and middlewares—handy for debugging.
The trade-off is a bit of a learning curve up front: you need to understand its four pieces (entrypoints, routers, middlewares, and services). We’ll cover them as we go.
Prerequisites
Before you start, you’ll need:
- A Linux server with Docker and Docker Compose installed.
- A domain with A records pointing to the server’s public IP (for example
app.midominio.comandtraefik.midominio.com). - Ports 80 and 443 open in your firewall.
If your server is exposed to the internet, it’s worth locking it down from day one. Check out our advanced Fail2ban setup for Linux servers to block scans and brute-force attacks against the ports you’re about to publish.
Project structure
Create a working directory and set up the file where Traefik will store certificates. The 600 permission is mandatory: Traefik refuses to use the file if other users can read it.
mkdir -p ~/traefik/letsencrypt && cd ~/traefik
touch letsencrypt/acme.json
chmod 600 letsencrypt/acme.json
docker network create proxy
The proxy network is where Traefik meets the services you want to publish. Only containers connected to it are reachable from the proxy, which shrinks your attack surface.
Traefik Docker Compose with Let’s Encrypt
Here’s the base docker-compose.yml. We’re using Traefik v3 and configuring everything through command-line arguments to keep it in one readable file.
services:
traefik:
image: traefik:v3.1
container_name: traefik
restart: unless-stopped
command:
- --providers.docker=true
- --providers.docker.exposedbydefault=false
- --providers.docker.network=proxy
- --entrypoints.web.address=:80
- --entrypoints.websecure.address=:443
- --entrypoints.web.http.redirections.entrypoint.to=websecure
- --entrypoints.web.http.redirections.entrypoint.scheme=https
- --certificatesresolvers.le.acme.email=admin@midominio.com
- --certificatesresolvers.le.acme.storage=/letsencrypt/acme.json
- --certificatesresolvers.le.acme.httpchallenge.entrypoint=web
- --api.dashboard=true
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./letsencrypt:/letsencrypt
networks:
- proxy
networks:
proxy:
external: true
Note three important choices here:
exposedbydefault=false: no container is published unless you explicitly ask for it withtraefik.enable=true. That’s the safe default.- A global HTTP → HTTPS redirect set at the entrypoint, so you don’t have to repeat it on every service.
- A resolver named
lethat uses the Let’s Encrypt HTTP-01 challenge and stores certificates inacme.json.
Start the proxy with docker compose up -d and tail the logs with docker compose logs -f traefik.
Publishing a service with labels
Now for the good part. To expose an application, all you do is add labels. We’ll use whoami, a test container that returns request info:
services:
whoami:
image: traefik/whoami
restart: unless-stopped
networks:
- proxy
labels:
- traefik.enable=true
- traefik.http.routers.whoami.rule=Host(`app.midominio.com`)
- traefik.http.routers.whoami.entrypoints=websecure
- traefik.http.routers.whoami.tls.certresolver=le
- traefik.http.services.whoami.loadbalancer.server.port=80
networks:
proxy:
external: true
When you run docker compose up -d, Traefik picks up the container, creates the router, requests the certificate from Let’s Encrypt, and starts serving https://app.midominio.com. No reloads, no extra commands.
If it’s your own app, optimize the image first: Docker multi-stage builds will cut its size and deploy time—something you’ll appreciate when the proxy needs to discover new containers often.
How Traefik gets certificates
Understanding the mechanism saves you a lot of headaches. Let’s Encrypt supports three challenge types, and Traefik handles all three:
HTTP-01
This is the one we used. Let’s Encrypt hits a URL on port 80 of your domain to check that you control the server. It’s the simplest option, but it doesn’t allow wildcard certificates and requires port 80 to be reachable from the internet.
TLS-ALPN-01
This validates over port 443. It’s useful if you can’t open port 80, though it can’t issue wildcards either.
DNS-01
This validates by creating a TXT record in your DNS zone. It’s the only method that supports wildcard certificates (*.midominio.com) and it works on servers that aren’t publicly exposed. It needs credentials from your DNS provider, for example Cloudflare:
environment:
- CF_DNS_API_TOKEN=${CF_DNS_API_TOKEN}
command:
- --certificatesresolvers.le.acme.dnschallenge=true
- --certificatesresolvers.le.acme.dnschallenge.provider=cloudflare
That token shouldn’t live in a versioned .env file. If you want to do it right, see how to centralize credentials in our guide to secret management with OpenBao.
Securing the dashboard and adding middlewares
The Traefik dashboard is valuable, but publishing it without authentication is a serious mistake. Generate a hash with htpasswd (from the apache2-utils package):
echo $(htpasswd -nB admin) | sed -e 's/\$/\$\$/g'
The sed doubles the $ symbols because Compose reads them as variables. Then add these labels to the traefik service:
labels:
- traefik.enable=true
- traefik.http.routers.dashboard.rule=Host(`traefik.midominio.com`)
- traefik.http.routers.dashboard.entrypoints=websecure
- traefik.http.routers.dashboard.tls.certresolver=le
- traefik.http.routers.dashboard.service=api@internal
- traefik.http.routers.dashboard.middlewares=auth
- traefik.http.middlewares.auth.basicauth.users=admin:$$2y$$05$$hashgenerado
Middlewares are Traefik’s other big strength. They chain onto routers and let you, among other things:
- Add security headers (HSTS,
X-Frame-Options). - Rate-limit requests per IP with
ratelimit. - Restrict access with
ipallowlist. - Compress responses with
compress.
Here’s an example of reusable security headers:
- traefik.http.middlewares.secure-headers.headers.stsSeconds=31536000
- traefik.http.middlewares.secure-headers.headers.stsIncludeSubdomains=true
- traefik.http.middlewares.secure-headers.headers.contentTypeNosniff=true
If you want centralized authentication for your apps, you can hand it off with the forwardAuth middleware to an identity provider. Our Keycloak SSO deployment guide for microservices walks through setting up that piece.
The Docker socket risk
Mounting /var/run/docker.sock gives Traefik full visibility into the daemon. Even in read-only mode, if the proxy is compromised, an attacker gains very sensitive information and, depending on the setup, can escalate privileges. In production it’s best to put a docker-socket-proxy in between that exposes only the endpoints you need (read-only containers) and point Traefik at it via --providers.docker.endpoint=tcp://socket-proxy:2375. It’s a small change that cuts the risk significantly.
Common errors and how to fix them
“TRAEFIK DEFAULT CERT” certificate. This means the real certificate wasn’t issued. Check that DNS points to the right IP, that port 80 is reachable, and that the router has tls.certresolver=le.
Permission error on acme.json. If the log mentions permissions being too open, run chmod 600 letsencrypt/acme.json.
Let’s Encrypt rate limits. They enforce per-domain and weekly limits. During testing, use the staging server by adding --certificatesresolvers.le.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory. Staging certificates aren’t trusted by browsers, but they keep you from getting blocked.
502 Bad Gateway. This usually means Traefik doesn’t know which port to use, or the container isn’t on the proxy network. Always set loadbalancer.server.port and double-check the network.
Routes that don’t show up. With exposedbydefault=false, forgetting traefik.enable=true leaves the container invisible.
Production best practices
Before you call the project done, run through this list:
- Pin image versions (
traefik:v3.1) instead of usinglatest, so an update won’t break your environment. - Back up
acme.json. Losing it means reissuing every certificate, and you may hit Let’s Encrypt’s limits. - Enable access logs and ship them to your observability stack.
- Version your config. A repo with the Compose file and a deployment pipeline, like those in our CI/CD with GitHub Actions guide, gives you reproducibility and traceability.
- Separate networks. Keep databases on internal networks with no connection to
proxy.
Conclusion
Traefik turns publishing services into something declarative: you describe what you want in the labels, and the proxy handles routing, HTTPS, and renewals. With a single Compose file you get an HTTPS redirect, automatic Let’s Encrypt certificates, a protected dashboard, and a solid foundation to grow with middlewares and centralized auth.
Your next step is easy: deploy whoami on a test server with the staging server enabled, confirm the whole flow works, and once everything’s green, switch to real certificates. You’ll find that adding a new service to your infrastructure is a matter of minutes again.