Next.js Discord

Discord Forum

I'm out of ideas. Any Docker guru around who can help me debug a build issue with Next.js 15.3.2?

Unanswered
Black carp posted this in #help-forum
Open in Discord
Black carpOP
The app builds fine locally on my Macbook. I'm trying to run it inside a docker container on a Ubuntu 22.04 VM. It builds fine on the VM itself, but when I try to build the container, it gets stuck on "Creating an optimized production build ..." until the VM gets OOM and the process is killed by the OOM killer. I tried different base images (node18-alpine, node20-alpine, node22-alpine), pruned everything. Reinstalled the OS. Upgraded the VM with more RAM. Nothing helped. I'm out of ideas

3 Replies

Black carpOP
Dockerfile looks like this:

FROM node:20-alpine AS base

# Stage 1: Install dependencies
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* ./
RUN \
  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  elif [ -f package-lock.json ]; then npm ci; \
  elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
  else echo "Lockfile not found." && exit 1; \
  fi

# Stage 2: Build the application
FROM base AS builder
WORKDIR /
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npx prisma generate
RUN \
  if [ -f yarn.lock ]; then yarn run build; \
  elif [ -f package-lock.json ]; then npm run build; \
  elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
  else echo "Lockfile not found." && exit 1; \
  fi

# Stage 3: Production server
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
COPY --from=builder /app/node_modules/@prisma ./node_modules/@prisma

COPY --from=builder /app/.env ./.env
# You need this to be able to run the migrations
COPY --from=builder /app/prisma ./prisma

# This script makes sure that the DB is accepting connections so migrations can run
COPY wait-for-it.sh /usr/local/bin/wait-for-it.sh
RUN chmod +x /usr/local/bin/wait-for-it.sh

USER nextjs
EXPOSE 3000
CMD ["sh", "-c", "wait-for-it.sh db:1433 -- npx prisma migrate deploy && npm run start-standalone"]
It stucks here
Dutch
Why you need this much complexity instead hosting it on cloud