When to use this stack
Traefik is a reverse proxy designed for Docker. It watches the Docker API for containers and builds its routes from labels you add to each service, so adding a new website means starting a container with a few labels instead of editing a central file. This stack adds Let’s Encrypt support to Traefik: it requests a certificate for your domain, answers the HTTP challenge on port 80, redirects every plain HTTP request to HTTPS, and renews the certificate before it expires.
Use this stack when you run several containers on one server and want each one published under its own hostname with valid TLS. It suits small teams, personal servers and VPS setups where you want certificates without touching a web server configuration. Do not use it when you only have one static site, because the Caddy stack in this series is shorter. Do not use it on a host that cannot receive traffic on ports 80 and 443 from the internet, because the default certificate method cannot complete from a private network. For wildcard certificates you need the DNS challenge, which requires an API token for your DNS provider; that is covered in the variations.
docker-compose.yml
services:
traefik:
image: traefik:v3.1
restart: unless-stopped
command:
- --providers.docker=true
- --providers.docker.exposedbydefault=false
- --entrypoints.web.address=:80
- --entrypoints.websecure.address=:443
- --entrypoints.web.http.redirections.entrypoint.to=websecure
- --entrypoints.web.http.redirections.entrypoint.scheme=https
- --certificatesresolvers.letsencrypt.acme.email=${ACME_EMAIL}
- --certificatesresolvers.letsencrypt.acme.storage=/letsencrypt/acme.json
- --certificatesresolvers.letsencrypt.acme.httpchallenge=true
- --certificatesresolvers.letsencrypt.acme.httpchallenge.entrypoint=web
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- traefik_letsencrypt:/letsencrypt
whoami:
image: traefik/whoami:v1.10
restart: unless-stopped
labels:
- traefik.enable=true
- traefik.http.routers.whoami.rule=Host(`${APP_DOMAIN}`)
- traefik.http.routers.whoami.entrypoints=websecure
- traefik.http.routers.whoami.tls.certresolver=letsencrypt
- traefik.http.services.whoami.loadbalancer.server.port=80
volumes:
traefik_letsencrypt:# Email for Let's Encrypt expiry notices and account registration
ACME_EMAIL=[email protected]
# Public hostname that resolves to this server, for example app.example.com
APP_DOMAIN=CHANGE_ME_app.example.com.env
# Email for Let's Encrypt expiry notices and account registration
ACME_EMAIL=[email protected]
# Public hostname that resolves to this server, for example app.example.com
APP_DOMAIN=CHANGE_ME_app.example.comStart and verify
mkdir -p ~/traefik-stack && cd ~/traefik-stack
# Save the two blocks above as docker-compose.yml and .env
# Validate the file and the .env interpolation
docker compose config --quiet
# Start, then check the containers and the certificate log
docker compose up -d
docker compose ps
docker compose logs --tail=50 traefik
# Verify: HTTP must redirect to HTTPS, HTTPS must answer
source .env
curl -sI "http://${APP_DOMAIN}" | head -n 3
curl -sI "https://${APP_DOMAIN}" | head -n 1
curl -s "https://${APP_DOMAIN}" | head -n 5Production checklist
- Create the DNS record for APP_DOMAIN before the first start, and open ports 80 and 443 in both the host firewall and any cloud firewall.
- Keep
exposedbydefault=false, so only containers withtraefik.enable=trueare published. - The Docker socket is mounted read-only, but read access to the Docker API still exposes secrets and lets anyone who compromises Traefik learn about every container. For production, put a socket proxy such as tecnativa/docker-socket-proxy between Traefik and the socket, and allow only the read endpoints Traefik needs.
- Keep the dashboard off on the public internet. If you enable it, protect it with a basic-auth middleware and do not publish its port.
- Use the Let’s Encrypt staging endpoint while testing (
--certificatesresolvers.letsencrypt.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory) to avoid production rate limits, then remove that line. - Back up the traefik_letsencrypt volume. The acme.json file contains the private keys of your certificates.
- Add HSTS and other security headers with a Traefik middleware label on each public router.
- Pin traefik:v3.1 and upgrade on purpose, because v2 and v3 use different configuration syntax.
Common problems
- Symptom: Traefik answers with its default 404 page. Cause: The container lacks
traefik.enable=true, the Host rule does not match the URL, or the port label points at the wrong container port. Fix: Rundocker compose configand compare the labels with the hostname you typed. - Symptom: The log says the certificate could not be obtained, or the challenge times out. Cause: DNS does not resolve to this server yet, port 80 is blocked, or a proxy in front of the server rewrites the request. Fix: Check the A record with
dig +short yourdomainand test port 80 from outside your network. - Symptom: Traefik repeatedly fails with “too many certificates already issued”. Cause: The acme.json file was deleted and the stack restarted many times. Fix: Use the staging endpoint while debugging, and keep the volume between restarts.
- Symptom: Traefik logs that acme.json permissions are too open. Cause: The file was created manually with permissions such as 0644. Fix: Delete acme.json inside the volume and let Traefik create it again, since it writes the file with mode 600.
- Symptom: A router is defined but returns 502 or no route. Cause: The container is on a different Docker network that Traefik cannot reach. Fix: Attach both services to a shared network or add the
traefik.docker.networklabel.
Variations
- Wildcard certificates with the DNS challenge: Replace the httpchallenge flags with a
dnschallengeprovider for your DNS host and pass its API token through .env. This is the only way to get a wildcard certificate with Let’s Encrypt. - File provider for fixed routes: Mount a dynamic YAML configuration for routes that are not containers, such as a router for a host service on the same machine.
- Socket proxy in front of Docker: Run a socket proxy service and point Traefik at tcp://socket-proxy:2375 instead of mounting the raw socket, which narrows what Traefik can do with the Docker API.
Questions
Do I need a domain name?
Yes. Let's Encrypt only issues certificates for names that resolve to your server. Create an A record for APP_DOMAIN before you start the stack.
Why must port 80 stay open if I only want HTTPS?
The HTTP-01 challenge is answered on port 80, and the redirect from HTTP to HTTPS also runs there. Closing port 80 breaks both certificate issuance and renewal.
Where are the certificates stored?
Traefik keeps them in acme.json inside the traefik_letsencrypt volume, together with the private keys. Back up that volume.
How do I add a second website?
Start another container on the same Docker network and add the same kind of labels with a different Host rule and router name.
Adapt this stack
Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.
Open in builderRelated stacks
Caddy 2 Reverse Proxy + Static Web Server
Caddy 2 in Docker Compose: serve a static site, proxy /api to a backend and get automatic HTTPS on a real domain. Pinned images and .env configuration.
Gitea Git Server with PostgreSQL 16
Self-hosted Gitea with PostgreSQL 16 in Docker Compose: web Git hosting, SSH on port 2222, a healthcheck-gated database start and persistent volumes.
Loki, Promtail and Grafana Log Stack
Collect Linux host logs with Loki and Promtail in Docker Compose and query them in Grafana. Pinned images, persistent volumes and a localhost-only Grafana port.
Portainer CE Web UI for Docker Management
Run Portainer CE in Docker Compose as a web UI for containers, images and volumes. Bound to localhost on HTTPS 9443, with persistent data and a pinned image.
Browse more Infrastructure & Monitoring stacks or the full library.