|
| 1 | +# ============================================================================= |
| 2 | +# STAGE 1: Dependencies - Install workspace dependencies |
| 3 | +# ============================================================================= |
| 4 | +FROM oven/bun:1.2.8 AS deps |
| 5 | + |
| 6 | +WORKDIR /app |
| 7 | + |
| 8 | +# Copy root workspace config |
| 9 | +COPY package.json bun.lock ./ |
| 10 | + |
| 11 | +# Copy all workspace package.json files |
| 12 | +COPY packages/db/package.json ./packages/db/ |
| 13 | +COPY packages/utils/package.json ./packages/utils/ |
| 14 | +COPY packages/integration-platform/package.json ./packages/integration-platform/ |
| 15 | +COPY packages/tsconfig/package.json ./packages/tsconfig/ |
| 16 | + |
| 17 | +# Copy API package.json |
| 18 | +COPY apps/api/package.json ./apps/api/ |
| 19 | + |
| 20 | +# Install all dependencies (including workspace deps) |
| 21 | +RUN bun install |
| 22 | + |
| 23 | +# ============================================================================= |
| 24 | +# STAGE 2: Builder - Build workspace packages and NestJS app |
| 25 | +# ============================================================================= |
| 26 | +FROM deps AS builder |
| 27 | + |
| 28 | +WORKDIR /app |
| 29 | + |
| 30 | +# Copy workspace packages source |
| 31 | +COPY packages/db ./packages/db |
| 32 | +COPY packages/utils ./packages/utils |
| 33 | +COPY packages/integration-platform ./packages/integration-platform |
| 34 | +COPY packages/tsconfig ./packages/tsconfig |
| 35 | + |
| 36 | +# Copy API source |
| 37 | +COPY apps/api ./apps/api |
| 38 | + |
| 39 | +# Bring in node_modules from deps stage |
| 40 | +COPY --from=deps /app/node_modules ./node_modules |
| 41 | + |
| 42 | +# Build workspace packages |
| 43 | +RUN cd packages/db && bun run build && cd ../.. |
| 44 | +RUN cd packages/integration-platform && bun run build && cd ../.. |
| 45 | + |
| 46 | +# Generate Prisma client for API (copy schema and generate) |
| 47 | +RUN cd packages/db && node scripts/combine-schemas.js && cd ../.. |
| 48 | +RUN cp packages/db/dist/schema.prisma apps/api/prisma/schema.prisma |
| 49 | +RUN cd apps/api && bunx prisma generate |
| 50 | + |
| 51 | +# Build NestJS application |
| 52 | +RUN cd apps/api && bun run build |
| 53 | + |
| 54 | +# ============================================================================= |
| 55 | +# STAGE 3: Production Runtime |
| 56 | +# ============================================================================= |
| 57 | +FROM node:20-alpine AS production |
| 58 | + |
| 59 | +WORKDIR /app |
| 60 | + |
| 61 | +# Install runtime dependencies |
| 62 | +RUN apk add --no-cache wget libc6-compat openssl |
| 63 | + |
| 64 | +# Copy built NestJS app |
| 65 | +COPY --from=builder /app/apps/api/dist ./dist |
| 66 | + |
| 67 | +# Copy prisma files |
| 68 | +COPY --from=builder /app/apps/api/prisma ./prisma |
| 69 | + |
| 70 | +# Copy package.json (for any runtime needs) |
| 71 | +COPY --from=builder /app/apps/api/package.json ./package.json |
| 72 | + |
| 73 | +# Copy workspace packages that are referenced by node_modules symlinks |
| 74 | +COPY --from=builder /app/packages/db ./packages/db |
| 75 | +COPY --from=builder /app/packages/utils ./packages/utils |
| 76 | +COPY --from=builder /app/packages/integration-platform ./packages/integration-platform |
| 77 | +COPY --from=builder /app/packages/tsconfig ./packages/tsconfig |
| 78 | + |
| 79 | +# Copy production node_modules (includes symlinks to workspace packages above) |
| 80 | +COPY --from=builder /app/node_modules ./node_modules |
| 81 | + |
| 82 | +# Set production environment |
| 83 | +ENV NODE_ENV=production |
| 84 | +ENV PORT=3333 |
| 85 | + |
| 86 | +# Install Prisma CLI and regenerate client in production stage |
| 87 | +RUN npm install -g [email protected] && \ |
| 88 | + prisma generate --schema=./prisma/schema.prisma |
| 89 | + |
| 90 | +# Create non-root user |
| 91 | +RUN addgroup --system nestjs && adduser --system --ingroup nestjs nestjs \ |
| 92 | + && chown -R nestjs:nestjs /app |
| 93 | + |
| 94 | +USER nestjs |
| 95 | + |
| 96 | +EXPOSE 3333 |
| 97 | + |
| 98 | +# Health check |
| 99 | +HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ |
| 100 | + CMD wget --no-verbose --tries=1 --spider http://localhost:3333/v1/health || exit 1 |
| 101 | + |
| 102 | +# Start the application |
| 103 | +CMD ["node", "dist/src/main.js"] |
| 104 | + |
0 commit comments