متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 198Web · Traefik

How to route containers with Traefik labels

With five apps in Docker on one server you need one entry point on ports 80/443 that sends app1.example.com to one container and app2.example.com to another — with Let's Encrypt certificates. Traefik watches Docker and builds its routes from labels on the containers: start a new container with three labels and it is online with HTTPS, no proxy file to edit. This how-to sets up Traefik with Docker Compose and routes an app with labels.

  • 5 الدروس
  • 4 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

198.1

The quick answer

compose.yaml
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:
docker compose up -d
curl -s https://whoami.example.com/ | head -3

198.2

Step by step

  1. DNS first — whoami.example.com must point to the server, and ports 80/443 must be open, or Let's Encrypt cannot check the name.
  2. 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 with traefik.enable=true are published.
  3. Labels on the app:
    • routers.NAME.rule — which requests (Host(...), PathPrefix(/api)).
    • routers.NAME.entrypoints and tls.certresolver — HTTPS with a Let's Encrypt certificate.
    • services.NAME.loadbalancer.server.port — the port inside the container. No ports: needed for the app.
  4. Add middlewares — for example basic auth for an admin tool (in Compose, write every $ of the hash as $$):
labels (part)
      - traefik.http.middlewares.admin-auth.basicauth.users=alice:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/
      - traefik.http.routers.admin.middlewares=admin-auth

Create the hash with htpasswd -nb alice 'password'.

  1. Several networks — if the app is in another Compose project, put both on a shared Docker network and add traefik.docker.network=proxy.
  2. Test certificates with staging first — add --certificatesresolvers.le.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory while testing, to avoid rate limits; remove it and delete acme.json for real certificates.
  3. 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.

198.3

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.

198.4

Common problems

ProblemCauseFix
404 page not foundNo 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 TimeoutWrong 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.sockRootless Docker or SELinux.Use the right socket path; :z or a socket proxy.
Labels ignoredLabels set on the image or wrong indentation.Labels belong to the service in Compose; recreate the container.

الأوامر في هذا الدرس

198.5

Go further

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.

كل الأوامر في هذا الفصل