Node.js 应用在本地运行但在 docker 上失败(sh: 1: rimraf: 未找到)

Node.js app runs locally but fails on docker (sh: 1: rimraf: not found)

我能够构建 docker 图像,但无法将容器获取到 运行。这是 package.json:

{
  "name": "linked-versions-viewer",
  "version": "1.0.0",
  "description": "...",
  "main": "./dist/bundle.js",
  "type": "module",

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "clean": "rimraf dist",
    "build_only": "rollup -c",
    "build": "cross-env NODE_ENV=production npm run clean && npm run build_only",
    "serve": "parcel src/index.html --port 12345",
    "start": "npm run clean && npm run serve"
  },
  "repository": {
    "type": "git",
    "url": "..."
  },
  "author": "...",
  "license": "...",
  "dependencies": {
    "antd": "^4.12.3",
    "base-widget": "...",
    "react": "^17.0.2",
    "react-dom": "^17.0.1"
  },
  "devDependencies": {
    "@types/node": "^14.14.28",
    "@types/react": "^17.0.2",
    "@types/react-dom": "^17.0.1",
    "cross-env": "^7.0.3",
    "parcel-bundler": "^1.12.4",
    "rev-hash": "^3.0.0",
    "rimraf": "^3.0.2",
    "rollup": "^2.39.0",
    "rollup-plugin-commonjs": "^10.1.0",
    "rollup-plugin-node-resolve": "^5.2.0",
    "rollup-plugin-replace": "^2.2.0",
    "rollup-plugin-terser": "^7.0.2",
    "rollup-plugin-typescript2": "^0.29.0",
    "rollup-plugin-uglify": "^6.0.4",
    "typescript": "^4.1.5"
  }
}

这是 docker 文件:

FROM node:14.17.0 as base

ENV NODE_ENV=production

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8080

FROM base as production

ENV NODE_PATH=./build

CMD ["npm", "start"]

我通过 运行ning docker build -t testapp . 创建了图像并尝试 运行ning 容器 docker run -p 8080:8080 -d testapp 但它总是立即退出并显示代码 1。这是错误记录图像:

> linked-versions-viewer@1.0.0 start /app
> npm run clean && npm run serve


> linked-versions-viewer@1.0.0 clean /app
> rimraf dist

sh: 1: rimraf: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! linked-versions-viewer@1.0.0 clean: `rimraf dist`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the linked-versions-viewer@1.0.0 clean 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/2021-06-09T20_40_17_204Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! linked-versions-viewer@1.0.0 start: `npm run clean && npm run serve`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the linked-versions-viewer@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我尝试完全删除 rimraf 依赖项,但随后出现了与 parcel 依赖项类似的错误,所以可能没有正确安装包?如果有帮助,该应用程序也是用打字稿编写的。

不确定接下来要尝试什么。感谢您的任何建议。

表格npm install docs,

With the --production flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in devDependencies

由于您的 base 映像中有 ENV NODE_ENV=production,因此您的容器中既没有安装 rimraf 也没有安装 parcel-bundler

您的 npm start 命令是 运行 npm run clean && npm run servenpm run clean 使用 rimraf 模块,npm run serve 使用 parcel-bundler 模块。这就是您看到这两个错误的原因。

您可以尝试以下解决方案之一,

  1. 从您的 Docker 文件中删除 ENV NODE_ENV=production(这是最快的解决方案,但不应在生产中使用)

  2. 您可以使用以下方法在容器内全局安装 rimrafparcel-bundler

    RUN npm install --global rimraf && npm install --global parcel-bundler
    

    但是,我仍然认为这不是一个好的生产就绪设置。

  3. 您可以在容器内使用 npm run build 正确构建您的应用程序并提供它。但是,我对 React 不够熟悉,无法帮助您在 Docker.

    上进行设置