Log inSign up

Route containers by label with Traefik and automatic HTTPS

Run Traefik v3 as a container with the Docker socket mounted read only, entry points on ports 80 and 443, and a Let's Encrypt certificate resolver that stores its data in acme.json. Each app then gets a few labels such as traefik.http.routers.app.rule=Host(`app.example.net`) and Traefik picks up the route and certificate the moment the container starts. The dashboard is served on its own host name behind basic authentication and an IP allow list.

Topic: Web servers and proxies · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13 · Updated 2026-12-03

How does Traefik decide where to send a request?

Traefik watches the Docker API and builds its routing table from labels on running containers. Each route has three parts: an entry point (the port it listens on), a router (a rule such as a host name, plus TLS settings and middlewares) and a service (the container and port that receive the traffic). Nothing is written to disk by hand: start a labelled container and it is live; stop it and the route disappears.

This makes Traefik a natural fit for servers where apps come and go as Compose stacks. If you prefer clicking through a form, Nginx Proxy Manager covers the same ground with a web panel.

What do you need before starting?

A server on Ubuntu 24.04, 22.04 or Debian 12 or 13 with Docker Engine and the Compose plugin from the official Docker repository, set up as in the Docker Engine tutorial (or start from the Docker image). Ports 80 and 443 must be free and reachable, and each host name needs DNS records pointing at 198.51.100.24 and 2001:db8:4f2a::24.

docker --version
docker compose version
sudo ss -tlnp | grep -E ":(80|443) "
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

Create a shared network that Traefik and every app will join, so Traefik can reach containers without any app publishing ports:

docker network create proxy

How do you set up the Traefik container?

Keep Traefik in its own folder with a Compose file, a certificate store and a password file. Prepare the files first; the certificate store must be readable by its owner only, or Traefik refuses to use it.

mkdir -p ~/traefik/letsencrypt && cd ~/traefik
touch letsencrypt/acme.json
chmod 600 letsencrypt/acme.json
sudo apt install -y apache2-utils
htpasswd -cB users deploy

The last command asks for a password and writes a bcrypt hash to ~/traefik/users. Using a file avoids the double-dollar escaping that hashes need when written straight into Compose labels. Now create compose.yaml:

services:
  traefik:
    image: traefik:v3
    container_name: traefik
    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.net
      - --certificatesresolvers.le.acme.storage=/letsencrypt/acme.json
      - --certificatesresolvers.le.acme.httpchallenge.entrypoint=web
      - --api.dashboard=true
      - --log.level=INFO
      - --accesslog=true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./letsencrypt:/letsencrypt
      - ./users:/etc/traefik/users:ro
    networks:
      - proxy
    labels:
      - traefik.enable=true
      - traefik.http.routers.dash.rule=Host(`traefik.example.net`)
      - 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-ip,dash-auth
      - traefik.http.middlewares.dash-auth.basicauth.usersfile=/etc/traefik/users
      - traefik.http.middlewares.dash-ip.ipallowlist.sourcerange=203.0.113.50/32

networks:
  proxy:
    external: true

Replace 203.0.113.50 with your own public address. The v3 tag follows the current major version; once things work, pin a specific minor release such as v3.x from the release notes so upgrades happen when you choose. Use a recent v3 release in any case, since older builds may fail to negotiate with the newest Docker Engine API.

How do you start Traefik and check the dashboard?

Bring the stack up and follow the log for the first certificate request:

cd ~/traefik
docker compose up -d
docker compose logs -f traefik

Then open https://traefik.example.net/dashboard/ (the trailing slash matters). The browser should ask for the user you created with htpasswd, and only from the allowed IP. From any other address Traefik returns 403 Forbidden before the password prompt appears. A command-line check:

curl -sI https://traefik.example.net/dashboard/ | head -n 1
curl -s -u deploy https://traefik.example.net/api/version

The first should return 401 without credentials, the second prints the running Traefik version after you enter the password. Avoid --api.insecure=true: it opens the dashboard on port 8080 without any login.

How do you publish an app with labels?

Add the app to the proxy network, set traefik.enable=true, and give its router a rule, an entry point and the certificate resolver. A complete example with a small test service:

services:
  whoami:
    image: traefik/whoami
    restart: unless-stopped
    networks:
      - proxy
    labels:
      - traefik.enable=true
      - traefik.http.routers.whoami.rule=Host(`app.example.net`)
      - traefik.http.routers.whoami.entrypoints=websecure
      - traefik.http.routers.whoami.tls.certresolver=le
      - traefik.http.services.whoami.loadbalancer.server.port=80

networks:
  proxy:
    external: true

Save it in a separate folder such as ~/apps/whoami/compose.yaml and run docker compose up -d there. After a few seconds, curl https://app.example.net prints request details from the container. Router names must be unique across all containers, so name them after the app. The loadbalancer.server.port label tells Traefik which container port to use when an image exposes more than one.

How do you add security headers and other middlewares?

Middlewares are defined once with labels and then attached to any router by name. A headers middleware for HSTS and common protections looks like this on the app container:

      - traefik.http.middlewares.secure.headers.stsseconds=31536000
      - traefik.http.middlewares.secure.headers.contenttypenosniff=true
      - traefik.http.middlewares.secure.headers.framedeny=true
      - traefik.http.routers.whoami.middlewares=secure

Other useful built-ins are ratelimit for throttling, redirectregex for www to bare domain moves, and compress for gzip. Start HSTS small, as explained in the HTTPS and redirects guide, before setting a year.

How do you upgrade and back up Traefik?

Read the release notes, change the image tag in compose.yaml if you pinned one, then pull and recreate:

cd ~/traefik
docker compose pull
docker compose up -d
docker compose logs --tail 30 traefik

The only state worth keeping is ~/traefik: the Compose file, the users file and letsencrypt/acme.json with your account key and certificates. Copy that folder off the server regularly. Losing it is not fatal, since Traefik will request new certificates, but repeated requests can run into Let's Encrypt rate limits.

What are common Traefik problems?

  • 404 page not found: no router matched. Check that the container has traefik.enable=true, that the host rule uses backticks, and that it shares the proxy network.
  • Gateway timeout: Traefik picked the wrong network or port. Set traefik.docker.network=proxy and the loadbalancer.server.port label.
  • Default certificate warning: the ACME request failed. Search the log for acme; DNS or a closed port 80 is the usual cause, and a wrong mode on acme.json is the other.
  • Changes to Traefik flags ignored: command line flags apply at start, so run docker compose up -d again after editing.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-2Traefik plus a few light containers1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months
A2-4several app stacks behind one proxy2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months

Questions about Traefik for Docker containers

Is mounting the Docker socket safe?

The socket grants control over Docker, which is effectively root. Mount it read only as shown, keep Traefik updated, and for stricter setups put a socket proxy container in between that allows only read calls.

Can Traefik issue wildcard certificates?

Yes, with a DNS challenge resolver instead of the HTTP challenge. You give Traefik an API token for your DNS host and set dnschallenge.provider on the resolver.

Does Traefik need its own server?

No. It runs next to your apps on the same VPS and uses little memory; size the plan for the apps.

Can I route to something that is not a container?

Yes, through the file provider. Add --providers.file.directory=/etc/traefik/dynamic, mount that folder, and describe the router and a service URL such as http://172.17.0.1:3000 in a YAML file.

What happened to ipwhitelist?

Traefik v3 renamed the middleware to ipallowlist. Configurations written for v2 need that change, along with a few others listed in the migration notes.