# Use official Node.js runtime as base image
FROM node:18-alpine
# Set the working directory inside the container
WORKDIR /app
# Create a non-root user for security
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
# Copy package.json and package-lock.json (if available)
COPY package*.json ./
# Install dependencies
RUN npm ci --only=production && npm cache clean --force
# Copy the source code
COPY src/ ./src/
# Copy package files
COPY package*.json ./
# Change ownership of the app directory to the nodejs user
RUN chown -R nextjs:nodejs /app
USER nextjs
# Expose the port the app runs on (if needed for HTTP endpoints)
EXPOSE 3000
# Add health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD node -e "console.log('Health check passed')" || exit 1
# Set environment variables
ENV NODE_ENV=production
ENV LOG_LEVEL=info
# Command to run the application
CMD ["npm", "start"]