我得到的错误,而转换我的下一个js项目到docker

2wnc66cl  于 2022-11-03  发布在  Docker
关注(0)|答案(6)|浏览(543)

我正在尝试将我的Next js项目转换为Docker。我从Next js github页面获得的Docker文件对我来说工作得很好,我成功地得到了一个构建。


# 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"]

这是我后来写的。

docker run -p 3000:3000 imagename

然后我面对这样的错误,我不能解决它。

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: []
}

我在网上搜索了很多,但是没有找到什么。你觉得我该怎么办?

daolsyd0

daolsyd01#

确保从示例中复制了所有文件。在这种情况下,需要确保已经使用以下命令添加或自定义了next.config.js

module.exports = {
  output: 'standalone'
}

您会注意到在示例中也定义了该文件:https://github.com/vercel/next.js/blob/canary/examples/with-docker/next.config.js

xoefb8l8

xoefb8l82#

答案取决于您运行的Next的版本。首先,您需要取消注解Dockerfile中将server.js(以及.next/standalone文件夹的其余部分)复制到/app的那一行。

COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./

然后,您需要在next.config.js文件中包含以下内容之一,具体取决于您正在运行的Next的版本。
如果您运行的是Next 12.1.x或 * 更早版本 *,则需要:

module.exports = {
  experimental: {
    outputStandalone: true,
  },
}

如果您运行的是12.2.x或更高版本,请使用以下命令:

module.exports = {
  output: 'standalone',
}

这将导致创建.next/standalone,并且该文件夹包含server.js,这样当COPY命令运行时,它和子文件夹将被放置到/app中。

dzhpxtsq

dzhpxtsq3#

FROM node:14-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 ./
RUN npm install --frozen-lockfile
RUN npm i next@12.0.8-canary.7

# Rebuild the source code only when needed

FROM node:14-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:14-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
COPY --from=builder /app/.env ./

# 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"]

解决了我的问题,这是新的Dockerfile,它为我工作。

xa9qqrwz

xa9qqrwz4#

只需将package.json文件中的nextjs版本更改为latest并运行yarn install。现在docker构建工作正常。

3pvhb19x

3pvhb19x5#

我遇到了同样的问题,冲突是我使用的是Next 11而不是12,您可以使用this dockerfile,它可以完美地运行Next 11,或者尝试以下代码。


# Install dependencies only when needed

FROM node:14-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:14-alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN yarn build

# Production image, copy all the files and run next

FROM node:14-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 --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json

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_modules/.bin/next", "start"]
yc0p9oo0

yc0p9oo06#

TL;DR

我的下一个例子是版本11,with-docker的例子是版本12。
我在官方的Next.js with-dockerexample中查看了Dockerfile的历史。在那里我注意到添加了这些lines to the Dockerfile,以及添加了实验性标志outputStandalone
当我的项目是next@11时,这个标志在next@12中可用,所以在我的Dockerfile中撤消这些行解决了这个问题。
查看该文件历史记录可以给予不同的问题。

相关问题