我正在尝试将我的 Next js 项目转换为 Docker。我从下一个 js github 页面获得的 Dockerfile 对我来说工作得很好,并且我成功构建了。
# Install dependencies only when needed
FROM node:16-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
# Rebuild the source code only when needed
FROM node:16-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN yarn build
# Production image, copy all the files and run next
FROM node:16-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
# 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
USER nextjs
EXPOSE 3000
ENV PORT 3000
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
# ENV NEXT_TELEMETRY_DISABLED 1
CMD ["node", "server.js"]
Run Code Online (Sandbox Code Playgroud)
这是我后来写的。
docker run -p 3000:3000 imagename
Run Code Online (Sandbox Code Playgroud)
然后我遇到了这样的错误,我无法解决它。
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module '/app/server.js'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
Run Code Online (Sandbox Code Playgroud)
我在互联网上搜索了很多,但找不到太多。你觉得我应该怎么做?
小智 70
确保您复制示例中的所有文件。在这种情况下,您需要确保您已添加或自定义了next.config.js
以下内容:
module.exports = {
output: 'standalone'
}
Run Code Online (Sandbox Code Playgroud)
您会注意到示例中也定义了该文件:https://github.com/vercel/next.js/blob/canary/examples/with-docker/next.config.js
Mic*_*ryl 23
答案取决于您运行的 Next 版本。首先,您需要取消注释复制到 /appDockerfile
的行server.js
(以及文件夹的其余部分.next/standalone
)。
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
Run Code Online (Sandbox Code Playgroud)
然后,您需要在文件中包含以下内容之一next.config.js
,具体取决于您运行的 Next 版本。
如果您正在运行 Next12.1.x
或更早版本,那么您需要:
module.exports = {
experimental: {
outputStandalone: true,
},
}
Run Code Online (Sandbox Code Playgroud)
如果您正在运行12.2.x
或稍后运行,请使用以下命令:
module.exports = {
output: 'standalone',
}
Run Code Online (Sandbox Code Playgroud)
这将导致创建 .next/standalone,并且该文件夹包含在内,server.js
以便当COPY
命令运行时,它和子文件夹将被放入 .next/standalone 中/app
。
归档时间: |
|
查看次数: |
41194 次 |
最近记录: |