Traefik reverse proxy for Docker (production guide)
A clean Traefik v3 setup that discovers your containers by label, issues Let's Encrypt certificates automatically, keeps the dashboard private, and ships with security middlewares enabled by default.
When Traefik beats Nginx
If you have one app, Nginx is simpler. If you have three or more apps on the same Docker host and add new ones every few months, Traefik wins on:
- Zero-config certificates. Add a label, certificate gets issued; no manual
certbot --nginx -d ...per service. - Discovery via labels. Add a service to a Compose file with the right labels — Traefik picks it up on the next start.
- Dynamic config. No reloads. Add or remove services and routes change live.
- Built-in middlewares. Rate limiting, auth, redirects, retries — declared in labels, not in conf files.
For a one-app server, see our Nginx + SSL guide instead. Below assumes you're standing up Traefik for a multi-service host.
The setup
Pick a hostname (traefik.example.com) and create the Compose file:
mkdir -p ~/traefik && cd ~/traefik
mkdir -p letsencrypt
touch letsencrypt/acme.json
chmod 600 letsencrypt/acme.json
nano docker-compose.yml
name: traefik
networks:
proxy:
name: proxy
services:
traefik:
image: traefik:v3.1.5
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./letsencrypt:/letsencrypt
networks:
- proxy
command:
# Providers
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--providers.docker.network=proxy"
# Entrypoints
- "--entrypoints.web.address=:80"
- "--entrypoints.web.http.redirections.entrypoint.to=websecure"
- "--entrypoints.web.http.redirections.entrypoint.scheme=https"
- "--entrypoints.websecure.address=:443"
- "--entrypoints.websecure.http.tls.certresolver=letsencrypt"
# Let's Encrypt
- "--certificatesresolvers.letsencrypt.acme.email=you@example.com"
- "--certificatesresolvers.letsencrypt.acme.storage=/letsencrypt/acme.json"
- "--certificatesresolvers.letsencrypt.acme.httpchallenge=true"
- "--certificatesresolvers.letsencrypt.acme.httpchallenge.entrypoint=web"
# Dashboard
- "--api.dashboard=true"
# Logs
- "--log.level=INFO"
- "--accesslog=true"
- "--accesslog.bufferingsize=100"
labels:
# Dashboard route — locked behind basic auth
- "traefik.enable=true"
- "traefik.http.routers.traefik.rule=Host(`traefik.example.com`)"
- "traefik.http.routers.traefik.entrypoints=websecure"
- "traefik.http.routers.traefik.service=api@internal"
- "traefik.http.routers.traefik.middlewares=traefik-auth"
- "traefik.http.middlewares.traefik-auth.basicauth.users=admin:$$apr1$$xyzREPLACEME$$abc"
Generate the dashboard password
sudo apt install -y apache2-utils
htpasswd -nB admin
# Paste username/hash into the basicauth.users label.
# Important: in a Compose YAML, escape every $ as $$ — otherwise Compose
# interprets it as a variable reference and the hash is silently broken.
Bring it up
docker compose up -d
docker compose logs -f traefik
You should see Traefik claim ports 80/443 and then provision a certificate for traefik.example.com over the HTTP-01 challenge. Visit https://traefik.example.com/dashboard/ (trailing slash required), authenticate with the basic-auth user, and you're in.
Adding a service: the label pattern
Any container on the proxy network can opt in to Traefik by adding labels. Example with a generic web app:
services:
myapp:
image: ghcr.io/example/myapp:1.2.3
restart: unless-stopped
networks:
- proxy
labels:
- "traefik.enable=true"
- "traefik.http.routers.myapp.rule=Host(`app.example.com`)"
- "traefik.http.routers.myapp.entrypoints=websecure"
- "traefik.http.routers.myapp.tls.certresolver=letsencrypt"
- "traefik.http.services.myapp.loadbalancer.server.port=8080"
networks:
proxy:
external: true
Three things make this work:
- The service is on the
proxynetwork (declaredexternal: truebecause it was created by Traefik's project). traefik.enable=trueopts in (because we setexposedbydefault=false).loadbalancer.server.porttells Traefik which internal port to hit — it doesn't infer fromEXPOSEreliably.
Bring the app up. The certificate is issued automatically the first time someone visits https://app.example.com/.
Security middlewares (use these by default)
Declare middlewares once on Traefik's own service and reference by name on every app:
labels:
- "traefik.enable=true"
# ... dashboard labels above ...
# Reusable middlewares
- "traefik.http.middlewares.sec-headers.headers.stsSeconds=31536000"
- "traefik.http.middlewares.sec-headers.headers.stsIncludeSubdomains=true"
- "traefik.http.middlewares.sec-headers.headers.contentTypeNosniff=true"
- "traefik.http.middlewares.sec-headers.headers.referrerPolicy=no-referrer"
- "traefik.http.middlewares.sec-headers.headers.frameDeny=true"
- "traefik.http.middlewares.rate-limit.ratelimit.average=100"
- "traefik.http.middlewares.rate-limit.ratelimit.burst=200"
On an app router, chain them:
- "traefik.http.routers.myapp.middlewares=sec-headers,rate-limit"
WebSockets and large payloads
WebSockets work out of the box in Traefik — no equivalent of Nginx's Upgrade dance needed. For large uploads, raise the body-size limit at the entrypoint:
- "--entrypoints.websecure.http.maxRequestBodyBytes=52428800" # 50 MB
The acme.json file
Traefik stores all issued certificates in /letsencrypt/acme.json. Two non-negotiable things:
- Must be mode 600. If it isn't, Traefik refuses to start and the log says so cryptically.
- Back it up. Let's Encrypt rate-limits new certificate issuance to 50 per registered domain per week. If you nuke the file and your stack has 60 services, you've a wall to climb.
Logs and observability
The access log writes to stdout by default (json with accesslog.format=json). Pipe it to whatever you use (Loki, journald, an external log host). For debugging a misbehaving route, raise --log.level=DEBUG temporarily — it's chatty.
Common Traefik mistakes
"Gateway timeout" on every request
The service isn't on the proxy network. Add networks: [proxy] on the app and declare proxy as external: true.
404 from Traefik even though the labels look right
You set traefik.enable=true on the wrong service in a multi-service compose file, or you forgot to set it at all because you assumed defaults. With exposedbydefault=false, opt-in is required.
Certificate issuance fails: "Connection refused"
Port 80 is not actually reachable from the public internet. Traefik needs 80 open for the HTTP-01 challenge. Check the firewall and the DNS A record.
Dashboard returns "401 unauthorized" even with the right password
You forgot to escape $ as $$ in the basicauth label. Compose ate every $ as a variable reference, the stored hash is wrong, and the login silently fails.
Wildcard certificates not issued
HTTP-01 cannot issue wildcards. You need DNS-01 with a provider plugin (Cloudflare, Route53, etc.). The compose command: changes accordingly.
Multi-service VPS for a Traefik stack?
Traefik + 5-10 small services fits comfortably on a 4 GB Inferno plan. The 10 Gbps network keeps WebSocket-heavy apps responsive under burst load.
What to do next
- Prefer simplicity? Caddy is similar with smaller config.
- Apply Docker security hardening to the apps Traefik exposes.
- Watchtower can keep your containers up to date — pair carefully.
- Uptime Kuma to monitor each route Traefik serves.
Author: Hanna Wagner — backend dev, Frankfurt. Last tested: 2026-05-22 on Traefik v3.1.5, Docker CE 27.3, Ubuntu 22.04.