Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
services:
traefik:
image: traefik:v3.5
command:
- --providers.docker=true
- --providers.docker.exposedbydefault=false
- --entrypoints.web.address=:80
- --entrypoints.web.http.redirections.entrypoint.to=websecure
- --entrypoints.web.http.redirections.entrypoint.scheme=https
- --entrypoints.websecure.address=:443
- --certificatesresolvers.le.acme.tlschallenge=true
- --certificatesresolvers.le.acme.email=ops@example.com
- --certificatesresolvers.le.acme.storage=/letsencrypt/acme.json
ports: ["80:80", "443:443"]
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- letsencrypt:/letsencrypt
restart: unless-stopped
whoami:
image: traefik/whoami
labels:
- traefik.enable=true
- traefik.http.routers.whoami.rule=Host(`whoami.example.com`)
- traefik.http.routers.whoami.entrypoints=websecure
- traefik.http.routers.whoami.tls.certresolver=le
- traefik.http.services.whoami.loadbalancer.server.port=80
restart: unless-stopped
volumes:
letsencrypt:Step by step
- DNS first —
whoami.example.commust point to the server, and ports 80/443 must be open, or Let's Encrypt cannot check the name. - Traefik (quick answer) — reads Docker through the socket (read-only), listens on 80 and 443, redirects HTTP to HTTPS, and gets certificates with the TLS challenge.
exposedbydefault=false: only containers withtraefik.enable=trueare published. - Labels on the app:
routers.NAME.rule— which requests (Host(...),PathPrefix(/api)).routers.NAME.entrypointsandtls.certresolver— HTTPS with a Let's Encrypt certificate.services.NAME.loadbalancer.server.port— the port inside the container. Noports:needed for the app.
- Add middlewares — for example basic auth for an admin tool (in Compose, write every
$of the hash as$$):
- traefik.http.middlewares.admin-auth.basicauth.users=alice:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/
- traefik.http.routers.admin.middlewares=admin-authCreate the hash with htpasswd -nb alice 'password'.
- Several networks — if the app is in another Compose project, put both on a shared Docker network and add
traefik.docker.network=proxy. - Test certificates with staging first — add
--certificatesresolvers.le.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directorywhile testing, to avoid rate limits; remove it and deleteacme.jsonfor real certificates. - Protect the Docker socket — whoever controls Traefik can control Docker. Keep it
:ro, keep Traefik updated, or use a socket proxy that allows only read calls.
Check that it worked
curl -s https://whoami.example.com/ prints Hostname: … and the request headers; curl -sI http://whoami.example.com/ answers with a redirect to HTTPS; the certificate is from Let's Encrypt (curl -vI https://whoami.example.com/ 2>&1 | grep issuer); docker compose logs traefik shows no ACME errors.
Common problems
| Problem | Cause | Fix |
|---|---|---|
404 page not found | No router matches: missing traefik.enable=true, or the Host rule differs. | Check labels with docker inspect; exact host name in the rule. |
Bad Gateway / Gateway Timeout | Wrong internal port, or Traefik and app not on the same network. | loadbalancer.server.port; shared network + traefik.docker.network. |
| Self-signed "TRAEFIK DEFAULT CERT" | ACME failed (DNS, port 80/443 closed, rate limit). | Check logs; fix DNS/firewall; use staging while testing. |
| Basic auth always fails | $ in the hash not doubled in Compose. | Write $$ for every $. |
permission denied on docker.sock | Rootless Docker or SELinux. | Use the right socket path; :z or a socket proxy. |
| Labels ignored | Labels set on the image or wrong indentation. | Labels belong to the service in Compose; recreate the container. |
Go further
- Command pages: traefik, docker, curl.
- Next how-tos: Run an app with Docker Compose, Set up Nginx as a reverse proxy, Host two websites on one server, Get a free TLS certificate with Let's Encrypt.
- Learn it: Traffic tools at work — Traefik, Web services — Traefik. Full guide: Traefik.
1Why set exposedbydefault=false?
Then only containers with traefik.enable=true are published, so a new internal container is not online by accident.
2Which port does the service label need?
The port the app listens on inside the container, not a port published on the host.