services:
  traefik:
    image: traefik:v3.6
    command:
      - --api.dashboard=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --providers.file.directory=/etc/traefik/dynamic
      - --providers.file.watch=true
      - --entrypoints.web.address=:80
      - --entrypoints.web.http.redirections.entryPoint.to=websecure
      - --entrypoints.web.http.redirections.entryPoint.scheme=https
      - --entrypoints.websecure.address=:443
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./docker/traefik/dynamic:/etc/traefik/dynamic:ro
      - ./docker/traefik/certs:/etc/traefik/certs:ro
    labels:
      - traefik.enable=true
      - traefik.http.routers.dashboard.rule=Host(`traefik.blog.localhost`)
      - traefik.http.routers.dashboard.service=api@internal
      - traefik.http.routers.dashboard.entrypoints=websecure
      - traefik.http.routers.dashboard.tls=true
    networks:
      - app

  php:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    volumes:
      - ./:/var/www/html:cached
    environment:
      APP_ENV: ${APP_ENV:-dev}
    depends_on:
      database:
        condition: service_healthy
    networks:
      - app

  worker:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    volumes:
      - ./:/var/www/html:cached
    environment:
      APP_ENV: ${APP_ENV:-dev}
    command: php bin/console messenger:consume commands scheduler_default async --time-limit=3600 --memory-limit=128M -vv
    restart: unless-stopped
    depends_on:
      database:
        condition: service_healthy
    networks:
      - app

  nginx:
    image: nginx:1.27-alpine
    volumes:
      - ./:/var/www/html:cached
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
    depends_on:
      - php
    labels:
      - traefik.enable=true
      - traefik.http.routers.blog.rule=Host(`blog.localhost`)
      - traefik.http.routers.blog.entrypoints=websecure
      - traefik.http.routers.blog.tls=true
      - traefik.http.services.blog.loadbalancer.server.port=80
    networks:
      - app

  database:
    image: mysql:8.4
    environment:
      MYSQL_DATABASE: ${MYSQL_DATABASE:-blog}
      MYSQL_USER: ${MYSQL_USER:-blog}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD:-blog}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-root}
    volumes:
      - database_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD:-root}"]
      interval: 5s
      timeout: 5s
      retries: 10
    networks:
      - app

networks:
  app:

volumes:
  database_data:
