services:
  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: labwise
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: labwise_db
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U labwise -d labwise_db"]
      interval: 5s
      timeout: 5s
      retries: 10
    deploy:
      resources:
        limits:
          memory: 256M

  backend:
    build:
      context: .
      dockerfile: Dockerfile.backend
    restart: unless-stopped
    # Reads all vars from server/.env, then overrides the two that differ in Docker
    env_file: ./server/.env
    environment:
      DATABASE_URL: postgresql://labwise:${POSTGRES_PASSWORD}@postgres:5432/labwise_db
      UPLOADS_DIR: /app/uploads
    volumes:
      - uploads_data:/app/uploads
      - ./server/AuthKey_A7ASKB9B7V.p8:/app/AuthKey_A7ASKB9B7V.p8:ro
    depends_on:
      postgres:
        condition: service_healthy
    deploy:
      resources:
        limits:
          memory: 192M

  frontend:
    build:
      context: .
      dockerfile: Dockerfile.frontend
    restart: unless-stopped
    ports:
      - "5173:5173"
    depends_on:
      - backend
    deploy:
      resources:
        limits:
          memory: 48M

volumes:
  postgres_data:
  uploads_data:
