Docker: standalone server, bundled worker, no caches in the image
Release / release (push) Failing after 10s

The runtime stage copied the whole production node_modules (495 MB, with
shadcn's CLI tree, the SWC compiler and sharp) and all of .next (163 MB, 119 MB
of it Turbopack's build cache). The image is now 427 MB instead of 1.04 GB:

- Next.js standalone output (NEXT_OUTPUT=standalone, set by the Dockerfile; a
  local `next build`/`next start` is unchanged): server.js with only the traced
  node_modules (39 MB). images.unoptimized (nothing uses next/image) keeps sharp
  and libvips out of the server's trace; /_next/image answers 404.
- build:worker bundles drizzle-orm, zod and ulid into dist/*.mjs and keeps only
  @libsql/client and libsql external; the standalone node_modules has both.
- npm's cache and .next/cache are BuildKit cache mounts: reused by the next
  build on the same machine (Next build 30 s -> 12 s after a code change), never
  in a layer.
- Release archives (BetterBench, both L80 tarballs) are fetched in their own
  stage; only the L80 binary is copied, BetterBench is installed from a bind
  mount.
- shadcn and tw-animate-css (CSS imports, build time only) move to
  devDependencies; package-lock.json regenerated with npm 11 (the image's npm).

Verified by building the image and running web, worker and sandbox from it:
pages and /api/live answer, the worker migrates, takes the lease and finds hf,
BetterBench and L80, all three healthchecks pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EooZ9biu7kFNB4eDJrwQc5
This commit is contained in:
Claude committed 2026-11-01 21:23:51 +00:00
1 parent 6b779debf7
commit 67cd0c945a
6 files changed
+440 -33

No files matched your search

+6
View File
@@ -36,6 +36,12 @@ step 1 (Hugging Face downloader) is released as a minor version.
- The **Model card** tab shows the page at once and the card when it arrives from the Hub.
- Database indexes for the queries the live view and the worker repeat every few seconds (the
migration runs by itself on start).
- **The image is 427 MB instead of 1.04 GB.** The web server runs as Next.js standalone output
(only the files it uses instead of all of `node_modules`), the worker is bundled, and neither the
build cache nor the downloaded archives end up in the image; build-only packages (`shadcn`,
`tw-animate-css`) are dev dependencies. The worker container used about 40 MB at idle instead of
about 190 MB in our test. Building the image again after a code change reuses npm's and Next's
caches (the Next build went from 30 to 12 seconds). Nothing changes in `compose.yaml` or `.env`.
## [0.16.0] - 2026-10-30
+35 -26
View File
@@ -9,43 +9,52 @@ FROM node AS build
ENV NEXT_TELEMETRY_DISABLED=1
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
# npm's download cache and Turbopack's build cache (.next/cache) are cache mounts: kept between
# builds on the same machine, never part of an image layer.
RUN --mount=type=cache,target=/root/.npm npm ci
COPY . .
RUN npm run build \
&& npm run build:worker \
&& npm prune --omit=dev
# Standalone output: the web server plus only the node_modules files it uses. The worker, migrate
# and sandbox scripts are bundled (esbuild) and only need @libsql/client from those.
RUN --mount=type=cache,target=/app/.next/cache \
NEXT_OUTPUT=standalone npm run build \
&& npm run build:worker
# --- downloads ---------------------------------------------------------------
# Pinned release archives, in their own stage: only what is installed from them reaches the image.
FROM python:3.13-slim-trixie AS downloads
# BetterBench (Apache-2.0), the benchmark run against endpoints. Not on PyPI: the release
# archive is pinned by checksum; flags used by src/worker/betterbench.ts are verified per version.
ARG BETTERBENCH_VERSION=0.6.1
ADD --checksum=sha256:f0a9d7fcc411c809ff87a0a22e2d76f6662b426431d2ae9fa273bc2644464db7 \
https://codeload.github.com/GGZ14/BetterBench/tar.gz/refs/tags/v${BETTERBENCH_VERSION} /downloads/betterbench.tar.gz
# L80 CLI (launch80.com): publishes a BetterBench run as a shareable page. Release archives pinned
# by checksum (from the release's checksums.txt); the per-architecture one is installed.
ARG L80_VERSION=0.4.0
ARG TARGETARCH
ADD --checksum=sha256:cde6b67dfb67b0ffddd56dfd384b8b24610573199287c59597359145310d9d23 \
https://github.com/launch80/L80-Skills/releases/download/v${L80_VERSION}/L80_linux_amd64.tar.gz /downloads/l80/amd64.tar.gz
ADD --checksum=sha256:bbd7c6cf35bcba2bf9a651f348581b691b36d5f7cdca9c8a11e8a1ac2fc3f5bc \
https://github.com/launch80/L80-Skills/releases/download/v${L80_VERSION}/L80_linux_arm64.tar.gz /downloads/l80/arm64.tar.gz
RUN mkdir -p /opt/l80 \
&& tar -xzf "/downloads/l80/${TARGETARCH:-amd64}.tar.gz" -C /opt/l80 L80 \
&& chmod 0755 /opt/l80/L80
# --- runtime -----------------------------------------------------------------
# Python base (provides the official `hf` CLI) plus the Node binary. The Python image already
# has CA certificates and libstdc++, so no apt.
FROM python:3.13-slim-trixie AS runtime
COPY --from=node /usr/local/bin/node /usr/local/bin/node
COPY --from=downloads /opt/l80/L80 /opt/l80/L80
# Pinned: the worker relies on the CLI's on-disk layout (see src/worker/hf-layout.ts);
# re-verify that before bumping.
ARG HF_HUB_VERSION=2.1.1
# BetterBench (Apache-2.0), the benchmark run against endpoints. Not on PyPI: the release
# archive is pinned by checksum; flags used by src/worker/betterbench.ts are verified per version.
ARG BETTERBENCH_VERSION=0.6.1
ADD --checksum=sha256:f0a9d7fcc411c809ff87a0a22e2d76f6662b426431d2ae9fa273bc2644464db7 \
https://codeload.github.com/GGZ14/BetterBench/tar.gz/refs/tags/v${BETTERBENCH_VERSION} /tmp/betterbench.tar.gz
# L80 CLI (launch80.com): publishes a BetterBench run as a shareable page. Release archives pinned
# by checksum (from the release's checksums.txt); the per-architecture one is installed.
ARG L80_VERSION=0.4.0
ARG TARGETARCH
ADD --checksum=sha256:cde6b67dfb67b0ffddd56dfd384b8b24610573199287c59597359145310d9d23 \
https://github.com/launch80/L80-Skills/releases/download/v${L80_VERSION}/L80_linux_amd64.tar.gz /tmp/l80/amd64.tar.gz
ADD --checksum=sha256:bbd7c6cf35bcba2bf9a651f348581b691b36d5f7cdca9c8a11e8a1ac2fc3f5bc \
https://github.com/launch80/L80-Skills/releases/download/v${L80_VERSION}/L80_linux_arm64.tar.gz /tmp/l80/arm64.tar.gz
RUN mkdir -p /opt/l80 \
&& tar -xzf "/tmp/l80/${TARGETARCH:-amd64}.tar.gz" -C /opt/l80 L80 \
&& chmod 0755 /opt/l80/L80 \
&& rm -rf /tmp/l80
RUN python -m venv /opt/hf \
RUN --mount=type=bind,from=downloads,source=/downloads/betterbench.tar.gz,target=/tmp/betterbench.tar.gz \
python -m venv /opt/hf \
&& /opt/hf/bin/pip install --no-cache-dir "huggingface_hub[hf_xet]==${HF_HUB_VERSION}" \
&& python -m venv /opt/betterbench \
&& /opt/betterbench/bin/pip install --no-cache-dir /tmp/betterbench.tar.gz \
&& rm -rf /root/.cache /tmp/betterbench.tar.gz \
&& rm -rf /root/.cache \
&& useradd --uid 1000 --create-home --shell /usr/sbin/nologin app \
&& mkdir -p /data /models /sandbox \
&& chown app:app /data /models \
@@ -65,9 +74,9 @@ ENV PATH=/opt/hf/bin:$PATH \
L80_BIN=/opt/l80/L80
WORKDIR /app
COPY --from=build --chown=app:app /app/package.json /app/next.config.ts ./
COPY --from=build --chown=app:app /app/node_modules ./node_modules
COPY --from=build --chown=app:app /app/.next ./.next
# server.js, package.json, the traced node_modules (also what dist/ needs: @libsql/client) and .next.
COPY --from=build --chown=app:app /app/.next/standalone ./
COPY --from=build --chown=app:app /app/.next/static ./.next/static
COPY --from=build --chown=app:app /app/dist ./dist
COPY --from=build --chown=app:app /app/drizzle ./drizzle
COPY --chmod=755 docker-entrypoint.sh docker-healthcheck.sh /usr/local/bin/
+1 -1
View File
@@ -55,7 +55,7 @@ case "$role" in
esac
case "$role" in
web) exec node_modules/.bin/next start ;;
web) exec node server.js ;; # Next.js standalone server (see Dockerfile)
worker) exec node dist/worker.mjs ;;
sandbox)
# It runs code written by the model under test: never as root (compose gives it `user:`).
+8
View File
@@ -3,6 +3,14 @@ import type { NextConfig } from "next";
const nextConfig: NextConfig = {
// Native SQLite driver: load from node_modules at runtime instead of bundling.
serverExternalPackages: ["@libsql/client", "libsql"],
// The Docker image runs the standalone server: only the files the server needs instead of all of
// node_modules (see Dockerfile). Local `next build` / `next start` stay as they are.
output: process.env.NEXT_OUTPUT === "standalone" ? "standalone" : undefined,
// Nothing uses next/image (model cards show the Hub's images as they are). With the optimizer off
// (/_next/image answers 404), sharp and its native libvips (28 MB) stay out of the standalone
// output; "next-server" is the key Next applies to its own server's trace.
images: { unoptimized: true },
outputFileTracingExcludes: { "next-server": ["**/node_modules/sharp/**/*", "**/node_modules/@img/**/*"] },
};
export default nextConfig;
+387 -3
View File
File diff suppressed because it is too large. Load diff
+3 -3
View File
@@ -14,7 +14,7 @@
"db:generate": "drizzle-kit generate",
"db:migrate": "dotenv -e .env -- tsx src/lib/db/migrate-cli.ts",
"test": "tsx --conditions=react-server --test \"src/**/*.test.ts\"",
"build:worker": "esbuild src/worker/index.ts --bundle --platform=node --target=node24 --format=esm --packages=external --outfile=dist/worker.mjs && esbuild src/lib/db/migrate-cli.ts --bundle --platform=node --target=node24 --format=esm --packages=external --outfile=dist/migrate.mjs && esbuild src/sandbox/index.ts --bundle --platform=node --target=node24 --format=esm --packages=external --outfile=dist/sandbox.mjs"
"build:worker": "esbuild src/worker/index.ts --bundle --platform=node --target=node24 --format=esm --external:@libsql/client --external:libsql --outfile=dist/worker.mjs && esbuild src/lib/db/migrate-cli.ts --bundle --platform=node --target=node24 --format=esm --external:@libsql/client --external:libsql --outfile=dist/migrate.mjs && esbuild src/sandbox/index.ts --bundle --platform=node --target=node24 --format=esm --external:@libsql/client --external:libsql --outfile=dist/sandbox.mjs"
},
"dependencies": {
"@base-ui/react": "^1.8.0",
@@ -33,9 +33,7 @@
"rehype-sanitize": "^6.0.0",
"remark-gfm": "^4.0.1",
"server-only": "^0.0.1",
"shadcn": "^4.21.1",
"sonner": "^2.0.8",
"tw-animate-css": "^1.4.0",
"ulid": "^3.0.2",
"zod": "^4.6.5"
},
@@ -51,8 +49,10 @@
"esbuild": "^0.25.12",
"eslint": "^9",
"eslint-config-next": "16.3.8",
"shadcn": "^4.21.1",
"tailwindcss": "^4",
"tsx": "^4.23.15",
"tw-animate-css": "^1.4.0",
"typescript": "^5"
}
}