/ Dockerfile
Dockerfile
 1  # Base image
 2  FROM node:20-alpine AS base
 3  
 4  # Install dependencies only when needed
 5  FROM base AS deps
 6  WORKDIR /app
 7  
 8  # Install dependencies and set up Yarn
 9  RUN apk add --no-cache libc6-compat
10  RUN corepack enable && corepack prepare yarn@stable --activate
11  
12  # Copy package files
13  COPY package.json yarn.lock ./
14  
15  # Install dependencies with nodeLinker: node-modules configuration
16  RUN echo 'nodeLinker: node-modules' > .yarnrc.yml && \
17      yarn install --immutable
18  
19  # Builder stage
20  FROM base AS builder
21  WORKDIR /app
22  
23  COPY . .
24  # Copy node_modules and verify it exists
25  COPY --from=deps /app/node_modules ./node_modules
26  
27  RUN yarn build
28  
29  # Production stage
30  FROM base AS runner
31  WORKDIR /app
32  
33  ENV NODE_ENV=production
34  ENV PORT=3000
35  ENV HOSTNAME=0.0.0.0
36  
37  RUN addgroup --system --gid 1001 nodejs
38  RUN adduser --system --uid 1001 nextjs
39  
40  COPY --from=builder /app/public ./public
41  COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
42  COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
43  
44  USER nextjs
45  
46  EXPOSE 3000
47  
48  CMD ["node", "server.js"]