在“/opt/app/.next”目录中找不到生产版本

reh*_*007 5 docker reactjs next.js

您使用的 Next.js 版本是什么?

10.0.5

您使用的 Node.js 版本是什么?

14 高山

你使用的是什么浏览器?

铬合金

您使用什么操作系统?

视窗

您如何部署您的应用程序?

Dockerfile 中的下一个构建

描述错误

我的下一个构建和下一个启动工作正常。突然之间,没有任何改变,我在运行时遇到这个错误

在“/opt/app/.next”目录中找不到生产版本

这是我的 docker 文件

FROM node:14-alpine

WORKDIR /opt/app
RUN chown -R node:node /opt/app

USER node

ARG NPM_TOKEN
RUN echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc

COPY --chown=node:node package*.json ./

RUN npm ci

COPY --chown=node:node . /opt/app

RUN npm install --dev && npm run lint && npm run build:app

RUN ls -la

CMD [ "npm", "start" ]

EXPOSE 3000

Run Code Online (Sandbox Code Playgroud)

这是我的 package.json 脚本

"start": "cross-env NODE_ENV=production node ./bin/start.js",
"build:app": "cross-env NODE_ENV=production rm -rf .next && APP_TENANT_CODE=app && next build",
Run Code Online (Sandbox Code Playgroud)

这是我的 start.js 文件

#!/usr/bin/env node
/**
 * So next.config.js doesn't allow async functions, but we need to read
 * runtime config variables from Key Vault.
 *
 * So instead we will read them and then start next.js server in  custom script.
 *
 */
const path = require('path')
const { default: startServer } = require('next/dist/server/lib/start-server')

process.on('SIGTERM', () => process.exit(0))
process.on('SIGINT', () => process.exit(0))

async function main() {
  const port = process.env.PORT || 3000
  const dir = path.resolve(__dirname, '..')
  const hostname = '0.0.0.0'

  // start the app
  const app = await startServer({ dir }, port, hostname)
  console.log(`started server on http://${hostname}:${port}`)
  await app.prepare()
}

main().catch(error => console.error(error))
Run Code Online (Sandbox Code Playgroud)

运行 docker image 后出现此错误

Error: Could not find a production build in the '/opt/app/.next' directory. Try building your app with 'next build' before starting the production server. https://err.sh/vercel/next.js/production-start-no-build-id
    at Server.readBuildId (/opt/app/node_modules/next/dist/next-server/server/next-server.js:146:355)
    at new Server (/opt/app/node_modules/next/dist/next-server/server/next-server.js:3:120)
    at createServer (/opt/app/node_modules/next/dist/server/next.js:2:638)
    at start (/opt/app/node_modules/next/dist/server/lib/start-server.js:1:323)
    at main (/opt/app/bin/start.js:52:21)
    at Object.<anonymous> (/opt/app/bin/start.js:57:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
Run Code Online (Sandbox Code Playgroud)

预期行为

应用程序应在http://127.0.0.1:3000上启动

重现

如上所述使用 Dockerfile 和 start.js 文件

小智 4

当您从该映像创建容器时,您的 .next 目录为空。

相应地更新 docker 文件。

FROM node:16
WORKDIR /opt/app

RUN chown -R node:node /opt/app
USER node

ARG NPM_TOKEN
RUN echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc

COPY --chown=node:node package*.json ./
RUN npm install

COPY --chown=node:node . /opt/app
RUN npm run lint

RUN npm run build:fair
CMD [ "npm", "run", "start" ]

EXPOSE 3000
Run Code Online (Sandbox Code Playgroud)