-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDockerfile
More file actions
89 lines (67 loc) · 2.69 KB
/
Dockerfile
File metadata and controls
89 lines (67 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# =============================================================================
# EVO-CAMPAIGN BACKEND - Optimized Multi-stage Dockerfile
# =============================================================================
# -----------------------------------------------------------------------------
# Stage 1: Dependencies and Build
# -----------------------------------------------------------------------------
FROM node:20-slim AS builder
# Set working directory
WORKDIR /app
# Install system dependencies
RUN apt-get update && apt-get install -y \
python3 \
make \
g++ \
&& rm -rf /var/lib/apt/lists/*
# Copy package files first (better cache layer)
COPY package*.json ./
# Install ALL dependencies for build (cached if package.json unchanged)
RUN npm ci --ignore-scripts && \
npm cache clean --force
# Copy config files
COPY tsconfig*.json ./
COPY nest-cli.json ./
# Copy source code
COPY src/ ./src/
# Build the application and create production node_modules
RUN npm run build && \
npm ci --only=production --ignore-scripts && \
cp -R node_modules prod_node_modules
# -----------------------------------------------------------------------------
# Stage 2: Production Runtime
# -----------------------------------------------------------------------------
FROM node:20-slim AS production
# Set working directory
WORKDIR /app
# Install system dependencies for production
RUN apt-get update && apt-get install -y \
dumb-init \
curl \
&& rm -rf /var/lib/apt/lists/*
# Create non-root user
RUN groupadd -g 1001 nodejs && \
useradd -r -u 1001 -g nodejs nestjs
# Copy package files and production dependencies
COPY --from=builder --chown=nestjs:nodejs /app/package*.json ./
COPY --from=builder --chown=nestjs:nodejs /app/prod_node_modules ./node_modules
# Copy built application from builder stage
COPY --from=builder --chown=nestjs:nodejs /app/dist ./dist
# Copy only essential config files for migrations
COPY --from=builder --chown=nestjs:nodejs /app/tsconfig.json ./
COPY --from=builder --chown=nestjs:nodejs /app/nest-cli.json ./
# Copy only migration files (not entire src/)
COPY --from=builder --chown=nestjs:nodejs /app/src/database ./src/database
# Create necessary directories
RUN mkdir -p /app/logs && \
chown -R nestjs:nodejs /app
# Switch to non-root user
USER nestjs
# Expose port (conditional based on RUN_MODE)
EXPOSE 3005
# Dynamic health check (only for API mode)
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD [ "$RUN_MODE" = "api" ] && curl -f http://localhost:${PORT:-3005}/api/v1/health || exit 0
# Use dumb-init to handle signals properly
ENTRYPOINT ["dumb-init", "--"]
# Start the application
CMD ["node", "dist/main.js"]