Traefik Cheat Sheet
Here are quick references for using Traefik
on Docker
containers environment.
I use Traefik
in my docker compose project more frequently than using Nginx
, as it has such advantages:
- Simply to use and fast to spin up
- Configuration relies on platform syntax(like
Docker labels
orKubernetes annotations
), whileNginx
uses its own syntax and directives. - Configuration can just sit in
docker-compose.yml
, butNginx
always uses a dedicate configuration file(/etc/nginx/nginx.conf
).
- Configuration relies on platform syntax(like
- Dynamic configuration:
- Changes to the configuration require a restart of the
Nginx
process - Changes to the configuration require a restart of its corresponding service, not the
Traefik
process
- Changes to the configuration require a restart of the
Entrypoint -> Router -> Middleware 1 -> Middleware 2 -> ... -> Service
Redirect root path /
to a subpath
The goal is to redirect root path /
to sub path /mtr
that's an ingress for a web service:
http://127.0.0.1
->http://127.0.0.1/mtr
http://127.0.0.1
->http://127.0.0.1/mtr
https://127.0.0.1/something
-> no redirect
It works for Traefik 2.0
services:
traefik:
image: traefik:v2.10
command:
- --api.insecure=true
- --providers.docker=true
- --providers.docker.exposedbydefault=false
- --entrypoints.web.address=:80
ports:
- 80:80
- 8080:8080 # Web UI Port
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
labels:
- traefik.enable=true
# Redirection from `http://xxx.com` to `http://xxx.com/foo`
- traefik.http.routers.domain.entrypoints=web
- traefik.http.routers.domain.rule=Path(`/`)
- traefik.http.routers.domain.service=noop@internal
- traefik.http.routers.domain.middlewares=to-foo@docker
- traefik.http.middlewares.to-foo.redirectregex.permanent=true
- traefik.http.middlewares.to-foo.redirectregex.regex=^http://([^/]+)/?$
- traefik.http.middlewares.to-foo.redirectregex.replacement=http://$${1}/foo
foo:
image: traefik/whoami:v1.10
hostname: foo.com
labels:
- traefik.enable=true
# just to ingress `http://xxx.com/foo`
- traefik.http.routers.foo.entrypoints=web
- traefik.http.routers.foo.rule=PathPrefix(`/foo`)
Route a prefix to a service
Match a request with a prefix /bar
, strip the prefix and route it to the bar service,
bar:
image: traefik/whoami:v1.10
hostname: bar.com
labels:
- traefik.enable=true
# ingress `http://xxx.com/bar/xyz` and send `http://xxx.com/xyz` to `bar` service
- traefik.http.routers.bar.entrypoints=web
- traefik.http.routers.bar.rule=PathPrefix(`/bar`)
- traefik.http.routers.bar.middlewares=bar-strip-prefix@docker
- traefik.http.middlewares.bar-strip-prefix.stripprefix.prefixes=/bar
Specify a custom port for the container
By default, Traefik used the first exposed port of a container, if a container exposes multiple ports, set traefik.http.services.xxx.loadbalancer.server.port
to override that port.
bar12345:
image: traefik/whoami:v1.10
hostname: bar12345.com
environment:
WHOAMI_PORT_NUMBER: 12345
labels:
- traefik.enable=true
- traefik.http.routers.bar12345.entrypoints=web
- traefik.http.routers.bar12345.rule=PathPrefix(`/bar12345`)
- traefik.http.routers.bar12345.service=bar12345
# Tell Traefik to use the port 12345 to connect to `bar12345` service
- traefik.http.services.bar12345.loadbalancer.server.port=12345
Specify more than one router and service per container
In this example, requests are forwarded for http://127.0.0.1/web2ports-a
to http://<private IP of container>:8001
in addition to http://127.0.0.1/web2ports-b
forwarding to http://<private IP of container>:8002
:
web2ports:
image: python:3.10
ports:
- 8001:8001
- 8002:8002
working_dir: /app
volumes:
- ./server_whoami.py:/app/server_whoami.py
command: >
sh -c "python3 server_whoami.py --port 8001
& python3 server_whoami.py --port 8002"
labels:
- traefik.enable=true
# Specify more than one router and service per container
- traefik.http.routers.a-router.entrypoints=web
- traefik.http.routers.a-router.rule=PathPrefix(`/web2ports-a`)
- traefik.http.routers.a-router.service=a-service
- traefik.http.services.a-service.loadbalancer.server.port=8001
- traefik.http.routers.b-router.entrypoints=web
- traefik.http.routers.b-router.rule=PathPrefix(`/web2ports-b`)
- traefik.http.routers.b-router.service=b-service
- traefik.http.services.b-service.loadbalancer.server.port=8002
Resources
URL Redirect abc.com to xyz.com - Traefik v2 (latest) - Traefik Labs Community Forum
Traefik redirect / (root) to sub path with Docker labels · GitHub