services:
  # Drupal CMS NGINX reverse proxy (cms.nasarek.dev).
  nginx:
    image: rnsrk/drupal-nginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
      args:
        DOMAIN: ${DOMAIN}
    container_name: drupal-reverse-proxy
    labels:
      - traefik.enable=true
      - traefik.docker.network=traefik
      - traefik.http.routers.drupal-reverse-proxy.rule=Host(`cms.${DOMAIN}`)
      - traefik.http.routers.drupal-reverse-proxy.entrypoints=web,websecure
      - traefik.http.routers.drupal-reverse-proxy.middlewares=https-redirect
      - traefik.http.routers.drupal-reverse-proxy.tls=true
      - traefik.http.routers.drupal-reverse-proxy.tls.certresolver=le
      - traefik.http.services.drupal-reverse-proxy.loadbalancer.server.port=80
    volumes:
      - ./drupal/root/web:/var/www/html
    networks:
      - traefik
      - drupal

  # Drupal PHP-FPM backend.
  drupal-fpm:
    image: rnsrk/drupal-php8-4-fpm-bookworm
    build:
      context: ./drupal
      dockerfile: Dockerfile
      args:
        DRUPAL_VERSION: ${DRUPAL_VERSION:-11.3.3}
    labels:
      - traefik.enable=false
    container_name: drupal-fpm
    expose:
      - "9000"
    volumes:
      - ./drupal/root:/opt/drupal
    networks:
      - database
      - drupal

  # Redis cache backend.
  redis:
    image: redis:7-alpine
    container_name: drupal-redis
    command: redis-server --loglevel warning
    volumes:
      - redis-data:/data
    networks:
      - drupal
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s

  # Next.js frontend (nasarek.dev).
  nextjs:
    image: rnsrk/nextjs-frontend
    build:
      context: ./nextjs
      dockerfile: Dockerfile
      args:
        NEXT_PUBLIC_DRUPAL_BASE_URL: https://cms.${DOMAIN}
        DRUPAL_CLIENT_ID: ${DRUPAL_CLIENT_ID}
        DRUPAL_CLIENT_SECRET: ${DRUPAL_CLIENT_SECRET}
        DRUPAL_OAUTH_SCOPE: ${DRUPAL_OAUTH_SCOPE:-}

    container_name: nextjs-frontend
    labels:
      - traefik.enable=true
      - traefik.docker.network=traefik
      - traefik.http.routers.nextjs-frontend.rule=Host(`${DOMAIN}`)
      - traefik.http.routers.nextjs-frontend.entrypoints=web,websecure
      - traefik.http.routers.nextjs-frontend.middlewares=https-redirect
      - traefik.http.routers.nextjs-frontend.tls=true
      - traefik.http.routers.nextjs-frontend.tls.certresolver=le
      - traefik.http.services.nextjs-frontend.loadbalancer.server.port=3000
    env_file:
      - ./nextjs/.env.local
    networks:
      - traefik
      - drupal
    depends_on:
      - drupal-fpm
    restart: unless-stopped

volumes:
  redis-data:
    name: drupal-redis-data

networks:
  traefik:
    external: true
  database:
    external: true
  drupal:
    name: drupal
