Getting Started with Docker and Traefik


目录

Docker is a first-class citizen in Traefik, offering native support for Docker containers and services. Whether you're using Docker Compose or running containers directly, Traefik provides a seamless experience for managing your Docker traffic.

This guide shows you how to:

Prerequisites

Install Traefik

Using Docker Compose

Create a Docker Compose file. This configuration:

# docker-compose.yml
services:
  traefik:
    image: traefik:v3.8
    command:
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--entrypoints.web.address=:80"
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

Start Traefik:

docker-compose up -d

Using Docker CLI

Alternatively, you can run Traefik directly with Docker. This command:

Create a configuration file:

# traefik.yml
api:
  insecure: true
entryPoints:
  web:
    address: ":80"
providers:
  docker: {}

Start Traefik:

docker run -d \
  -p 80:80 \
  -p 8080:8080 \
  -v $PWD/traefik.yml:/etc/traefik/traefik.yml \
  -v /var/run/docker.sock:/var/run/docker.sock \
  traefik:v3.8

Expose the Dashboard

Because we explicitly enabled insecure mode, the dashboard is reachable on port 8080 without authentication. Do not enable this flag in production.

You can access the dashboard at:

http://localhost:8080/dashboard/

Traefik Dashboard Screenshot

Deploy a Sample Application

Create a whoami service:

# whoami.yml
services:
  whoami:
    image: traefik/whoami
    labels:
      - "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"

Apply the configuration:

docker-compose -f whoami.yml up -d

Test Your Setup

You can use the following curl command to verify that the application is correctly exposed:

curl http://whoami.localhost

Hostname: 068c0a29a8b7
IP: 127.0.0.1
IP: ::1
IP: 192.168.147.3
RemoteAddr: 192.168.147.2:56006
GET / HTTP/1.1
Host: whoami.localhost
User-Agent: curl/8.7.1
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 192.168.147.1
X-Forwarded-Host: whoami.localhost
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: 9232cdd4fd6c
X-Real-Ip: 192.168.147.1

You can also open http://whoami.localhost in a browser to test the application:

whoami application Screenshot

If you navigate to the HTTP Routers section of the Traefik dashboard, you can see that the whoami.localhost route is managed by the Traefik Docker provider:

Traefik Dashboard HTTP Routers Section Screenshot

That's it! You've successfully deployed Traefik and configured routing in Docker.

Next Steps

{% include-markdown "includes/traefik-for-business-applications.md" %}


版本号 #3
由 壳壳虫 创建于 2026-10-31 13:58:53 UTC
由 cocobook-sync 更新于 2026-11-05 04:17:05 UTC