Repository navigation
Expand file tree
/
Copy pathDockerfile.migrate
More file actions
152 lines (138 loc) · 8.12 KB
/
Copy pathDockerfile.migrate
File metadata and controls
152 lines (138 loc) · 8.12 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# =============================================================================
# Dockerfile.migrate — Postgres migration runner for helldivers.bot
# =============================================================================
#
# LOCAL BUILD:
# docker build -f ./Dockerfile.migrate -t ghcr.io/elfensky/helldiversbot-migrate:staging .
#
# WHAT THIS IMAGE DOES
#
# Single-purpose, short-lived container that runs `prisma migrate deploy`
# followed by an idempotent seed script, then exits. It is NOT a long-running
# service.
#
# The app image now runs the same two steps on start, from /app/migrate
# (Dockerfile.app, migrate-tools stage; src/update/migrateOnStart.mjs). This
# image stays as the manual and emergency one-shot: for example a seed with
# FORCE_SEED=true, or a migration run while no app task is up.
#
# WHY THIS DOCKERFILE LOOKS UNUSUAL
#
# The "obvious" approach would be `COPY package.json . && npm install prisma`.
# Don't do that. With the project's full package.json on disk, `npm install`
# treats it as the source of truth and installs every Next.js / Sentry /
# Mermaid / OpenTelemetry dependency alongside Prisma — ~1.2 GB of node_modules
# instead of ~300 MB. The image ends up at >2 GB even though we only need 4
# packages. See issue #278 for the full investigation and slim-down history.
#
# The fix: copy package.json to /tmp as a read-only version reference, write
# a minimal package.json in /app next to the project's package-lock.json, then
# install only the packages we need, at the locked versions.
# =============================================================================
FROM node:26-alpine
# tini is a tiny PID 1 init (~150 KB) that reaps zombie processes and forwards
# signals (SIGTERM, SIGINT) to its child. Without it, `docker stop` on a
# mid-flight migrate may be silently dropped — the kernel does not apply
# default signal handlers to PID 1, and the shell exec'd by CMD doesn't always
# forward signals correctly. After the 10s grace period, Docker SIGKILLs and
# you may leave a pg_advisory_lock orphaned in the database.
# See https://github.com/krallin/tini for the canonical PID 1 problem write-up.
RUN apk add --no-cache tini
WORKDIR /app
# -----------------------------------------------------------------------------
# Project file copies
# -----------------------------------------------------------------------------
# project-package.json lives at /tmp, NOT /app, so npm never sees it. We only
# read it to extract the project's pinned Prisma versions; this keeps the
# migrate container in lockstep with whatever the main app pins, with no
# duplicate version strings to forget about updating.
COPY package.json /tmp/project-package.json
# The project lockfile sits next to the minimal manifest written below, so the
# install keeps the project's exact, integrity-checked versions; no floating
# transitive dependency slips in at build time.
COPY package-lock.json ./
# Schema, migrations, and seed JSON files. Used by `prisma generate` (compiles
# the client into src/generated/prisma at build time) and `prisma migrate
# deploy` + seed.mjs (at runtime).
COPY prisma ./prisma/
# The seed script imports isValidSeason (Zod schema) from src/validators/.
# isValidSeason in turn imports CAMPAIGN_STATUS / EVENT_STATUS from
# src/shared/enums/events.mjs (a pure-constants module with no further
# imports), so both files are copied. zod is installed below alongside prisma.
COPY src/validators/isValidSeason.mjs ./src/validators/isValidSeason.mjs
COPY src/shared/enums/events.mjs ./src/shared/enums/events.mjs
# Loaded by every prisma CLI invocation. Imports dotenv at top-of-file and
# calls env('POSTGRES_URL') to validate the connection string is present.
# Both dotenv and the prisma CLI must be installed for this file to load.
COPY prisma.config.mjs ./
# -----------------------------------------------------------------------------
# Install Prisma + minimal runtime deps in a single image layer
# -----------------------------------------------------------------------------
#
# Everything below is one big chained RUN so it lands in a single layer (each
# separate RUN creates a new immutable layer in the final image, and cleanup
# done in a later layer doesn't shrink earlier ones).
#
RUN \
# 1. Write a minimal package.json in /app: the project's ranges for ONLY
# these 5 packages, plus the project's `overrides`, read from the /tmp
# reference copy (`prisma` is a devDependency there, the rest are prod).
# - prisma: the CLI that runs `migrate deploy`
# - @prisma/client: runtime needed by seed.mjs (which imports
# PrismaClient from src/generated/prisma)
# - @prisma/adapter-pg: driver adapter the seed uses; Prisma 7 has no
# built-in pg driver, you must pass an adapter
# - dotenv: loaded synchronously by prisma.config.mjs at
# startup of every prisma CLI invocation
# - zod: used by src/validators/isValidSeason.mjs
# (imported by the seed script)
node -e "const p = require('/tmp/project-package.json'), all = { ...p.dependencies, ...p.devDependencies }; \
const deps = Object.fromEntries(['prisma', '@prisma/client', '@prisma/adapter-pg', 'dotenv', 'zod'].map((n) => [n, all[n]])); \
require('fs').writeFileSync('package.json', JSON.stringify({ name: 'migrate', version: '1.0.0', private: true, type: 'module', dependencies: deps, overrides: p.overrides }))" && \
\
# 2. Install them. The lockfile pins every version, transitive ones
# included, and npm checks its integrity hashes; everything the minimal
# manifest does not need is pruned, so the ~1.2 GB Next.js tree never
# installs. --no-audit --no-fund: skip noise that adds ~5-10 seconds.
npm install --no-audit --no-fund && \
\
# 3. Generate the Prisma client into src/generated/prisma (per the
# `output` field in prisma/schema.prisma's `generator client {}` block).
# POSTGRES_URL=dummy is needed because prisma.config.mjs validates the
# env var at config-load time; `prisma generate` doesn't actually
# connect to the database, it just parses the schema.
POSTGRES_URL=postgresql://dummy npx prisma generate && \
\
# 4. Cleanup. The /tmp version reference, the npm download cache, and any
# other tmp install artifacts. All in this same RUN layer so they don't
# stick around in the final image.
rm /tmp/project-package.json && \
npm cache clean --force && \
rm -rf /root/.npm /tmp/*
# -----------------------------------------------------------------------------
# Runtime configuration
# -----------------------------------------------------------------------------
# Drop privileges to the non-root `node` user (built into node:*-alpine
# images). Files in /app are owned by root because the install above ran as
# root, but the node user reads them fine — we never write to /app at runtime.
#
# Why no `chown -R node:node /app`: it would create a full duplicate of /app
# in a separate image layer, since chown changes every inode and Docker layers
# are content-addressed per file. The migrate slim-down (#278) measured this
# at ~1.4 GB of pure layer-doubling waste.
USER node
# tini becomes PID 1 and forwards signals to whatever CMD specifies as PID 2.
ENTRYPOINT ["/sbin/tini", "--"]
# Default command:
# 1. `prisma migrate deploy` — applies any pending SQL migrations from
# prisma/migrations/ in order. Idempotent: skips already-applied ones.
# 2. `node --experimental-strip-types prisma/seed/seed.mjs` — upserts
# historical season JSON files into h1_* tables. Also idempotent: it
# compares each file's season against the seasons already in h1_season
# and runs only the missing ones, so a deploy that adds one season file
# writes one season. Set FORCE_SEED=true on this container to re-upsert
# everything ("false" is correctly read as off).
#
# `&&` ensures the seed only runs if migrate succeeds. The container exits
# with the appropriate code.
CMD ["sh", "-c", "npx prisma migrate deploy && node --experimental-strip-types prisma/seed/seed.mjs"]