-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
69 lines (49 loc) · 1.89 KB
/
Dockerfile
File metadata and controls
69 lines (49 loc) · 1.89 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
# ==================== STAGE 1: Dependencies ====================
# Install dependencies only when needed
FROM node:20-alpine AS deps
# Install pnpm
RUN corepack enable && corepack prepare pnpm@10.28.1 --activate
# Set working directory
WORKDIR /app
# Copy package files and prisma schema
COPY package.json pnpm-lock.yaml ./
COPY prisma ./prisma
# Install dependencies (this will run prisma generate via postinstall)
RUN pnpm install --frozen-lockfile
# ==================== STAGE 2: Builder ====================
# Rebuild the source code only when needed
FROM node:20-alpine AS builder
# Install pnpm
RUN corepack enable && corepack prepare pnpm@10.28.1 --activate
WORKDIR /app
# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
# Copy source code
COPY . .
# Generate Prisma Client and build the application
RUN pnpm run build
# ==================== STAGE 3: Runner ====================
# Production image, copy all the files and run the app
FROM node:20-alpine AS runner
# Install pnpm
RUN corepack enable && corepack prepare pnpm@10.28.1 --activate
WORKDIR /app
# Set environment to production
ENV NODE_ENV=production
# Create a non-root user
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nestjs
# Copy necessary files from builder
COPY --from=builder --chown=nestjs:nodejs /app/dist ./dist
COPY --from=builder --chown=nestjs:nodejs /app/node_modules ./node_modules
COPY --from=builder --chown=nestjs:nodejs /app/package.json ./package.json
COPY --from=builder --chown=nestjs:nodejs /app/prisma ./prisma
# Switch to non-root user
USER nestjs
# Expose the application port
EXPOSE 3000
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
CMD node -e "require('http').get('http://localhost:3000/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})"
# Start the application
CMD ["node", "dist/main"]