-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile
More file actions
95 lines (77 loc) · 2.54 KB
/
Copy pathDockerfile
File metadata and controls
95 lines (77 loc) · 2.54 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
90
91
92
93
94
95
# Use Node.js 20 LTS as base image
FROM node:20-alpine AS base
# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
# ! DEPS STEP
# Install dependencies based on the preferred package manager
COPY package.json package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci --force; \
else echo "Lockfile not found." && exit 1; \
fi
# ! BUILDER STEP
# Rebuild the source code only when needed
# Includes development dependencies (TypeScript, build tools, etc.)
FROM base AS builder
WORKDIR /app
RUN corepack enable pnpm
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Build the application
RUN pnpm build
# ! BUILDCACHE STEP
# Optimized build stage for caching - includes dev dependencies for building
FROM base AS buildcache
WORKDIR /app
RUN corepack enable pnpm
COPY --from=deps /app/node_modules ./node_modules
COPY package.json pnpm-lock.yaml* ./
COPY src ./src
COPY tsconfig.json ./
COPY openapi.json ./
# Install all dependencies (including dev) for building, then build
RUN pnpm install --frozen-lockfile
RUN pnpm build
# ! DEVELOPMENT STEP
# Development image with hot reload and all dependencies
FROM base AS development
WORKDIR /app
RUN apk add --no-cache libc6-compat
RUN corepack enable pnpm
COPY --from=deps /app/node_modules ./node_modules
# Copy source files
COPY package.json pnpm-lock.yaml* ./
COPY src ./src
COPY tsconfig.json ./
COPY openapi.json ./
EXPOSE 3000
CMD ["pnpm", "run", "dev"]
# ! RUNNER STEP
# Production image, copy all the files and run the app
# Only production dependencies + compiled code
FROM base AS runner
WORKDIR /app
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 gympal
# Install pnpm and copy dependencies
RUN corepack enable pnpm
COPY --from=deps /app/node_modules ./node_modules
# Copy the built application and lockfile
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/pnpm-lock.yaml* ./
COPY --from=builder /app/openapi.json ./openapi.json
# Install only production dependencies
RUN pnpm install --prod --frozen-lockfile
# Change to non-root user
USER gympal
# Expose the port the app runs on
EXPOSE 3000
ENV PORT=3000
# Start the application
CMD ["node", "dist/server.js"]
# CMD ["npm", "start"] slower build