Linux & software2 min read

Traefik as a reverse proxy for Docker with automatic HTTPS

Set up Traefik v3 with Docker Compose: publish containers under your own domain via labels, get Let's Encrypt certificates automatically and secure the dashboard.

Traefik is a reverse proxy built for containers. It reads the labels of your Docker containers and sets up the routing itself: start the container, the domain is live and the certificate is there. Ideal when several Docker applications run on one server.

Requirements

  • Docker and Docker Compose, see Install Docker
  • Ports 80 and 443 are free (no Nginx or Apache on them) and reachable from outside
  • The A records of your domains point to the server

Create a shared network

Traefik and your applications share a Docker network:

docker network create proxy

Start Traefik

mkdir -p ~/traefik && cd ~/traefik
touch acme.json && chmod 600 acme.json
nano compose.yaml
services:
  traefik:
    image: traefik:v3.1
    restart: unless-stopped
    command:
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --providers.docker.network=proxy
      - --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.email=admin@example.com
      - --certificatesresolvers.le.acme.storage=/acme.json
      - --certificatesresolvers.le.acme.httpchallenge.entrypoint=web
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./acme.json:/acme.json
    networks:
      - proxy

networks:
  proxy:
    external: true

Enter your address for acme.email and start it:

docker compose up -d
docker compose logs -f

exposedbydefault=false means Traefik only publishes containers you explicitly enable.

Publish an application

As an example, the small test service whoami under whoami.example.com:

services:
  whoami:
    image: traefik/whoami
    restart: unless-stopped
    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
    networks:
      - proxy

networks:
  proxy:
    external: true

After docker compose up -d the service is reachable at https://whoami.example.com with a valid certificate. The container needs no ports entry of its own; Traefik reaches it via the proxy network.

If the application inside the container does not listen on port 80, tell Traefik the port:

- traefik.http.services.whoami.loadbalancer.server.port=3000

The dashboard

Traefik has a dashboard that shows all routers and services. Never expose it unprotected. With a password it works like this. First create the entry:

sudo apt install apache2-utils
htpasswd -nB admin

In the output you must write every $ as $$ so that Compose does not read it as a variable. Then add to the Traefik file:

    labels:
      - traefik.enable=true
      - traefik.http.routers.dash.rule=Host(`traefik.example.com`)
      - traefik.http.routers.dash.entrypoints=websecure
      - traefik.http.routers.dash.tls.certresolver=le
      - traefik.http.routers.dash.service=api@internal
      - traefik.http.routers.dash.middlewares=dash-auth
      - traefik.http.middlewares.dash-auth.basicauth.users=admin:$$2y$$05$$...

and under command the line - --api.dashboard=true.

The Docker socket

Traefik reads the containers via /var/run/docker.sock. Whoever controls this socket controls the server. That is why it is mounted read-only here, and you should update the Traefik image regularly:

docker compose pull && docker compose up -d

Troubleshooting

  • No certificate, browser warns: docker compose logs traefik shows the answer from Let's Encrypt. Usually port 80 is closed or the A record points elsewhere.
  • 404 page not found: The container is not attached to the proxy network, traefik.enable=true is missing, or the Host rule does not match.
  • Bad Gateway: Wrong internal port, set loadbalancer.server.port.

Written by TryNxt team

We build and run TryNxt ourselves and write down here what we learn along the way.