FROM node:18-alpine AS builder # Create app directory RUN mkdir -p /app WORKDIR /app # Copy package files first for better caching COPY package*.json ./ # Install dependencies RUN npm ci --only=production # Copy source code COPY . . # Production stage FROM node:18-alpine # Create app directory RUN mkdir -p /app WORKDIR /app # Copy built application from builder stage COPY --from=builder /app . # Create non-root user for security RUN addgroup -g 1001 -S nodejs && \ adduser -S nodejs -u 1001 # Change ownership of the app directory to nodejs user RUN chown -R nodejs:nodejs /app USER nodejs # Expose the port the app runs on EXPOSE 8080 # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD node -e "require('http').get('http://localhost:8080/', (res) => { process.exit(res.statusCode === 200 ? 0 : 1) })" # Start the application CMD ["npm", "start"]