53 lines
1.2 KiB
Docker
53 lines
1.2 KiB
Docker
# Multi-stage build for optimized production image
|
|
FROM node:18-alpine AS builder
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy package files
|
|
COPY package*.json ./
|
|
|
|
# Install dependencies
|
|
RUN npm ci --only=production && npm cache clean --force
|
|
|
|
# Copy source code
|
|
COPY . .
|
|
|
|
# Create non-root user
|
|
RUN addgroup -g 1001 -S nodejs && \
|
|
adduser -S gameuser -u 1001
|
|
|
|
# Production stage
|
|
FROM node:18-alpine AS production
|
|
|
|
WORKDIR /app
|
|
|
|
# Install security updates
|
|
RUN apk --no-cache upgrade
|
|
|
|
# Copy non-root user from builder
|
|
RUN addgroup -g 1001 -S nodejs && \
|
|
adduser -S gameuser -u 1001
|
|
|
|
# Copy built application
|
|
COPY --from=builder --chown=gameuser:nodejs /app/node_modules ./node_modules
|
|
COPY --from=builder --chown=gameuser:nodejs /app/package*.json ./
|
|
COPY --from=builder --chown=gameuser:nodejs /app/server.js ./
|
|
COPY --from=builder --chown=gameuser:nodejs /app/src ./src
|
|
|
|
# Health check
|
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
|
CMD node -e "require('http').get('http://localhost:3003/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1) })"
|
|
|
|
# Switch to non-root user
|
|
USER gameuser
|
|
|
|
# Expose port
|
|
EXPOSE 3003
|
|
|
|
# Set environment
|
|
ENV NODE_ENV=production
|
|
ENV PORT=3003
|
|
|
|
# Start application
|
|
CMD ["node", "server.js"]
|