【发布时间】:2023-02-14 16:19:22
【问题描述】:
我在 next.js 上使用 yarn berry。 我需要减少这个构建的 docker 镜像大小, 所以我转换为基于 next.js 的独立构建。
这是我的 Dockerfile。
FROM node:16-alpine as builder
WORKDIR /app
COPY package.json ./
COPY yarn.lock ./
COPY .pnp.cjs ./
COPY .pnp.loader.mjs ./
COPY .yarnrc.yml ./
COPY .yarn .yarn
RUN yarn install --immutable
COPY . .
ARG GITHUB_SHA=0
RUN yarn build
FROM node:16-alpine AS runner
WORKDIR /app
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
ENV NODE_ENV='production'
CMD node server.js
它可以构建命令docker build -t standalone .
然后我可以获得输出图像。
当我使用命令docker run -d -p3000:3000 standalone 运行这个 docker 镜像时。
我收到这个错误。
node:internal/modules/cjs/loader:988
throw err;
^
Error: Cannot find module 'next/dist/server/next-server'
Require stack:
- /app/server.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:985:15)
at Function.Module._load (node:internal/modules/cjs/loader:833:27)
at Module.require (node:internal/modules/cjs/loader:1057:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (/app/server.js:4:20)
at Module._compile (node:internal/modules/cjs/loader:1155:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
at Module.load (node:internal/modules/cjs/loader:1033:32)
at Function.Module._load (node:internal/modules/cjs/loader:868:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/app/server.js' ]
}
【问题讨论】: