# Use the official Node.js image as the base image
FROM node:22.16.0 AS builder

# Set the working directory inside the container
WORKDIR /app

# Copy package.json and package-lock.json (if available) to the working directory
COPY package*.json ./

# Install the application dependencies
RUN npm install

# Copy the rest of the application code to the working directory
COPY . .

# Production stage
FROM node:22.16.0-alpine AS production

# Set the working directory inside the container
WORKDIR /app

# Copy dependencies from builder stage
COPY --from=builder /app/node_modules ./node_modules

# Copy application code from builder stage
COPY --from=builder /app/. .

# Install pm2 globally
RUN npm install -g pm2

# Expose the port that the application listens on
EXPOSE 3000

# Define the command to run the application in production mode with pm2.json config
CMD ["pm2", "start", "pm2.json", "--no-daemon"]