Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
110 lines (91 loc) · 5.64 KB
/
Copy pathDockerfile
File metadata and controls
110 lines (91 loc) · 5.64 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Production Dockerfile for nest-server-starter projects.
# Works in both standalone and monorepo (lt fullstack init) setups.
#
# Standalone: docker build -t api .
# Monorepo: docker build --build-arg API_DIR=projects/api -t api .
# (build context = monorepo root)
ARG API_DIR=.
# Stage 1: Install dependencies
FROM node:24-alpine@sha256:a0b9bf06e4e6193cf7a0f58816cc935ff8c2a908f81e6f1a95432d679c54fbfd AS deps
WORKDIR /app
# Build tools for bcrypt native addon
RUN apk add --no-cache python3 make g++
# Copy all manifests for dependency resolution.
# In monorepo mode pnpm needs workspace config + all project manifests;
# copying everything and then installing is the simplest cross-mode approach.
COPY . /tmp/src
# pnpm-lock.yaml and pnpm-workspace.yaml are REQUIRED — the lockfile pins every version and the
# workspace file carries the security `overrides:`. A previous version chained `|| true` across the
# whole `&&` list, so a failed copy of either still exited 0 and the image silently built without
# them. Only .npmrc is genuinely optional, so only that one may fail.
RUN set -e; \
find /tmp/src -maxdepth 3 -name "package.json" -not -path "*/node_modules/*" \
-exec sh -c 'dir=$(dirname "$1" | sed "s|^/tmp/src|.|"); mkdir -p "/app/$dir"; cp "$1" "/app/$dir/"' _ {} \; ; \
cp -f /tmp/src/pnpm-lock.yaml /app/; \
cp -f /tmp/src/pnpm-workspace.yaml /app/; \
if [ -f /tmp/src/.npmrc ]; then cp -f /tmp/src/.npmrc /app/; fi; \
rm -rf /tmp/src
# Provision the exact pnpm declared in package.json (single source of truth).
# No corepack: Node >= 25 no longer ships it. The +sha512 suffix is stripped;
# npm enforces registry integrity for the tarball itself.
RUN npm install -g "$(node -p "require('./package.json').packageManager.split('+')[0]")"
# Install dependencies (--ignore-scripts prevents husky/prepare errors in Docker)
# Rebuild bcrypt native addon separately
RUN pnpm install --frozen-lockfile --ignore-scripts && pnpm rebuild bcrypt
# Stage 2: Build
FROM node:24-alpine@sha256:a0b9bf06e4e6193cf7a0f58816cc935ff8c2a908f81e6f1a95432d679c54fbfd AS builder
ARG API_DIR=.
WORKDIR /app
COPY --from=deps /app ./
COPY ${API_DIR}/ ./${API_DIR}/
# Provision the exact pnpm declared in package.json (single source of truth).
# No corepack: Node >= 25 no longer ships it. The +sha512 suffix is stripped;
# npm enforces registry integrity for the tarball itself.
RUN npm install -g "$(node -p "require('./package.json').packageManager.split('+')[0]")"
RUN cd ${API_DIR} && pnpm run build
# Remove devDependencies after build
RUN CI=true pnpm install --frozen-lockfile --prod --ignore-scripts
# Stage 3: Production runner
FROM node:24-alpine@sha256:a0b9bf06e4e6193cf7a0f58816cc935ff8c2a908f81e6f1a95432d679c54fbfd
ARG API_DIR=.
WORKDIR /app
ENV NODE_ENV=production
# Non-root user
RUN addgroup -g 1001 -S nodejs && adduser -S nodejs -u 1001
# tini as PID 1. Without a real init, node itself becomes PID 1 — and a PID-namespace init is
# SIGNAL_UNKILLABLE, so a default-disposition signal sent from userspace is silently discarded by
# the kernel. The listening HTTP server keeps the event loop busy, so `docker stop` then waits out
# its whole grace period and SIGKILLs: in-flight requests dropped, every onModuleDestroy() skipped.
# tini forwards signals properly and reaps orphans. (`installGracefulShutdown(server)` in main.ts is
# the other half — it is what actually drains the loop. It replaces enableShutdownHooks(); keeping
# both makes Nest close in parallel with any configured shutdownDelayMs, voiding the delay.)
#
# BUDGET for `shutdownDelayMs`: the delay is spent BEFORE the app starts closing, so it consumes the
# orchestrator's grace period and the drain has to finish in what is left. The grace periods are
# short and the process is SIGKILLed the moment one runs out — mid-wait, with no hook having run,
# which is strictly worse than configuring no delay at all:
# docker stop / docker-compose -> 10s (raise via `stop_grace_period`)
# Kubernetes -> 30s (raise via `terminationGracePeriodSeconds`)
# installProcessDiagnostics() -> 30s force-exit, regardless of the orchestrator
# So keep shutdownDelayMs well below the SMALLEST of those and leave room for the drain: with the
# 10s Compose default, a 5s delay already leaves only ~5s to finish in-flight requests and close
# Mongo/Redis/S3. The framework warns above 10000 and caps at 60000, but neither guard knows this
# container's grace period — that budget is yours to keep.
RUN apk add --no-cache tini
# Create writable directories for runtime files (TUS uploads, GraphQL schema)
RUN mkdir -p /app/uploads && chown -R nodejs:nodejs /app
# Copy built application and production-only dependencies
COPY --from=builder --chown=nodejs:nodejs /app/${API_DIR}/dist ./dist
COPY --from=builder --chown=nodejs:nodejs /app/${API_DIR}/package.json ./
COPY --from=builder --chown=nodejs:nodejs /app/node_modules ./node_modules
COPY --chown=nodejs:nodejs --chmod=755 ./${API_DIR}/docker-entrypoint.sh /app/docker-entrypoint.sh
USER nodejs
EXPOSE 3000
# Container healthcheck — uses the framework's HealthCheckModule (enabled via config.healthCheck).
# Orchestrators (Docker Swarm, Kubernetes, docker-compose) can leverage this to detect
# unhealthy containers and restart them. Adjust interval/timeout via --health-* flags at runtime.
# The endpoint is GET /health-check (see CoreHealthCheckController).
# start-period=60s accommodates cold-start with migrations, Mongo connection, and index creation.
HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
CMD wget -qO- http://127.0.0.1:3000/health-check || exit 1
ENTRYPOINT ["/sbin/tini", "--", "/app/docker-entrypoint.sh"]