|
1 | | -FROM node:24-alpine |
2 | 1 |
|
3 | | -# Install pnpm globally |
4 | | -RUN npm install -g pnpm |
| 2 | +# Stage 1: Build the application |
| 3 | +FROM node:24-alpine AS builder |
5 | 4 |
|
6 | | -# Set working directory |
7 | | -WORKDIR /usr/src/app |
| 5 | +WORKDIR /app |
8 | 6 |
|
| 7 | +# Install pnpm globally |
| 8 | +RUN npm install -g pnpm |
9 | 9 |
|
10 | | -# Copy only manifest, lockfile, and prisma schema for better caching |
| 10 | +# Copy package manifests and lockfile |
11 | 11 | COPY package.json pnpm-lock.yaml ./ |
12 | 12 | COPY prisma ./prisma |
13 | 13 |
|
14 | | - |
15 | | -# Install all dependencies (including devDependencies for build) |
| 14 | +# Install dependencies |
16 | 15 | RUN pnpm install --unsafe-perm |
17 | 16 |
|
18 | | -# Generate Prisma Client |
19 | | -RUN npx prisma generate |
20 | | - |
21 | | -# Copy the rest of the source code |
| 17 | +# Copy the rest of your application code |
22 | 18 | COPY . . |
23 | 19 |
|
24 | | -# Build the app (requires @nestjs/cli as devDependency) |
| 20 | +# Run Prisma Generate |
| 21 | +RUN npx prisma generate |
| 22 | + |
| 23 | +# Build the application |
25 | 24 | RUN pnpm build |
26 | 25 |
|
27 | | -# Prune devDependencies for smaller final image |
28 | | -RUN pnpm prune --prod |
| 26 | +# Stage 2: Production Image |
| 27 | +FROM node:24-alpine |
| 28 | + |
| 29 | +WORKDIR /app |
| 30 | + |
| 31 | +# Install pnpm in the final production image as well |
| 32 | +RUN npm install -g pnpm |
| 33 | + |
| 34 | +# Set environment variables for better console output |
| 35 | +ENV FORCE_COLOR=1 |
| 36 | +ENV NODE_ENV=production |
| 37 | + |
| 38 | +# Copy only the necessary production artifacts from the 'builder' stage |
| 39 | +COPY --from=builder /app/node_modules ./node_modules |
| 40 | +COPY --from=builder /app/package.json ./package.json |
| 41 | +COPY --from=builder /app/pnpm-lock.yaml ./pnpm-lock.yaml |
| 42 | +COPY --from=builder /app/dist ./dist |
| 43 | +COPY --from=builder /app/prisma ./prisma |
| 44 | +COPY docker-entrypoint.sh . |
29 | 45 |
|
30 | | -# Ensure entrypoint is executable |
| 46 | +# Ensure the entrypoint is executable |
31 | 47 | RUN chmod +x docker-entrypoint.sh |
32 | 48 |
|
33 | 49 | # Expose the NestJS port |
|
0 commit comments