1+ FROM node:22-alpine AS base
2+ ENV PNPM_HOME="/pnpm"
3+ ENV PATH="$PNPM_HOME:$PATH"
4+
5+ FROM base AS builder
6+ RUN apk update && apk add --no-cache libc6-compat
7+ WORKDIR /app
8+ RUN corepack enable pnpm
9+ RUN pnpm install -g turbo
10+ COPY . .
11+
12+ # Generate a partial monorepo with a pruned lockfile for the relay workspace
13+ RUN turbo prune @evolu/relay --docker
14+
15+ # ------------------------------------------------------------
16+ # Installer stage - build the pruned workspace
17+ FROM base AS installer
18+ RUN apk update && apk add --no-cache libc6-compat
19+ WORKDIR /app
20+
21+ # Install pnpm and turbo
22+ RUN corepack enable pnpm
23+ RUN pnpm install -g turbo
24+
25+ # Install dependencies from pruned lockfile
26+ COPY --from=builder /app/out/json/ .
27+ RUN pnpm install --frozen-lockfile
28+
29+ # Copy source and build
30+ COPY --from=builder /app/out/full/ .
31+ # Ensure README.md is available at the root for the build process
32+ COPY --from=builder /app/README.md ./README.md
33+ RUN turbo run build
34+
35+ # ------------------------------------------------------------
36+ # Runner stage - minimal runtime image
37+ FROM base AS runner
38+ WORKDIR /app
39+
40+ # Create non-root user
41+ RUN addgroup --system --gid 1001 nodejs && \
42+ adduser --system --uid 1001 evolu --ingroup nodejs
43+ RUN chown evolu:nodejs /app
44+ USER evolu
45+
46+ # Copy built application
47+ COPY --from=installer --chown=evolu:nodejs /app/apps/relay/dist ./dist
48+
49+ # Copy the complete node_modules with all dependencies
50+ COPY --from=installer --chown=evolu:nodejs /app/node_modules ./node_modules
51+
52+ # Copy the built workspace packages that the relay needs at runtime
53+ COPY --from=installer --chown=evolu:nodejs /app/packages/common/dist ./node_modules/@evolu/common/dist
54+ COPY --from=installer --chown=evolu:nodejs /app/packages/common/package.json ./node_modules/@evolu/common/package.json
55+ COPY --from=installer --chown=evolu:nodejs /app/packages/nodejs/dist ./node_modules/@evolu/nodejs/dist
56+ COPY --from=installer --chown=evolu:nodejs /app/packages/nodejs/package.json ./node_modules/@evolu/nodejs/package.json
57+
58+ # Expose port
59+ EXPOSE 4000
60+
61+ # Health check
62+ HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
63+ CMD wget --no-verbose --tries=1 --spider http://localhost:4000 || exit 1
64+
65+ # Start the application
66+ CMD ["node", "dist/src/cli.js", "start"]
0 commit comments