Automatic HTTPS for Docker containers with Traefik and Let's Encrypt
Configure Traefik to issue Let's Encrypt certificates automatically for Docker containers using HTTP-01 challenges and HTTP-to-HTTPS redirection.
30 May 2026, 23:33 UTC

Desired outcome
Traefik should automatically request a Let's Encrypt certificate for a domain pointed at your host, serve the container over HTTPS on port 443, redirect HTTP on port 80 to HTTPS, and renew the certificate before expiry without restarting Traefik.
Prerequisites
- A host with Docker installed and a public DNS record resolving example.com to the host's public IP.
- Inbound TCP port 80 and 443 reachable from the public internet. Let's Encrypt validates HTTP-01 by contacting /.well-known/acme-challenge/ on port 80.
- Traefik v2.x. Configuration keys may differ in Traefik v3; review release notes before applying this to a v3 deployment.
- An email address for the Let's Encrypt account.
Terms used in this guide
- Entry point: a network address Traefik listens on, such as port 80 or 443.
- Router: matches incoming requests to a service using a rule, commonly Host(`example.com`).
- Certificate resolver: Traefik's ACME client that obtains and renews certificates from Let's Encrypt.
- HTTP-01 challenge: a Let's Encrypt validation method where Traefik serves a token at a well-known path to prove control of the domain.
1. Create the Traefik static configuration
Save this as traefik.yml on the host. It defines two entry points, redirects port 80 to port 443, enables the Docker provider, and configures the Let's Encrypt resolver.
entryPoints:
web:
address: ":80"
http:
redirections:
entryPoint:
to: websecure
scheme: https
websecure:
address: ":443"
api:
dashboard: true
insecure: true
providers:
docker:
endpoint: "unix:///var/run/docker.sock"
exposedByDefault: false
certificatesResolvers:
letsencrypt:
acme:
email: "[contact removed]"
storage: "acme.json"
httpChallenge:
entryPoint: web
Replace [contact removed] with a real address. Let's Encrypt uses it for account notifications and expiry warnings. The dashboard is exposed without authentication here because insecure: true disables access controls; restrict or disable it before production use.
2. Protect the certificate store
Traefik writes certificates and the ACME account key to acme.json. Create it with restrictive permissions before starting the container so other local users cannot read private keys.
touch acme.json
chmod 600 acme.json
Run these commands on the host where acme.json will live. If the Traefik container runs as a non-root user, match the file owner to that user or allow the container user to write to the file.
3. Define Traefik and a test container in Compose
Save this as docker-compose.yml. Replace example.com with your domain.
services:
traefik:
image: traefik:v2.10
command:
- "--api.insecure=true"
- "--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=[contact removed]"
- "--certificatesresolvers.letsencrypt.acme.storage=acme.json"
- "--certificatesresolvers.letsencrypt.acme.httpChallenge.entryPoint=web"
ports:
- "80:80"
- "443:443"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "./traefik.yml:/traefik.yml"
- "./acme.json:/acme.json"
labels:
- "traefik.enable=true"
whoami:
image: traefik/whoami
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`example.com`)"
- "traefik.http.routers.whoami.entrypoints=websecure"
- "traefik.http.routers.whoami.tls.certresolver=letsencrypt"
The Docker socket is mounted read-only (:ro), which lets Traefik read container labels without allowing it to modify container state. The whoami container is a small HTTP echo service useful for verifying routing.
4. Start Traefik
Run from the directory containing the Compose file:
docker compose up -d
Traefik reads traefik.yml, connects to the Docker socket, and discovers the labeled whoami container. It creates a router and service dynamically without a restart.
5. Verify issuance and redirection
Run these checks from a host with internet access to your domain.
| Check | Command | Expected result |
|---|---|---|
| HTTP redirect | curl -I http://example.com/ | Response 301 or 308 with Location: https://example.com/ |
| HTTPS reachable | curl -I https://example.com/ | Response 200 from the whoami container |
| Certificate issuer | openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -issuer -subject | Issuer contains Let's Encrypt; subject contains example.com |
| Certificate store | ls -l acme.json | File exists, permissions 600, non-zero size |
| Traefik logs | docker logs traefik | ACME messages showing certificate obtained or renewed; no repeated failure errors |
The first HTTPS request may take several seconds while Traefik completes the HTTP-01 challenge with Let's Encrypt. Subsequent requests use the cached certificate until renewal is due.
Limitations
- HTTP-01 requires public inbound TCP/80. Firewalls or cloud security groups that block port 80 will cause issuance to fail.
- HTTP-01 cannot issue wildcard certificates. Wildcards require DNS-01 validation through a supported DNS provider.
- Let's Encrypt enforces rate limits per domain. Repeated failed or duplicate issuance attempts can trigger temporary blocking. Use Let's Encrypt's staging endpoint during initial testing by adding the staging caServer to the resolver configuration.
- acme.json contains private keys. Do not commit it to version control, and keep permissions restricted.
- This guide assumes Traefik v2.x. Traefik v3 may deprecate or rename configuration keys; verify syntax before upgrading.
Recovery and rollback
Starting Traefik and requesting certificates changes state by creating or modifying acme.json. To reset the ACME state:
- Stop Traefik: docker compose stop traefik
- Back up or move the existing store: mv acme.json acme.json.bak
- Restart Traefik: docker compose start traefik
Traefik will create a new acme.json and attempt issuance again on the next request. Be aware that re-issuance counts against Let's Encrypt rate limits.
Version note
Traefik v2 and v3 share provider concepts but differ in configuration syntax and deprecated options. If you are using Traefik v3, consult the official migration notes before copying the keys above.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.