vercel / next.js

The React Framework
https://nextjs.org
MIT License
124.86k stars 26.65k forks source link

Docker standalone build is not working when adding layout to /app path #68690

Open AdisonCavani opened 1 month ago

AdisonCavani commented 1 month ago

Link to the code that reproduces this issue

https://github.com/AdisonCavani/nextjs-docker-reproduction

To Reproduce

  1. docker build -t nextjs-docker .
  2. docker run -p 3000:3000 nextjs-docker
  3. Open website

Current vs. Expected behavior

Current: Website is not loading. React throws errors into console.
Expected: Website should work

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
  Available memory (MB): 16296
  Available CPU cores: 12
Binaries:
  Node: 22.2.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.2.5 // Latest available version is detected (14.2.5).
  eslint-config-next: N/A
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.5.4
Next.js Config:
  output: standalone

Which area(s) are affected? (Select all that apply)

Output (export/standalone)

Which stage(s) are affected? (Select all that apply)

next start (local), Other (Deployed)

Additional context

When you change the /app folder name to something else, the website is working fine.

samcx commented 1 month ago

@AdisonCavani What errors are you seeing in the console?

AdisonCavani commented 2 weeks ago

@samcx the website is blank and console shows errors:

image

bdiazc90 commented 2 weeks ago

I'm using Nextjs (standalone output) with Dockerfile and docker-compose.yml

In my local, it builds and runs correctly, but when i deploy in AWS EC2, it fails in run stage.

Here are the files:

FROM node:18-alpine AS base

# 1. Install dependencies only when needed
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install

# 2. Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED=1
RUN npx prisma generate
RUN npm run build

# 3. Production image, copy all the files and run next
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

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next

# 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

ENV NEXT_TELEMETRY_DISABLED=1

ENV HOSTNAME="0.0.0.0"

# Note: Don't expose ports here, Compose will handle that for us

USER nextjs

CMD ["node", "server.js"]

services:

  app:
    build:
      context: ../app_enterbase
    container_name: app
    ports:
      - "3000:3000"
    volumes:
      # - ../app_enterbase:/app
      - .:/app
      - /app/node_modules
      - /app/.next

Error in AWS EC2 with docker when its in run stage:

✔ Container app Created 0.6s Attaching to app app | node:internal/modules/cjs/loader:1143 app | throw err; app | ^ app | app | Error: Cannot find module '/app/server.js' app | at Module._resolveFilename (node:internal/modules/cjs/loader:1140:15) app | at Module._load (node:internal/modules/cjs/loader:981:27) app | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12) app | at node:internal/main/run_main_module:28:49 { app | code: 'MODULE_NOT_FOUND', app | requireStack: [] app | } app | app | Node.js v18.20.4

Please help!

weisisheng commented 1 week ago

Wow, I thought I was the edge case. Saw this as well today. I've got the front page rendering now, not sure what I did but front page will not reflect any changes to it, although I can add and build secondary pages which render correctly.