I had been looking for examples of how to add labels properly and have Traefik dynamically incorporate it. I am using docker-compose
to configure my services and recently someone who's been around the block a few times suggested this configuration for Traefik and the classic "whoami" service:
version: "3.9"
services:
traefik:
image: traefik:v2.8
container_name: traefik
command:
- --api.insecure=true
- --log.level=DEBUG
- --providers.docker
- --entrypoints.web.address=:80
- --entrypoints.web.http.redirections.entryPoint.to=websecure
- --entrypoints.web.http.redirections.entrypoint.scheme=https
- --entrypoints.websecure.address=:443
- --entrypoints.websecure.http.tls=true
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
whoami:
image: traefik/whoami:v1.8.1
container_name: whoami
labels:
traefik.http.routers.whoami.rule: Host(`whoami.local`)
traefik.http.routers.whoami.entrypoints: websecure
Using this alone, my whoami service works but I was surprised to see "labels" configured as an object hash of key/values. Though much nicer than an array in most cases I've seen examples like this:
labels:
- traefik.http.routers.strapi.rule=Host(`strapi.local`)
- traefik.http.routers.strapi.entrypoints=websecure
- traefik.http.services.strapi.loadbalancer.server.port=1337
Is there a correct one? I will say, because the objection notation is nicer (imo) and worked for "whoami" I figured I'd use that. However, when I try to start the popular CMS Strapi up with this docker-compose file:
version: '3.9'
services:
strapi:
image: strapi/strapi
restart: unless-stopped
volumes:
- /home/ken/data/strapi:/srv/app
ports:
- '1337:1337'
labels:
traefik.enable: true
traefik.http.routers.strapi.rule: Host(`strapi.local`)
traefik.http.routers.strapi.entrypoints: websecure
traefik.http.services.strapi.loadbalancer.server.port: 1337
I get no errors but it just never shows up in Traefik! So then my ability to revert to old bad habits kicks in and I try this instead:
labels:
- traefik.enable=true
- traefik.http.routers.strapi.rule=Host(`strapi.local`)
- traefik.http.routers.strapi.entrypoints=websecure
- traefik.http.services.strapi.loadbalancer.server.port=1337
Now suddenly it shows in Traefik! Looking at it's definition it looks good too:
However, go to strapi.local
and it redirects to HTTPS and then after accepting my SSL key I get nothing for a bit and then a Gateway Timeout. What gives? What's right?