Files
motovaultpro/backend/Dockerfile
Eric Gullickson e22d643ae3 Security Fixes
2025-08-24 14:39:50 -05:00

64 lines
1.5 KiB
Docker

# Production Dockerfile for MotoVaultPro Backend
# Stage 1: Build stage
FROM node:20-alpine AS builder
# Install build dependencies
RUN apk add --no-cache dumb-init git
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install all dependencies (including dev for building)
RUN npm install && npm cache clean --force
# Copy source code
COPY . .
# Build the application
RUN npm run build
# Stage 2: Production runtime
FROM node:20-alpine AS production
# Install runtime dependencies only
RUN apk add --no-cache dumb-init
# Set working directory
WORKDIR /app
# Copy package files and any lock file generated in builder stage
COPY package*.json ./
COPY --from=builder /app/package-lock.json ./
# Install only production dependencies
RUN npm ci --omit=dev && npm cache clean --force
# Create non-root user
RUN addgroup -g 1001 -S nodejs && \
adduser -S nodejs -u 1001
# Copy built application from builder stage
COPY --from=builder /app/dist ./dist
# Change ownership to non-root user
RUN chown -R nodejs:nodejs /app
# Switch to non-root user
USER nodejs
# Expose port
EXPOSE 3001
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD node -e "require('http').get('http://localhost:3001/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1) }).on('error', () => process.exit(1))"
# Use dumb-init for proper signal handling
ENTRYPOINT ["dumb-init", "--"]
# Run production application
CMD ["npm", "start"]