Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
149 lines (119 loc) · 6.53 KB
/
Copy pathDockerfile
File metadata and controls
149 lines (119 loc) · 6.53 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
# syntax=docker/dockerfile:1.28@sha256:bb22d9815c728170f72750f4e5b0d672e06176142e1d602c7e66c050100b7e5b
# ---------------------------------------------------------------------------
# OmniBot — production image
#
# Stages: base → deps (dev+prod) → build → prod-deps → migrator / runtime
# Build: docker build -t omnibot .
# ---------------------------------------------------------------------------
# Alpine keeps the runtime small. Prisma 7 has no native query engine any more
# (the client goes through node-postgres), so only the migrator carries a
# platform-specific binary: the schema engine, fetched for musl at install.
ARG NODE_IMAGE=node:26.13.1-alpine@sha256:6bd9ab43c0ec0190ca7b2d193906cb8b7edf6e7f4afa43ddeccd6452710bffb5
# --- base: pnpm, shared by every stage ------------------------------------
FROM ${NODE_IMAGE} AS base
ENV PNPM_HOME=/pnpm \
PATH=/pnpm:$PATH \
CI=1
WORKDIR /app
# Manifests first: dependency layers stay cached as long as they do not change.
# docs/site/package.json is only needed so that pnpm can validate the workspace
# against the lockfile — the docs site itself is never installed nor shipped.
# docker/migrator/package.json lists the migrator's dependencies.
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
COPY docs/site/package.json ./docs/site/package.json
COPY docker/migrator/package.json ./docker/migrator/package.json
# pnpm at the version `packageManager` pins in package.json (`pnpm@x.y.z`, a
# valid npm spec), installed with the image's npm: Node no longer ships
# corepack from v25 on.
RUN npm install --global "$(node -p 'require("./package.json").packageManager')"
# --- deps: full dependency graph (dev included), used to build -------------
FROM base AS deps
# The store cache is keyed per target platform: a multi-arch build runs this
# stage once per platform, and they must not share a store holding native
# binaries built for the other one.
ARG TARGETPLATFORM
RUN --mount=type=cache,id=pnpm-store-${TARGETPLATFORM},target=/pnpm/store,sharing=locked \
pnpm install --frozen-lockfile --filter omni-bot
# --- build: generate the Prisma client, compile TypeScript ---------------
FROM deps AS build
COPY tsconfig.json prisma.config.ts ./
COPY src ./src
# `pnpm build` = prisma generate → src/generated/prisma (from every *.prisma
# under src/),
# tsc → dist/
RUN pnpm build
# tsc only emits the .ts files it compiles, so the runtime assets have to be
# placed next to the JavaScript by hand:
# - i18n bundles (*.json) live next to their module sources,
# - image assets (fonts, pictures) live in each module's assets/ folder.
# The generated Prisma client is TypeScript under src/, so tsc already emitted
# it. Type declarations are dropped (useless at runtime); .js.map files are kept
# so that production stack traces stay readable.
RUN set -eux; \
find dist \( -name '*.d.ts' -o -name '*.d.ts.map' \) -delete; \
cd src; \
find . -type f \( -name '*.json' -o -path '*/assets/*' \) \
-not -path './prisma/*' -not -path './generated/*' \
-exec sh -c 'mkdir -p "/app/dist/$(dirname "$1")" && cp "$1" "/app/dist/$1"' _ {} \;
# --- prod-deps: runtime dependency tree only ------------------------------
FROM base AS prod-deps
ARG TARGETPLATFORM
RUN --mount=type=cache,id=pnpm-store-${TARGETPLATFORM},target=/pnpm/store,sharing=locked \
pnpm install --frozen-lockfile --prod --filter omni-bot
# --- migrator-deps: the Prisma CLI and nothing else -----------------------
# `pnpm deploy` extracts, from the frozen lockfile, the dependency tree of the
# docker/migrator workspace package: the Prisma CLI alone. Building on `deps`
# instead would ship the bot's whole dev tooling (TypeScript, lefthook, vite…)
# in an image that only applies migrations.
FROM base AS migrator-deps
ARG TARGETPLATFORM
RUN --mount=type=cache,id=pnpm-store-${TARGETPLATFORM},target=/pnpm/store,sharing=locked \
pnpm --filter omnibot-migrator deploy --prod /migrator
# --- migrator: short-lived image that applies the Prisma migrations -------
# It carries the Prisma CLI, prisma.config.ts (schema location and connection
# URL from DATABASE_URL), dbinfo.prisma and the migration history. Applying
# migrations needs the datasource, not the models: src/prisma alone is a valid
# schema. npm and npx are removed, as in the runtime image.
FROM ${NODE_IMAGE} AS migrator
RUN rm -r /usr/local/lib/node_modules \
&& rm /usr/local/bin/npm /usr/local/bin/npx
WORKDIR /app
COPY --from=migrator-deps /migrator ./
COPY prisma.config.ts ./
COPY src/prisma ./src/prisma
USER node
# The Prisma binary is invoked directly: the image has no package manager.
CMD ["/app/node_modules/.bin/prisma", "migrate", "deploy"]
# --- runtime: minimal production image ------------------------------------
FROM ${NODE_IMAGE} AS runtime
# tini: real init as PID 1 — forwards SIGTERM to node (the bot closes the
# Discord client and the Prisma pool on SIGTERM) and reaps zombies.
#
# The bot only ever runs `node`: npm and npx, shipped with the Node image, are
# removed. They are dead weight here, and their bundled dependencies are what
# an image scanner would otherwise keep reporting.
RUN apk add --no-cache tini font-noto-emoji \
&& rm -r /usr/local/lib/node_modules \
&& rm /usr/local/bin/npm /usr/local/bin/npx \
&& addgroup -S -g 10001 omnibot \
&& adduser -S -u 10001 -G omnibot -H -h /app omnibot
ENV NODE_ENV=production
WORKDIR /app
# `#*` subpath imports resolve through package.json; the `development`
# condition is deliberately NOT enabled, so they map to ./dist/*.
COPY --chown=root:root package.json ./
COPY --chown=root:root bootstrap ./bootstrap
COPY --from=prod-deps --chown=root:root /app/node_modules ./node_modules
COPY --from=build --chown=root:root /app/dist ./dist
# Everything is owned by root and read-only for the unprivileged runtime user.
USER omnibot
# The bot exposes no HTTP/TCP endpoint: every 30 s, it touches /tmp/omnibot-health
# when its gateway connection is ready and the database answers, and removes it
# otherwise (src/core/monitoring/). Healthy means touched less than 2 minutes
# ago, so a stuck process turns unhealthy too. The start period covers the
# connection to Discord, before the first heartbeat. /tmp is a tmpfs in
# compose.prod.yaml, the only writable path of the read-only root filesystem.
HEALTHCHECK --interval=30s --timeout=5s --start-period=2m --retries=3 \
CMD test -n "$(find /tmp/omnibot-health -mmin -2 2>/dev/null)"
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["sh", "-c", "node ./bootstrap/validate-env-vars.ts && exec node ./dist/index.js"]