Skip to content

Commit 878ea22

Browse files
committed
fix(docker): rewrite CAS Dockerfile with turbo prune stage
pnpm deploy --legacy failed with ERR_PNPM_WORKSPACE_PKG_NOT_FOUND because @cfxdevkit/executor (workspace:* dep of cas-backend) wasn't in the Docker build context — only apps/cas/backend/package.json was copied. Fix: add a Stage 0 (pruner) that runs 'turbo prune @cfxdevkit/cas-backend --docker'. This outputs out/json/ (all needed package.json manifests) and out/full/ (full source tree) for the minimal workspace subset. The builder stage installs from the pruned manifests, giving pnpm deploy --legacy the full workspace context it needs to resolve workspace:* deps. Also add 'pnpm rebuild better-sqlite3' in the deploy dir after pnpm deploy, since pnpm deploy does a fresh install from the content-addressable store into a new location and the native binary needs to be recompiled there.
1 parent 52d9a5c commit 878ea22

1 file changed

Lines changed: 30 additions & 16 deletions

File tree

apps/cas/backend/Dockerfile

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,59 @@
11
# syntax=docker/dockerfile:1
22
# ─────────────────────────────────────────────────────────────────────────────
3-
# CAS backend — multi-stage ARM64-native build
3+
# CAS backend — Turborepo-aware multi-stage ARM64-native build
44
#
5-
# Build:
5+
# Build (from monorepo root):
66
# docker build -t cas-backend -f apps/cas/backend/Dockerfile .
77
#
88
# Run:
99
# docker run --rm -p 3001:3001 -e CAS_API_KEY=secret cas-backend
1010
# ─────────────────────────────────────────────────────────────────────────────
1111

12+
# ── Stage 0: prune ────────────────────────────────────────────────────────────
13+
# turbo prune creates a minimal workspace subset containing only the packages
14+
# needed for @cfxdevkit/cas-backend (including @cfxdevkit/executor and its
15+
# transitive workspace deps). Without this, pnpm deploy --legacy fails with
16+
# ERR_PNPM_WORKSPACE_PKG_NOT_FOUND when trying to resolve workspace:* deps.
17+
FROM node:22-slim AS pruner
18+
WORKDIR /app
19+
RUN corepack enable && corepack prepare pnpm@10.11.0 --activate
20+
COPY . .
21+
RUN pnpm dlx turbo prune @cfxdevkit/cas-backend --docker
22+
1223
# ── Stage 1: build ────────────────────────────────────────────────────────────
1324
FROM node:22-slim AS builder
1425

1526
WORKDIR /app
1627

17-
# Install build tools required by native modules (better-sqlite3 → node-gyp)
28+
# Build tools for native modules (better-sqlite3 → node-gyp)
1829
RUN apt-get update && apt-get install -y --no-install-recommends \
1930
python3 make g++ \
2031
&& rm -rf /var/lib/apt/lists/*
2132

22-
# Install pnpm
2333
RUN corepack enable && corepack prepare pnpm@10.11.0 --activate
2434

25-
# Copy workspace manifests first for layer-cache efficiency
26-
COPY package.json pnpm-workspace.yaml pnpm-lock.yaml ./
27-
COPY tsconfig.base.json ./
28-
COPY apps/cas/backend/package.json ./apps/cas/backend/
29-
30-
RUN pnpm install --frozen-lockfile --filter @cfxdevkit/cas-backend
35+
# Install deps from the pruned manifest set (all needed package.json files present)
36+
COPY --from=pruner /app/out/json/ .
37+
COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
38+
RUN pnpm install --frozen-lockfile
3139

32-
# Compile better-sqlite3 native binding (not compiled during install —
33-
# removed from onlyBuiltDependencies so Vercel frontend builds don't break)
40+
# Compile better-sqlite3 native binding (not in onlyBuiltDependencies so
41+
# Vercel frontend builds don't fail — rebuild explicitly here where Python exists)
3442
RUN pnpm rebuild better-sqlite3
3543

36-
COPY apps/cas/backend/ ./apps/cas/backend/
37-
44+
# Copy full pruned source tree and build the app
45+
COPY --from=pruner /app/out/full/ .
3846
RUN pnpm --filter @cfxdevkit/cas-backend build
3947

40-
# Create a production-only flat bundle (includes compiled native bindings)
48+
# Create prod bundle — workspace deps resolvable now that prune provided them
4149
RUN pnpm deploy --filter @cfxdevkit/cas-backend --prod --legacy /app/deploy
4250

51+
# Re-compile better-sqlite3 in the deploy dir (pnpm deploy re-installs from
52+
# the content-addressable store into a new location; binary needs a rebuild)
53+
WORKDIR /app/deploy
54+
RUN pnpm rebuild better-sqlite3
55+
WORKDIR /app
56+
4357
# ── Stage 2: runtime ─────────────────────────────────────────────────────────
4458
FROM node:22-slim AS runtime
4559

@@ -48,7 +62,7 @@ LABEL org.opencontainers.image.title="cas-backend" \
4862

4963
WORKDIR /app
5064

51-
# Copy production node_modules from deploy bundle (includes compiled better-sqlite3)
65+
# Copy production node_modules (includes compiled better-sqlite3) + built app
5266
COPY --from=builder /app/deploy/node_modules ./node_modules
5367
COPY --from=builder /app/apps/cas/backend/dist ./dist
5468
COPY --from=builder /app/apps/cas/backend/package.json ./

0 commit comments

Comments
 (0)