1- # Build stage
2- FROM node:20-alpine AS builder
1+ # Dependencies stage
2+ FROM node:20-slim AS deps
33
4- # Set working directory
54WORKDIR /app
65
7- # Add pnpm to path
8- ENV PNPM_HOME=/usr/local/bin
6+ # Install build dependencies
7+ RUN apt-get update && apt-get install -y --no-install-recommends \
8+ python3 \
9+ make \
10+ g++ \
11+ && rm -rf /var/lib/apt/lists/*
912
10- # Install pnpm - use exact version for reproducibility
11- RUN npm install -g
[email protected] \
12- && pnpm config set store-dir /.pnpm-store
13+ # Install corepack and pnpm
14+ RUN corepack enable && corepack prepare
[email protected] --activate
1315
14- # Copy package.json and lockfile
16+ # Copy package files
1517COPY package.json pnpm-lock.yaml* .npmrc* ./
1618
17- # Install dependencies with cache optimization
18- RUN --mount=type=cache,id=pnpm,target=/.pnpm-store \
19- pnpm install --frozen-lockfile --prod=false
19+ # Install dependencies
20+ RUN pnpm install --frozen-lockfile
2021
21- # Copy application code
22+ # Builder stage
23+ FROM node:20-slim AS builder
24+
25+ WORKDIR /app
26+
27+ RUN corepack enable && corepack prepare
[email protected] --activate
28+
29+ # Copy dependencies and source
30+ COPY --from=deps /app/node_modules ./node_modules
2231COPY . .
2332
2433# Build application
34+ ENV NEXT_TELEMETRY_DISABLED=1
2535RUN pnpm build
2636
2737# Production stage
28- FROM node:20-alpine AS runner
38+ FROM node:20-slim AS runner
2939
30- # Set working directory
3140WORKDIR /app
3241
33- # Set environment variables
3442ENV NODE_ENV=production \
35- PNPM_HOME=/usr/local/bin
43+ NEXT_TELEMETRY_DISABLED=1
3644
37- # Add a non-root user to run the application
38- RUN addgroup --system --gid 1001 nodejs \
39- && adduser --system --uid 1001 nextjs \
40- &&
npm install -g [email protected] 45+ # Install wget for healthcheck
46+ RUN apt-get update && apt-get install -y --no-install-recommends \
47+ wget \
48+ && rm -rf /var/lib/apt/lists/*
4149
42- # Copy only necessary files from builder
43- COPY --from=builder --chown=nextjs:nodejs /app/next.config.js ./
50+ # Create non-root user
51+ RUN groupadd -r nodejs --gid=1001 && \
52+ useradd -r -g nodejs --uid=1001 nextjs
53+
54+ # Copy built application
4455COPY --from=builder --chown=nextjs:nodejs /app/public ./public
4556COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
46- COPY --from=builder --chown=nextjs:nodejs /app/package.json ./package.json
47- COPY --from=builder --chown=nextjs:nodejs /app/pnpm-lock.yaml ./pnpm-lock.yaml
48-
49- # Install production dependencies and necessary dev dependencies
50- RUN --mount=type=cache,id=pnpm,target=/.pnpm-store \
51- pnpm install --frozen-lockfile --prod --no-optional \
52- && pnpm add -D @next/bundle-analyzer
53-
54- # Set proper permissions
55- RUN chmod -R 755 /app
57+ COPY --from=builder --chown=nextjs:nodejs /app/next.config.js ./
58+ COPY --from=builder --chown=nextjs:nodejs /app/package.json ./
59+ COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules
5660
57- # Switch to non-root user
5861USER nextjs
5962
60- # Expose the port the app runs on
6163EXPOSE 3000
6264
63- # Define healthcheck
64- HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
65+ ENV PORT=3000
66+
67+ HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
6568 CMD wget --no-verbose --tries=1 --spider http://localhost:3000/ || exit 1
6669
67- # Run the application
68- CMD ["pnpm" , "start" ]
70+ CMD ["node_modules/.bin/next" , "start" ]
0 commit comments