# Use an official Node runtime as the parent image
FROM node:18-slim

# Create app directory
WORKDIR /usr/src/app

# Install curl for healthcheck
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./

# Install dependencies
RUN npm ci --only=production

# Bundle app source
COPY . .

# Create a non-root user with system UID
RUN addgroup --system --gid 999 nodejs && \
    adduser --system --uid 999 --ingroup nodejs --no-create-home nodejs && \
    chown -R nodejs:nodejs /usr/src/app

USER nodejs

# Your app binds to port 3000
EXPOSE 3000

# Add healthcheck
HEALTHCHECK --interval=30s --timeout=3s \
  CMD curl -f http://localhost:3000/health || exit 1

# Define environment variable
ENV NODE_ENV=production

# Start the app
CMD [ "node", "index.js" ]