Traefik dynamically discovers services running in Docker and routes traffic to them using simple container labels. This enables easy and powerful HTTP routing and load balancing without manual configuration.
In order for Traefik to recognize a container and route traffic to it, the container must be started with a specific set of labels.
Below is an example set of labels for a container that should be exposed via Traefik:
Label |
Value |
|---|---|
traefik.enable |
true |
traefik.http.routers.my_app.entrypoints |
http |
traefik.http.routers.my_app.rule |
Host(`application.local`) |
traefik.http.routers.my_app.service |
my_app |
traefik.http.services.my_app.loadbalancer.server.port |
80 |
`traefik.enable=true` Enables Traefik for this container. If false or not set, Traefik will ignore it.
`traefik.http.routers.my_app.entrypoints=http` Binds this route to the http entry point (typically port 80).
`traefik.http.routers.my_app.rule=Host(`application.local`)` Defines the routing rule. In this case, any request with the Host header application.local will be routed to this container.
`traefik.http.routers.my_app.service=my_app` Assigns the router to a named service (here, my_app).
`traefik.http.services.my_app.loadbalancer.server.port=80` Defines the internal container port that Traefik should forward traffic to.
To use this setup, ensure your Docker container is started with these labels (e.g., in docker-compose.yml):
services:
my_app:
image: my_app_image
labels:
- "traefik.enable=true"
- "traefik.http.routers.my_app.entrypoints=http"
- "traefik.http.routers.my_app.rule=Host(`application.local`)"
- "traefik.http.routers.my_app.service=my_app"
- "traefik.http.services.my_app.loadbalancer.server.port=80"
With this configuration, Traefik will automatically detect the container and route incoming HTTP requests with the host application.local to it.
This also enables built-in load balancing: if multiple containers share the same service name (my_app), Traefik will distribute requests between them.