Repository navigation
Expand file tree
/
Copy pathdockerfile.unified
More file actions
268 lines (231 loc) · 12.4 KB
/
Copy pathdockerfile.unified
File metadata and controls
268 lines (231 loc) · 12.4 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# ReadMeABook - Unified Dockerfile
# Single container with PostgreSQL, Redis, and Next.js app
# Designed for easy deployment with minimal configuration
# Documentation: documentation/deployment/unified.md
# Static ffmpeg/ffprobe source (static-pie, no runtime deps, GPLv3 build without nonfree/libfdk-aac).
# Pinned to a multi-arch (amd64 + arm64) index digest; BuildKit copies the variant matching
# TARGETPLATFORM. Other platforms fail here ("no match for platform"). Multi-arch approach
# from PR #241 (TaylorMcGinnis). Bump: update tag + digest here AND FFMPEG_VERSION below.
FROM mwader/static-ffmpeg:9.0.2-1@sha256:f5d100184884eb3e82b0eb14bb5ebf98da99826ac824989720297fb9ef21c822 AS ffmpeg
# Stages (multi-stage layout from PR #279, ChaoticSciuridae):
# base node:24-bookworm + PostgreSQL/Redis/supervisord + pinned ffmpeg (runtime OS layer)
# builder node:24-bookworm + full npm ci, prisma generate, next build (never shipped)
# prisma-cli node:24-bookworm + the Prisma CLI closure installed from package-lock.json (never shipped)
# runtime base + standalone app, generated client, CLI tree, scripts/, docker/unified/*
# builder and prisma-cli MUST stay on the same distro as base and build for TARGETPLATFORM
# (never --platform=$BUILDPLATFORM): Prisma engines and bcrypt are native and arch-specific.
# Runtime OS layer: debian with node preinstalled
FROM node:24-bookworm AS base
# Install PostgreSQL 16 repository key
RUN apt-get update && apt-get install -y curl gnupg && \
install -d /etc/apt/keyrings && \
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /etc/apt/keyrings/postgresql.gpg && \
echo "deb [signed-by=/etc/apt/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt bookworm-pgdg main" > /etc/apt/sources.list.d/postgresql.list
# Install PostgreSQL, Redis, supervisord, and gosu (for reliable user switching)
RUN apt-get update && apt-get install -y \
postgresql-16 \
postgresql-client-16 \
redis-server \
supervisor \
curl \
openssl \
locales \
gosu \
xz-utils \
&& sed -i 's/^# \(en_US.UTF-8 UTF-8\)/\1/' /etc/locale.gen \
&& locale-gen en_US.UTF-8 \
&& update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 \
&& rm -rf /var/lib/apt/lists/*
# Install static ffmpeg + ffprobe from the pinned "ffmpeg" stage (no transitive deps like imagemagick).
# versions.json lists the bundled library versions (GPL source traceability).
ARG FFMPEG_VERSION=9.0.2
COPY --from=ffmpeg /ffmpeg /ffprobe /usr/local/bin/
COPY --from=ffmpeg /versions.json /usr/local/share/ffmpeg/versions.json
# Guard: binaries must match the build arch (ELF e_machine byte; a wrong-arch binary can still
# run under QEMU, see issue #230) and the pinned version (catches a tag/digest mismatch).
RUN set -eu; \
case "$(uname -m)" in \
x86_64) want=3e ;; \
aarch64) want=b7 ;; \
*) echo "ERROR: no pinned static ffmpeg for $(uname -m) (supported: amd64, arm64)" >&2; exit 1 ;; \
esac; \
for bin in ffmpeg ffprobe; do \
got="$(od -An -t x1 -j 18 -N 1 "/usr/local/bin/$bin" | tr -d ' \n')"; \
[ "$got" = "$want" ] || { echo "ERROR: $bin e_machine 0x$got, expected 0x$want for $(uname -m)" >&2; exit 1; }; \
ver="$("/usr/local/bin/$bin" -version | head -n 1)"; \
case "$ver" in \
"$bin version $FFMPEG_VERSION "*) echo "OK: $ver ($(uname -m))" ;; \
*) echo "ERROR: expected $bin $FFMPEG_VERSION, got: $ver" >&2; exit 1 ;; \
esac; \
done
# Remove imagemagick (pre-installed in node:24-bookworm base image, not needed)
RUN apt-get purge -y imagemagick imagemagick-6-common 'imagemagick-6*' \
'libmagickcore*' 'libmagickwand*' && \
apt-get autoremove -y --purge && \
rm -rf /var/lib/apt/lists/*
ENV LANG=en_US.UTF-8
ENV LC_ALL=en_US.UTF-8
# ============================================================================
# builder: full dependency tree + Next.js standalone build (never shipped)
# ============================================================================
FROM node:24-bookworm AS builder
# Re-declare build arguments after FROM (ARGs before FROM are not available after)
ARG APP_VERSION=unknown
ARG GIT_COMMIT=unknown
ARG BUILD_DATE=unknown
# Must stay /app: the generated Prisma client records absolute paths from generate time.
WORKDIR /app
# Copy package files
COPY package.json package-lock.json* ./
COPY prisma ./prisma/
# Install ALL dependencies (including dev dependencies needed for build)
RUN npm ci && npm cache clean --force
# Copy application code (before generating Prisma to avoid overwriting)
COPY . .
# Generate Prisma client AFTER copying code (prevents stale generated files from overwriting).
# The dummy URL exists only in this stage; the runtime image never carries it.
ENV DATABASE_URL="postgresql://dummy:dummy@localhost:5432/dummy?schema=public"
RUN ./node_modules/.bin/prisma generate
# Version variables: NEXT_PUBLIC_* are inlined into the client bundle by next build (VersionBadge)
ENV NEXT_PUBLIC_APP_VERSION=${APP_VERSION}
ENV NEXT_PUBLIC_GIT_COMMIT=${GIT_COMMIT}
ENV NEXT_PUBLIC_BUILD_DATE=${BUILD_DATE}
ENV APP_VERSION=${APP_VERSION}
ENV GIT_COMMIT=${GIT_COMMIT}
ENV BUILD_DATE=${BUILD_DATE}
# Build Next.js application (output: 'standalone' → .next/standalone/server.js + traced node_modules)
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
ENV TURBOPACK=0
RUN npm run build
# ============================================================================
# prisma-cli: the Prisma CLI closure, installed from package-lock.json (never shipped)
# ============================================================================
# The CLI is a devDependency, so standalone output omits it; the entrypoint needs it for
# `db push` / `db execute`. prisma-cli-lock.js copies prisma's transitive entries (version,
# resolved, sha512 integrity) out of the app lockfile, so npm ci installs exactly the locked
# tarballs. Only prisma + @prisma/engines may run install scripts; @prisma/engines downloads
# its schema/query engines from binaries.prisma.sh, checked against that host's sha256.
FROM node:24-bookworm AS prisma-cli
WORKDIR /opt/prisma-cli
RUN --mount=type=bind,source=package-lock.json,target=/tmp/app/package-lock.json \
--mount=type=bind,source=docker/unified/prisma-cli-lock.js,target=/tmp/app/prisma-cli-lock.js \
node /tmp/app/prisma-cli-lock.js /tmp/app/package-lock.json /opt/prisma-cli
RUN npm ci --ignore-scripts --no-audit --no-fund && \
npm rebuild prisma @prisma/engines && \
rm -rf node_modules/.package-lock.json node_modules/@prisma/engines/node_modules/.cache && \
npm cache clean --force
# Guard: installed CLI is the lockfile version and exactly one executable schema engine exists.
RUN set -eu; \
want="$(node -p "require('./package.json').dependencies.prisma")"; \
got="$(node -p "require('./node_modules/prisma/package.json').version")"; \
[ "$got" = "$want" ] || { echo "ERROR: prisma $got installed, lockfile says $want" >&2; exit 1; }; \
set -- node_modules/@prisma/engines/schema-engine-*; \
[ "$#" -eq 1 ] && [ -x "$1" ] || { echo "ERROR: expected one executable schema engine, got: $*" >&2; exit 1; }; \
echo "OK: prisma $got with $1"
# ============================================================================
# runtime: the shipped image
# ============================================================================
FROM base AS runtime
# Re-declare build arguments after FROM (ARGs before FROM are not available after)
ARG APP_VERSION=unknown
ARG GIT_COMMIT=unknown
ARG BUILD_DATE=unknown
# Set version environment variables for runtime
ENV NEXT_PUBLIC_APP_VERSION=${APP_VERSION}
ENV NEXT_PUBLIC_GIT_COMMIT=${GIT_COMMIT}
ENV NEXT_PUBLIC_BUILD_DATE=${BUILD_DATE}
ENV APP_VERSION=${APP_VERSION}
ENV GIT_COMMIT=${GIT_COMMIT}
ENV BUILD_DATE=${BUILD_DATE}
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
ENV TURBOPACK=0
WORKDIR /app
# Next.js standalone output: server.js, .next/server, traced node_modules (bcrypt, bull, ...).
# next.config.ts outputFileTracingExcludes keeps the whole-project trace's source/tests/docs out.
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public
# npm run rmab:recover needs the full package.json
COPY --from=builder /app/package.json ./package.json
# Schema + run-once data migrations applied by the entrypoint
COPY --from=builder /app/prisma ./prisma
# Whole generated client: credential recovery requires src/generated/prisma/client, and the
# bundled app finds its query engine via process.cwd()/src/generated/prisma
COPY --from=builder /app/src/generated/prisma ./src/generated/prisma
# Prisma CLI tree in ONE copy (keeps the .bin/prisma symlink; a lone copy of .bin/prisma becomes
# a regular file and the CLI can no longer find its wasm). Overlaps with the standalone tree are
# the same lockfile entries, so the merge cannot mix versions.
COPY --from=prisma-cli /opt/prisma-cli/node_modules ./node_modules
# Credential recovery (documentation/admin-features/credential-recovery.md)
COPY --chown=root:root scripts ./scripts
# Native prebuilds for other platforms: bcrypt 6 ships prebuilds/<platform>-<arch>/ for every
# platform and tracing copies the whole folder. node-gyp-build only loads prebuilds/linux-<arch>/,
# so drop the rest; the runtime guard then requires every ELF file in /app to match the build arch.
RUN set -eu; \
case "$(uname -m)" in \
x86_64) keep=linux-x64 ;; \
aarch64) keep=linux-arm64 ;; \
*) echo "ERROR: no prebuild mapping for $(uname -m) (supported: amd64, arm64)" >&2; exit 1 ;; \
esac; \
find /app/node_modules -type d -name prebuilds -prune | while read -r dir; do \
for p in "$dir"/*; do \
[ -e "$p" ] || continue; \
[ "$(basename "$p")" = "$keep" ] || { rm -rf "$p"; echo "removed $p"; }; \
done; \
done
# Create necessary directories
RUN mkdir -p \
/var/run/postgresql \
/var/lib/postgresql/data \
/var/lib/redis \
/app/config \
/app/cache \
/downloads \
/media \
/app/.next/cache \
/var/log/supervisor
# Setup PostgreSQL
RUN chown -R postgres:postgres /var/lib/postgresql /var/run/postgresql && \
chmod 2777 /var/run/postgresql
# Setup Redis data directory
RUN chown -R redis:redis /var/lib/redis
# Setup app directories (node user)
RUN chown -R node:node /app/config /app/cache /downloads /media /app/.next/cache
# Copy supervisord configuration
COPY --chown=root:root docker/unified/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# Copy entrypoint script
COPY --chown=root:root docker/unified/entrypoint.sh /entrypoint.sh
# Convert line endings (Windows CRLF -> Unix LF) and make executable
RUN sed -i 's/\r$//' /entrypoint.sh && chmod +x /entrypoint.sh
# Copy app startup wrapper
COPY --chown=root:root docker/unified/app-start.sh /app/app-start.sh
# Convert line endings and make executable
RUN sed -i 's/\r$//' /app/app-start.sh && chmod +x /app/app-start.sh
# Copy redis startup wrapper
COPY --chown=root:root docker/unified/redis-start.sh /app/redis-start.sh
# Convert line endings and make executable
RUN sed -i 's/\r$//' /app/redis-start.sh && chmod +x /app/redis-start.sh
# Copy postgres startup wrapper
COPY --chown=root:root docker/unified/postgres-start.sh /app/postgres-start.sh
# Convert line endings and make executable
RUN sed -i 's/\r$//' /app/postgres-start.sh && chmod +x /app/postgres-start.sh
# Guard (offline, final stage): runs the entrypoint's own Prisma CLI preflight block, then
# docker/unified/runtime-guard.js (bind-mounted, never in a layer) with the entrypoint's exact
# PRISMA_CLI: CLI/client/engine versions agree, the schema engine renders the schema, credential
# recovery loads its client, bcrypt and scripts/lib, every bare require() in .next/server
# resolves (app externals also load), bull's Lua commands are present, no non-runtime
# source/docs/tests are in /app, the postgres/redis/node account IDs are unchanged, and every ELF
# file in /app (+ ffmpeg/ffprobe) matches the build arch. Failure meanings: unified.md.
RUN --network=none \
--mount=type=bind,source=docker/unified/runtime-guard.js,target=/tmp/guard/runtime-guard.js \
node /tmp/guard/runtime-guard.js
# Expose app port
EXPOSE 3030
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD curl -f http://localhost:3030/api/health || exit 1
# Set entrypoint
ENTRYPOINT ["/entrypoint.sh"]
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]