
FROM node:22-alpine AS builder
# Install pnpm
RUN npm install -g pnpm

WORKDIR /app

# Copy pnpm specific files
COPY pnpm-lock.yaml package.json ./

# Install dependencies
RUN pnpm install --frozen-lockfile

# Copy the rest of the application
COPY . .


# Build the application
RUN pnpm build

# Production stage
FROM node:22-alpine AS runtime

WORKDIR /app

# Copy package files
COPY package.json pnpm-lock.yaml ./

# Install pnpm
RUN npm install -g pnpm

# Install production dependencies only
RUN pnpm install --prod --frozen-lockfile && \
    rm -rf /root/.npm /root/.pnpm-store


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

# Use non-root user
USER node

EXPOSE 3000

ENV NODE_ENV=production

CMD ["node", "build"]
