1- FROM node:20-alpine
2- ARG DATABASE_URL
1+ FROM node:20-alpine AS base
2+
3+ # Stage 1: Install dependencies only when needed
4+ FROM base AS deps
5+ RUN apk add --no-cache libc6-compat
36
47WORKDIR /usr/src/app
58
9+ COPY package.json yarn.lock* ./
10+ COPY apps/web/package.json ./apps/web/
11+ COPY packages/ ./packages/
12+
13+ RUN yarn install --frozen-lockfile --ignore-scripts
14+
15+ # Stage 2: Rebuild the source code only when needed
16+ FROM base AS builder
17+ WORKDIR /usr/src/app
18+
19+ COPY --from=deps /usr/src/app/node_modules ./node_modules
20+
621COPY . .
722
8- RUN npm install
9- RUN cd packages/db && DATABASE_URL=$DATABASE_URL npx prisma generate && cd ../..
10- ## put DATABASE_URL in apps/web/.env
11- RUN echo DATABASE_URL=$DATABASE_URL >> apps/web/.env
12- RUN DATABASE_URL=$DATABASE_URL npm run build
13- ## Remove .env file
14- RUN rm apps/web/.env
23+ RUN yarn global add turbo@^2.1.1
24+
25+ RUN DATABASE_URL=$DATABASE_URL npx prisma generate --schema packages/db/prisma/schema.prisma
26+
27+ RUN cd apps/web && yarn add sharp
28+ RUN yarn turbo run build --filter=web...
29+
30+ # Stage 3: Production image
31+ FROM base AS runner
32+ WORKDIR /usr/src/app
33+
34+ RUN addgroup --system --gid 1001 nodejs
35+ RUN adduser --system --uid 1001 nextjs
36+
37+ RUN mkdir .next
38+ RUN chown nextjs:nodejs .next
39+
40+ USER nextjs
41+
42+ COPY --from=builder --chown=nextjs:nodejs /usr/src/app/apps/web/.next/standalone ./
43+ COPY --from=builder --chown=nextjs:nodejs /usr/src/app/apps/web/.next/static ./apps/web/.next/static
44+ COPY --from=builder --chown=nextjs:nodejs /usr/src/app/apps/web/public ./apps/web/public
1545
1646EXPOSE 3000
47+ ENV NODE_ENV production
48+ # For Standalone build
49+ ENV PORT 3000
50+ ENV HOSTNAME "0.0.0.0"
1751
18- CMD ["npm ", "run", "start "]
52+ CMD ["node ", "apps/web/server.js "]
0 commit comments