Traefik with Docker Compose Example

Traefik has a bit of a learning curve, and it might be kind of hard to justify for personal projects where virtual hosts are simpler. But it's still fun to tinker with. It took quite a bit of tinkering to get routing for correct for my YEAHBOI guide. Here's a purely docker-compose based example.

version: '3.1'

services:
  traefik:
    image: traefik:latest
    command: --providers.docker  --api.dashboard=true
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.to-traefik.rule=Host(`traefik.example.com`)"
      - "traefik.http.routers.to-traefik.service=api@internal"
      - "traefik.http.middlewares.test-auth.basicauth.users=myusername:$$apa1$$kFilpK3H$$CaLljM7uHiE0BQ9pCy7oK0"
      - "traefik.http.routers.to-traefik.middlewares=test-auth@docker"
  project1:
    image: project1:1.0
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.to-project1.service=project1"
      - "traefik.http.routers.to-project1.rule=Host(`project1.example.com`)"
      - "traefik.http.services.project1.loadbalancer.server.port=80"
  project2:
    image: project2:1.0
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.to-project2.service=project2"
      - "traefik.http.routers.to-project2.rule=Host(`project2.example.com`)"
      - "traefik.http.services.project2.loadbalancer.server.port=80"

Getting random Bad Gateway responses?

I started getting random Bad Gateway responses at one point, but it turned out that I misconfigured the traefik service labels. If you start getting random Bad Gateway responses, triple check that your labels are correct and accurate.