在 CentOS 的生产模式下使用 Docker .next 文件夹缺少一些文件

.next folder is missing some files using Docker on CentOS in production mode

我正在 运行 一个 next.js 项目,当我尝试在我的本地系统上以 production 模式构建时一切正常。

即使我在我的系统上使用 docker,其 OS 正在 ubuntu 18.04.2 构建并且 运行ning 项目已成功完成。但是当 OS 为 CentOS 7.6.1810 的服务器上的代码为 运行 时,.next 文件夹中的一些文件会丢失,这会导致 运行 项目出错: (

这是我在 package.json 中的设置:

{
  "name": "with-redux-wrapper",
  "version": "2.0.0",
  "scripts": {
    "dev": "node server.js 0.0.0.0",
    "build": "next build",
    "start": "node server",
    "export": "next export"
  },
  "dependencies": {
    "@sentry/browser": "^5.4.3",
    "@zeit/next-css": "^1.0.1",
    "@zeit/next-source-maps": "0.0.4-canary.1",
    "animated-scroll-to": "^1.2.2",
    "isomorphic-fetch": "^2.2.1",
    "isomorphic-unfetch": "^3.0.0",
    "js-cookie": "^2.2.1",
    "next": "8.1.0",
    "next-cookies": "^1.1.3",
    "next-redux-wrapper": "2.0.0",
    "next-routes": "^1.0.17",
    "next-seo": "^1.12.0",
    "node-fetch": "^2.6.0",
    "nprogress": "^0.2.0",
    "react": "16.8.6",
    "react-datepicker2": "^2.0.5",
    "react-dom": "16.8.6",
    "react-hotjar": "^2.0.0",
    "react-lottie": "^1.2.3",
    "react-markdown": "^4.0.8",
    "react-persian-datepicker": "^3.0.2",
    "react-recaptcha": "^2.3.8",
    "react-redux": "5.0.7",
    "react-scroll-up": "^1.3.3",
    "react-slick": "^0.21.0",
    "react-slick-16": "^0.16.1",
    "redux": "4.0.0",
    "redux-api-middleware": "^3.0.1",
    "redux-devtools-extension": "2.13.2",
    "redux-thunk": "2.2.0",
    "slick-carousel": "^1.8.1"
  },
  "license": "ISC",
  "devDependencies": {
    "express": "^4.17.1",
    "persian-date": "^1.1.0",
    "serve": "^11.1.0"
  }
}

还有我的 Dockerfile 这迫使我对注释行进行评论,因为 production 模式会导致问题:

FROM node:10.16-alpine AS build-stage
WORKDIR /app
COPY package*.json /app/
RUN npm config set unsafe-perm true
RUN npm install --verbose
# --> ENV NODE_ENV=production
COPY . /app/
RUN npm run build
# --> RUN npm run export

RUN ls .next -a
RUN npm install -g pm2

# --> CMD ["pm2-runtime", "ecosystem.config.js", "--env", "production"]
CMD ["pm2-runtime", "server.js"]

ecosystem.config.js:

module.exports = {
  apps : [{
    name: "app",
    script: "./server.js",
    env: {
      NODE_ENV: "development",
    },
    env_production: {
      NODE_ENV: "production",
    }
  }]
}

next.config.js:

const withCSS = require('@zeit/next-css')
const withSourceMaps = require('@zeit/next-source-maps')()
const webpackConfig = config => {
  console.log = function() {}
  return config
}

module.exports = withCSS(withSourceMaps({
  webpack: webpackConfig,
}))

只有当 运行在没有注释提到的行的情况下在服务器上运行时我才会得到错误: ENV NODE_ENV=production; RUN npm run build:

Error: Could not find a valid build in the '/app/.next' directory! Try building your app with 'next build' before starting the server.
at Server.readBuildId (/app/node_modules/next-server/dist/server/next-server.js:271:23)
at new Server (/app/node_modules/next-server/dist/server/next-server.js:38:29)
at module.exports (/app/node_modules/next-server/index.js:4:10)
at module.exports (/app/node_modules/next/dist/server/next.js:9:12)
at Object.<anonymous> (/app/server.js:8:13)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)

RUN npm run export:

{ Error: ENOENT: no such file or directory, open '/app/.next/BUILD_ID'
    at Object.openSync (fs.js:443:3)
    at Object.readFileSync (fs.js:343:35)
    at Object.default_1 [as default] (/app/node_modules/next/dist/export/index.js:36:26)
    at nextExport (/app/node_modules/next/dist/cli/next-export.js:61:21)
    at commands.(anonymous function).then (/app/node_modules/next/dist/bin/next:86:36)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '/app/.next/BUILD_ID' }
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! with-redux-wrapper@2.0.0 export: `next export`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the with-redux-wrapper@2.0.0 export script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-09-03T05_11_45_045Z-debug.log

问题是关于 next.jscentos 上的一些问题,通过将服务器操作系统更改为 ubuntu 来解决。