在Dockerfile中没有从package.json安装依赖项(RxJS)吗?

时间:2020-09-08 19:51:38

标签: node.js angular docker npm

我正在尝试将我的应用容器化以将其部署到GCP。在进行任何触发器设置之前,我一直在进行一些本地测试,由于某种原因,尽管在package.json中列出了rxjs(也许还有其他)都没有安装。

在我的Dockerfile中,它在构建步骤中出错,因为它找不到rxjs模块(RUN ls node_modules显示它甚至在安装后也不存在)。我已经明确尝试添加RUN npm install rxjs,但是那还是行不通的。当我这样做时,我遇到了很多其他错误,因为它们找不到npm正在暂存的模块。这种情况在我的本地计算机和我试图构建/部署的Cloud Run实例上都发生。

在任何人提到它之前,我都尝试过删除package-lock.json并进行全新安装-结果完全相同。当我实际上直接从命令行构建它时,一切都可以正常工作。似乎更像是Docker容器的问题。

Dockerfile

FROM node:12.18-alpine AS dev

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install --only=dev

COPY . .

RUN npm run build:prod

FROM node:12.18-alpine AS prod

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install --only=prod

COPY . .

COPY --from=dev /usr/src/app/dist ./dist

CMD ["npm", "run", "start"]

package.json

{
  "name": "ui",
  "version": "1.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "build:prod": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "dependencies": {
    "@angular/animations": "~9.1.7",
    "@angular/cdk": "^9.2.4",
    "@angular/common": "~9.1.7",
    "@angular/compiler": "~9.1.7",
    "@angular/core": "~9.1.7",
    "@angular/forms": "~9.1.7",
    "@angular/http": "^7.2.16",
    "@angular/material": "^9.2.4",
    "@angular/platform-browser": "~9.1.7",
    "@angular/platform-browser-dynamic": "~9.1.7",
    "@angular/router": "~9.1.7",
    "@ngstack/code-editor": "^1.0.0",
    "angular-markdown-editor": "^2.0.2",
    "hammerjs": "^2.0.8",
    "ngx-markdown": "^10.1.1",
    "rxjs": "~6.5.4",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.901.7",
    "@angular/cli": "~9.1.6",
    "@angular/compiler-cli": "~9.1.7",
    "@types/hammerjs": "^2.0.36",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^5.1.2",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~3.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "protractor": "~5.4.3",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.8.3"
  }
}

docker build --rm .的输出

$ docker build --rm .
Sending build context to Docker daemon  868.4kB
Step 1/15 : FROM node:12.18-alpine AS dev
 ---> 18f4bc975732
Step 2/15 : WORKDIR /usr/src/app
 ---> Using cache
 ---> be5e328970e5
Step 3/15 : COPY package*.json ./
 ---> Using cache
 ---> 634457d1a251
Step 4/15 : RUN npm install --only=dev
 ---> Using cache
 ---> 4e8cd1472043
Step 5/15 : COPY . .
 ---> Using cache
 ---> 423a944a84c2
Step 6/15 : RUN npm run build:prod
 ---> Running in b68a5b25ff84

> ui@1.0.0 build:prod /usr/src/app
> ng build --prod

Unknown error: Error: Cannot find module 'rxjs'
Require stack:
- /usr/src/app/node_modules/inquirer/lib/ui/prompt.js
- /usr/src/app/node_modules/inquirer/lib/inquirer.js
- /usr/src/app/node_modules/@angular/cli/models/analytics.js
- /usr/src/app/node_modules/@angular/cli/models/command-runner.js
- /usr/src/app/node_modules/@angular/cli/lib/cli/index.js
- /usr/src/app/node_modules/@angular/cli/lib/init.js
- /usr/src/app/node_modules/@angular/cli/bin/ng
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! ui@1.0.0 build:prod: `ng build --prod`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the ui@1.0.0 build:prod 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/2020-09-08T19_47_14_999Z-debug.log
The command '/bin/sh -c npm run build:prod' returned a non-zero code: 1

1 个答案:

答案 0 :(得分:0)

第4/15步:运行npm install --only = dev --->使用缓存

只要您在构建时遇到一些问题,我建议您在不使用缓存的情况下进行构建。只需添加--no-cache,以确保确实调用了npm install

docker build --no-cache -t mybuild:v1 .