Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
291 lines (272 loc) · 18.3 KB
/
Copy pathDockerfile
File metadata and controls
291 lines (272 loc) · 18.3 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# syntax=docker/dockerfile:1
# The self-host image: one Bun process, two SQLite files, one uploads directory. There is
# no database service to link, no migration step and no entrypoint script, because the app
# applies its own schema at boot and reads its whole configuration from the environment.
#
# Three decisions here are deliberate and each one exists to stop this file needing edits
# later. Read them before "simplifying" any of them away.
#
# 1. It runs FROM SOURCE (`bun src/index.ts`), not from the `bun build --compile` binary
# that `docs/self-host.md` uses. The binary is smaller, but it is also the one thing in
# this project that is fragile across base images: `--compile` bundles sharp's
# JavaScript and NOT its native module (see the note in `src/media/files.ts`), so the
# image would carry a binary pinned to one libc and a native addon that has to match it,
# and every base bump becomes an investigation. From source, sharp stays an ordinary
# entry in `node_modules` that `bun install` resolved for this platform. It is also
# exactly how the live box runs (see `src/server/build-info.ts`), so the container is
# not a second, separately-broken deployment shape.
#
# 2. Debian slim, not Alpine. sharp ships prebuilt binaries for glibc and musl separately,
# and the musl ones are the less travelled path for no gain that survives a rebuild.
# (`tar` used to be the other reason: the backup spawned it. Since ADR 0067 the archive is
# written and read in JavaScript, `src/server/tar.ts`, so the app no longer needs it; it is
# in Debian's essential set anyway, for an owner opening an old archive by hand.)
#
# 3. The data directories are created and chowned IN THE IMAGE. Docker seeds a fresh named
# volume from the image's own directory, ownership included, so `docker compose up` gives
# the unprivileged user a writable volume on first boot with nothing else running. That
# part is unchanged and still carries the common case.
#
# What it never covered is a BIND mount, which keeps the host's ownership — and that is
# the normal shape on the NAS boxes this image is meant to be easy on, because their
# container UIs mount a real folder and their backup jobs need to see it. Measured on a
# Linux daemon on 2026-08-21, a root-owned bind mount does not degrade, it kills the
# container on boot with `SQLITE_CANTOPEN` and a `bun:sqlite` stack trace — a diagnosis
# nobody should have to make from a NAS web UI. So there IS an entrypoint now
# (`docker-entrypoint.sh`): it adopts PUID/PGID, chowns only when ownership is actually
# wrong, and drops privileges with `setpriv` from util-linux, which Debian already has.
# `docker run --user 1000:1000` still bypasses all of it and behaves exactly as before.
#
# 4. The PRODUCTION dependencies are their own stage, and the two installs share BuildKit's
# cache mount. Measured on this repo: the dev tree is 186 MB and the production one 94 MB,
# and the old shape resolved BOTH inside the build stage — so `rm -rf node_modules &&
# bun install --production` sat AFTER `COPY src`, and every one-line edit to the app
# re-ran a full production install that nothing about the edit had changed. Now the
# production set depends on `package.json` and `bun.lock` alone: an upgrade that only
# moves source code reuses it, and the two installs stream from one warm package cache
# instead of two cold downloads.
# --- deps: what the SERVER needs, and nothing the build needed -----------------------------
#
# Its own stage rather than a prune at the end of the build, so it depends on the manifest
# and the lockfile alone. A source-only upgrade — which is most of them — reuses this layer
# whole. Installing into an EMPTY directory is still what makes it a production set:
# `bun install --production` over a dev `node_modules` that already matches the lockfile
# reports "no changes" and removes nothing, which is how the image once shipped React,
# Tiptap, Tailwind and TypeScript and weighed 535 MB.
FROM oven/bun:1-slim AS deps
WORKDIR /app
COPY package.json bun.lock ./
RUN --mount=type=cache,target=/root/.bun/install/cache,sharing=locked \
bun install --frozen-lockfile --production
# What the SERVER never opens, deleted in the layer that produced it. Measured on this repo:
# 95 MB of production dependencies becomes 67 MB, which is 28 MB off the image and off every
# pull of it.
#
# Each of these is dead at runtime for a specific reason, not because it looked like clutter:
# `.map` files are read by a debugger attaching to the process and by nothing else; `.d.ts`
# declarations are consumed by `tsc`, which does not run here (Bun transpiles and does not
# typecheck); the prose is prose. Nothing is deleted for being a test fixture or an example,
# because a package is allowed to read its own files at runtime and there is no way to know
# from the outside which ones do.
#
# PROVED BY RUNNING IT, not by reasoning: the app was booted against a tree pruned exactly
# this way and exercised where these files would be missed — a post with two highlighted code
# blocks rendered (shiki resolves its grammars from `node_modules` at runtime), an image
# variant was served (sharp's native module), the admin answered 200, and the log carried no
# resolution error. The reasoning above is why it is safe; that run is why we know.
#
# THE LICENCES ARE NOT PROSE, and `-iname '*.md'` does not know the difference. Eight packages
# in the production tree carry their copyright notice ONLY as `LICENSE.md` — jose, qs, ms,
# express-rate-limit, @img/colour, @shikijs/vscode-textmate, css-to-react-native,
# json-schema-typed — and MIT, BSD-2 and BSD-3 all require that notice to travel with every
# copy. This image IS a copy, published to two public registries, so deleting them is not a
# size decision anybody gets to make. Keeping every licence file in the tree costs 172 KB
# against the 28 MB above; the eight that would otherwise vanish cost far less than that.
RUN find node_modules -type f \( \
-name '*.map' -o -name '*.d.ts' -o -name '*.d.mts' -o -name '*.d.cts' \
-o -iname 'README*' -o -iname 'CHANGELOG*' -o -iname '*.md' \
\) \
! -iname 'LICENSE*' ! -iname 'LICENCE*' ! -iname 'COPYING*' ! -iname 'NOTICE*' \
-delete
# The MUSL half of sharp, which this image can never load.
#
# Decision 2 above chose Debian over Alpine because of exactly this split — and then shipped
# both halves anyway. `bun install` resolves sharp's platform packages as optionals and takes
# every libc variant for the architecture, so a glibc image carries
# `@img/sharp-libvips-linuxmusl-*` (18 MB) and `@img/sharp-linuxmusl-*` (1 MB) that the
# dynamic linker could not load if it tried: a `.node` built against musl does not open on
# glibc. Not a size trade-off, then — dead files, 19 MB of them, on every pull.
#
# sharp finds its binary by attempting each candidate and catching the failure, so an absent
# package takes the same path an incompatible one would. PROVED by uploading a real image
# through `/api/media/upload` in this image with these directories gone, and reading back the
# variants sharp produced from it.
RUN rm -rf node_modules/@img/sharp-libvips-linuxmusl-* node_modules/@img/sharp-linuxmusl-*
# --- build: the production tree again, only to produce the bundles -------------------------
FROM oven/bun:1-slim AS build
WORKDIR /app
# Dependencies first, so an edit to `src/` does not re-resolve the whole tree.
#
# --production HERE TOO, because neither build reads a devDependency: both are Bun's bundler over
# this repository's own TypeScript, and their output on the production set was compared with the
# full install's and is byte-identical. The full set is the workshop — TypeScript, the tests' DOM,
# and since the Cloudflare runtime arrived wrangler with its own workerd. Measured in this base
# image on linux/arm64: 357 MB with 133 MB of workerd, against 113 MB, so every build of this
# image, each architecture of a release among them, was downloading 244 MB it never opened.
COPY package.json bun.lock ./
RUN --mount=type=cache,target=/root/.bun/install/cache,sharing=locked \
bun install --frozen-lockfile --production
COPY tsconfig.json ./
COPY src ./src
COPY locales ./locales
COPY scripts ./scripts
# Neither output is committed (`.gitignore` ignores every `dist/`), so both are built here:
# the reader's island bundles, and the admin's own with its stylesheet. Both are Bun's bundler
# over this repository's TypeScript and nothing else — React, Tiptap and the Tailwind CLI were
# what this stage used to install for, and all three left in September 2026.
RUN bun run build:assets && bun run build:admin
# WHAT THE SERVER OPENS, and nothing that only the BUILD opened. The runtime stage copies
# `src` whole, so until now it copied the workshop with it. Measured inside the published
# image on 2026-09-20: `src` is 10.9 MB, of which 291 test files are 2.5 MB, the admin's
# browser half is 1.6 MB of TypeScript already bundled into `src/admin/dist`, and the
# reader's islands are 316 KB already bundled into `src/assets/dist`.
#
# None of it is reachable at runtime, and that is three separate facts rather than an
# impression: no file outside those two trees imports `@/admin/...` or `@/assets/js/...`,
# `web/admin/spa.ts` serves `admin/dist`, and `web/assets.ts` imports `assets/dist/*.js` as
# text. `src/admin/admin.css` and `utilities.css` go the same way: `build-admin.ts`
# concatenates them INTO `dist/admin.css`, which is the one the fingerprinted route reads.
#
# Deleted HERE rather than in the runtime stage, because a delete in a later layer does not
# take the bytes out of an earlier one. The COPY below has to never carry them at all.
RUN find src -name '*.test.ts' -delete \
&& find src/admin -mindepth 1 -maxdepth 1 ! -name dist -exec rm -rf {} + \
&& rm -rf src/assets/js
# --- runtime -----------------------------------------------------------------------------
FROM oven/bun:1-slim
WORKDIR /app
# HOST=0.0.0.0 is REQUIRED here and is the one place it is right by default. The app defaults
# to 127.0.0.1 (`src/env.ts`), which is correct for a native install with nginx on the same
# machine and completely wrong inside a container: loopback there is the container's own, so
# nothing outside it could ever connect and the published port would answer refused. What
# keeps that from meaning "exposed to the internet" is the PUBLISH side — compose binds
# 127.0.0.1:3000:3000 on the host, for the reasons written beside it in docker-compose.yml.
# QUIREINK_PACKAGE says which of the three packages this is (ADR 0065), so the admin shows the
# upgrade that applies here (pull the image) and `/api/health` can tell an install test so.
ENV NODE_ENV=production \
QUIREINK_PACKAGE=docker \
HOST=0.0.0.0 \
PORT=3000 \
DATA_DIR=/var/lib/quire/data \
STORAGE_LOCAL_DIR=/var/lib/quire/uploads
COPY --from=deps /app/node_modules ./node_modules
COPY --from=build /app/src ./src
COPY --from=build /app/locales ./locales
# THE SCRIPTS A RUNNING BLOG USES, not the whole directory.
#
# `COPY --from=build /app/scripts` shipped 4.1 MB of workshop to every install: 3.2 MB of
# public-domain paintings that only the demo seeder reads, the twelve static guards, the 211
# tour flows, the screenshot drivers. Measured 2026-09-16; the published image is 87 MB
# compressed, and JPEGs do not compress twice, so the paintings alone were about 4% of what
# every person pulls. Nothing in `src/` reads any of it and the entrypoint never seeds.
#
# What stays is what the docs tell an operator to run: the backup and uptime scripts
# (`docs/self-host.md`), the owner-account CLI (`bun run user`), the pen sheet, `backup-decrypt.ts`
# (2.2.14), and `restore.ts` with the `restore-lib.ts` it imports, which are the ones that have to
# be here on the worst day. ADR 0035 keeps the restore a shell act on a STOPPED service, so a tool
# that only existed in a git checkout is a tool nobody has when they need it — and that is what
# the restore was until it was looked for in this image: `bun scripts/restore.ts` answered
# `Module not found` inside the container, while every doc called restoring one command. The
# matrix restored the image's archive with the CHECKOUT's script, so nothing went red.
# `src/install/image-scripts.test.ts` now holds every script copied here to the files it imports.
# The BUILD stage still gets the whole directory, because that is where `build:assets` and
# `build:admin` live.
#
# `ops/` is named file by file rather than copied whole, because two of its six are
# workshop too: `tour.sh` and `shoot-readme.sh` drive a browser and call `seed-showcase.ts`
# and `tour.ts`, none of which is in the image. Shipping a script that cannot run is how an
# operator ends up reporting a bug against a tool nobody meant them to have.
COPY --from=build /app/scripts/ops/quire-backup.sh /app/scripts/ops/quire-uptime.sh ./scripts/ops/
COPY --from=build /app/scripts/user.ts /app/scripts/pen-sheet.ts /app/scripts/backup-decrypt.ts \
/app/scripts/restore.ts /app/scripts/restore-lib.ts ./scripts/
COPY package.json bun.lock tsconfig.json ./
# See note 3 above. Both paths are ENV defaults, so overriding them in compose without
# mounting something writable there is the one way to get this wrong.
RUN mkdir -p "$DATA_DIR" "$STORAGE_LOCAL_DIR" && chown -R bun:bun /var/lib/quire
# NO `USER bun` HERE, and that is the entrypoint's whole reason for existing: a process that
# is already unprivileged cannot fix the ownership of a bind mount it cannot write to. The
# container starts as root, spends one shell script adopting PUID/PGID (1000:1000 by
# default, which is `bun` — so the default is byte-for-byte the old behaviour), and execs
# the app as that user. The app itself never runs as root, and compose keeps
# `no-new-privileges` on top of that.
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
# WHAT THIS IMAGE IS, in the fields every tool reads.
#
# The base image sets nine of these and until 2026-09-20 this one inherited all nine, so a
# published Quire Ink container described itself as title `bun`, version `1.4.2-slim`, source
# `https://github.com/oven-sh/bun`, licence NOASSERTION, and a build date belonging to
# somebody else's build. Read off the published 2.2.12 manifest, both architectures.
#
# Not cosmetic for the audience this image is for. `image.source` is the field a registry
# reads to link a package to its repository, so the GHCR page for this image pointed at Bun's;
# and Watchtower, Diun and a NAS container UI read these same labels to tell an owner what
# they are running and whether it has moved. An upgrade notifier comparing `image.version`
# was comparing Bun's.
#
# `image.version` is now one of the places the release checklist names, and `check:docs`
# holds it to `package.json`. `revision` and `created` are facts about one build rather than
# about the source, so they arrive as build arguments; a local `docker compose up --build`
# leaves them empty, which is honest, where inheriting was not.
ARG SOURCE_COMMIT=""
ARG BUILD_DATE=""
LABEL org.opencontainers.image.title="Quire Ink" \
org.opencontainers.image.description="A self-hosted blog: one Bun process, two SQLite files, no build step." \
org.opencontainers.image.version="2.2.23" \
org.opencontainers.image.url="https://quireink.com" \
org.opencontainers.image.source="https://github.com/joiha-steven/quireink" \
org.opencontainers.image.documentation="https://github.com/joiha-steven/quireink#readme" \
org.opencontainers.image.licenses="AGPL-3.0-only" \
org.opencontainers.image.vendor="Quire Ink" \
org.opencontainers.image.revision="${SOURCE_COMMIT}" \
org.opencontainers.image.created="${BUILD_DATE}"
# How the official MCP registry proves this image is ours: the value must match `name` in
# `server.json`, and the registry reads it off the published manifest. It is a label and
# nothing reads it at runtime, so it costs a layer of metadata and no bytes that run.
LABEL io.modelcontextprotocol.server.name="com.quireink/blog"
EXPOSE 3000
# The probe lives HERE rather than in `docker-compose.yml`, so `docker run` gets it too and
# there is one definition to keep true. `/api/health` checks the two things that actually
# stop this app serving — the database answers, and the upload store is writable — and it
# reports 503 rather than failing, which is the whole point of probing it.
#
# It costs a Bun start per tick, so the interval is 60s rather than 30: this app dies by
# disk or by database, not by hanging, and on a 1 GB box a second runtime every half minute
# is a real slice of a small machine. `--smol` runs that probe in the low-memory heap mode.
# curl is not in the image and does not need to be.
HEALTHCHECK --interval=60s --timeout=5s --start-period=20s --retries=3 \
CMD bun --smol --eval "process.exit((await fetch('http://127.0.0.1:'+(process.env.PORT||3000)+'/api/health')).ok ? 0 : 1)"
# `bun:sqlite` is synchronous and single-threaded by design (one writer by construction),
# so this is one process and never a cluster. Scale the box, not the process count.
#
# `--smol` IS NOT A CONCESSION, IT IS FASTER, and this container is the reason it has to be
# here rather than left to the operator: JavaScriptCore sizes its heap from the MACHINE's
# memory and cannot see a cgroup limit, so inside `--memory=128m` on an 8 GB host it believes
# it has 8 GB and lets garbage pile up until the limit stops it. Measured 2026-09-21 on the
# demo fixture, 33 posts and 18 images, `--memory=128m --cpus=0.25`:
#
# default 121.5 MB of 128, pinned at 100% of its CPU, the boot warm STILL UNFINISHED
# after twenty minutes, and the home page taking 16.5 seconds
# --smol 55.1 MB, 0.05% CPU, the warm done in 379 ms, the home page in 5 ms
#
# The failure is worse than a crash: it is never OOM-killed, so nothing restarts it and the
# site is down while the container reports healthy. The same fixture given 512 MB and the same
# quarter CPU warms in 1.6 seconds, which is what says the work was never the problem.
#
# And it costs nothing where there is room. 3,000 requests at 32 concurrent, 2 GB and 2 CPUs:
# 4,995 and 5,115 req/s by default against 5,962 and 6,234 with `--smol`, p50 5.4 ms against
# 4.3 ms. A compact heap collects less and fits in cache; the flag is 20% FASTER here.
#
# Nothing reads it: `clockBlockedBy` and the update check scan `process.execArgv` for
# `--watch` and `--hot` only, so the clock still winds (ADR 0031).
CMD ["bun", "--smol", "src/index.ts"]