FROM node:18-alpine

# Install Chromium and dependency packages
RUN apk add --no-cache chromium nss freetype harfbuzz ca-certificates ttf-freefont

WORKDIR /var/www/html_docker/node_api

# Install PM2 globally
RUN npm install pm2 -g

# Install and configure PM2 Logrotate to keep logs in check
RUN pm2 install pm2-logrotate && \
    pm2 set pm2-logrotate:max_size 100M && \
    pm2 set pm2-logrotate:retain 10 && \
    pm2 set pm2-logrotate:compress true

# ── Install dependencies ──────────────────────────────────────────────────
# Copy package files
COPY node_api/package*.json ./

# Install dependencies
RUN npm install --production

# ── Copy source code ─────────────────────────────────────────────────────────
COPY node_api/ ./

# The source code is mounted via volumes for development, 
# but the WORKDIR ensures we are in the right place.

CMD ["pm2-runtime", "ecosystem.config.js"]
