# Production Docker Compose – Strapi CMS
# Usage: docker compose -f docker-compose.prod.yml up -d
#
# Database: PostgreSQL on the MACHINE (host), not in Docker.
# Strapi connects via host.docker.internal. Set DATABASE_* in .env.
# See .env.production.example.

services:
  meilisearch:
    image: getmeili/meilisearch:v1.11
    container_name: nrel-cms-meilisearch
    restart: unless-stopped
    environment:
      MEILI_MASTER_KEY: ${MEILISEARCH_MASTER_KEY:-}
      MEILI_ENV: production
    volumes:
      - meilisearch_data:/meili_data
    networks:
      - cms-network
    # Optional: expose only internally; reverse proxy can route /search if needed
    # ports: ["7700:7700"]

  strapi:
    build:
      context: .
      dockerfile: Dockerfile
      # Use host network so apk can reach Alpine mirrors (fixes DNS in build)
      network: host
    image: nrel-cms-strapi:latest
    container_name: nrel-cms
    restart: unless-stopped
    env_file: .env
    environment:
      NODE_ENV: production
      HOST: 0.0.0.0
      PORT: 1337
      DATABASE_CLIENT: postgres
      # Host machine Postgres (host.docker.internal = host from inside container)
      DATABASE_HOST: ${DATABASE_HOST:-host.docker.internal}
      DATABASE_PORT: ${DATABASE_PORT:-5432}
      DATABASE_NAME: ${DATABASE_NAME:-nre}
      DATABASE_USERNAME: ${DATABASE_USERNAME:-postgres}
      DATABASE_PASSWORD: ${DATABASE_PASSWORD:?Set DATABASE_PASSWORD in .env}
      DATABASE_SSL: ${DATABASE_SSL:-false}
      MEILISEARCH_HOST: http://meilisearch:7700
      STRAPI_PUBLIC_URL: ${STRAPI_PUBLIC_URL:?Set STRAPI_PUBLIC_URL in .env}
      PUBLIC_URL: ${STRAPI_PUBLIC_URL}
      FRONTEND_URL: ${FRONTEND_URL:?Set FRONTEND_URL in .env}
    extra_hosts:
      # So Strapi container can reach Postgres on the host machine
      - "host.docker.internal:host-gateway"
    volumes:
      - cms_uploads:/app/public/uploads
    networks:
      - cms-network
    depends_on:
      meilisearch:
        condition: service_started
    # Expose for reverse proxy (e.g. Apache/Nginx to https://cms.yourdomain.com)
    ports:
      - "1337:1337"
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:1337/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

volumes:
  meilisearch_data:
  cms_uploads:

networks:
  cms-network:
    driver: bridge
