Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

How Traefik Works

A classic reverse proxy reads a configuration file you write. Traefik also reads providers: Docker, Kubernetes, files, and others. With the Docker provider it watches containers start and stop and creates or removes routes from their labels. The idea of a reverse proxy itself is explained in what a reverse proxy is.

The Four Building Blocks

PartWhat it is
Entry pointA port Traefik listens on
RouterA rule that matches requests
ServiceWhere matched requests go
MiddlewareA change on the way

Middlewares cover redirects, headers, basic authentication, IP allow lists, and rate limits.

The Static Configuration

Create a shared network for Traefik and your applications:

docker network create proxy

Then a traefik.yml, which uses the version 3 syntax:

entryPoints:
  web:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: websecure
          scheme: https
  websecure:
    address: ":443"

providers:
  docker:
    exposedByDefault: false
    network: proxy

certificatesResolvers:
  le:
    acme:
      email: YOUR_EMAIL
      storage: /letsencrypt/acme.json
      httpChallenge:
        entryPoint: web

Replace YOUR_EMAIL with the contact address for the ACME account. Traefik reads this file from /etc/traefik inside the container. With exposedByDefault set to false, a container is only routed when it carries the label traefik.enable=true, so nothing is published by accident.

The Compose File

services:
  traefik:
    image: traefik:v3
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./traefik.yml:/etc/traefik/traefik.yml:ro
      - ./letsencrypt:/letsencrypt
      - /var/run/docker.sock:/var/run/docker.sock:ro
    networks:
      - proxy

networks:
  proxy:
    external: true

Create the certificate store before the first start and keep it readable by root only; Traefik refuses a storage file with looser permissions:

mkdir letsencrypt
touch letsencrypt/acme.json
chmod 600 letsencrypt/acme.json
docker compose up -d

The Docker Socket Is Sensitive

The :ro flag only stops the container from replacing the socket file. It does not limit what can be done through the Docker API, and full API access is equal to root on the host. Anyone who takes over Traefik could control every container. Keep Traefik updated, and consider a socket proxy that allows only the read calls Traefik needs.

Route an Application with Labels

services:
  app:
    image: traefik/whoami
    networks:
      - proxy
    labels:
      - traefik.enable=true
      - traefik.http.routers.app.rule=Host(`app.example.com`)
      - traefik.http.routers.app.entrypoints=websecure
      - traefik.http.routers.app.tls.certresolver=le

networks:
  proxy:
    external: true

The application has no ports: entry: Traefik reaches it over the shared network (see Docker networks). If the container exposes several ports, name the right one with the label traefik.http.services.app.loadbalancer.server.port. While testing, point the resolver at the Let's Encrypt staging server with the caServer setting, so failed attempts do not run into rate limits.

Keep the Dashboard Private

Traefik's dashboard shows every route and service. Do not switch on the insecure API mode on a public port: it serves the dashboard without a login on port 8080, which stays closed to the internet. Only ports 80 and 443 are opened. Either leave the dashboard off, or route it through its own router with a basic authentication middleware and an IP allow list, or bind it to localhost and reach it through an SSH tunnel (see SSH port forwarding).

Traefik or Nginx?

TraefikNginx
Routes fromLabels, providersConfig files
CertificatesBuilt inCertbot
Static filesNoYes
Best forMany containersFixed sites

For a fixed set of sites, an Nginx reverse proxy is simpler to read and debug.

Was this answer helpful?
Loading...