简体   繁体   中英

docker build fail on nestjs microservice build

I have this docker file:

FROM node:14.15.0 as build

WORKDIR /node-app

COPY package*.json /node-app/
RUN npm set progress=false && npm config set depth 0 && npm cache clean --force
RUN npm install
COPY . .
RUN npm run build

FROM node:14.15 

WORKDIR /node-app
COPY package.json /node-app/

RUN npm install --only=production
COPY --from=build /node-app/dist ./dist  
RUN npm run start:prod 

using this package.json

{
  "name": "genysis-dbservice",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "private": true,
  "dependencies": {
    "@nestjs/common": "^7.6.5",
    "@nestjs/config": "^0.6.1",
    "@nestjs/core": "^7.6.5",
    "@nestjs/microservices": "^7.6.5",
    "@nestjs/platform-express": "^7.6.5",
    "@nestjs/websockets": "^7.6.5",
    "core-js": "^3.6.5",
    "neo4j-driver": "^4.1.1",
    "reflect-metadata": "^0.1.12",
    "rimraf": "^2.6.2",
    "rxjs": "^6.3.3"
  },
  "devDependencies": {
    "@nestjs/testing": "^7.4.2",
    "@types/express": "^4.16.0",
    "@types/jest": "^26.0.9",
    "@types/node": "^10.12.18",
    "@types/supertest": "^2.0.7",
    "concurrently": "^4.1.0",
    "jest": "^26.3.0",
    "nodemon": "^1.18.9",
    "prettier": "^1.15.3",
    "supertest": "^3.4.1",
    "ts-jest": "^26.2.0",
    "ts-node": "8.1.0",
    "tsconfig-paths": "3.8.0",
    "tslint": "5.16.0",
    "typescript": "~4.0.5",
    "wait-on": "^3.2.0"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

and this tsconfig:

  "compilerOptions": {
      "module": "commonjs",
      "declaration": true,
      "removeComments": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "allowSyntheticDefaultImports": true,
      "target": "es2019",
      "sourceMap": true,
      "outDir": "./dist",
      "baseUrl": "./",
      "incremental": true
  },
  "exclude": ["node_modules"]
}

my node is version 14.15.0, my npm is 6.14.8 and global nest is 7.5.4. I have changed the typescript version from ~4.5.0 to 4.4.2 to 4.0.5 and still I am getting a failure on the RUN npm run build.

 #16 2.145 sh: 1: nest: not found

needless to say the npm run build and run start:prod works fine outside docker.....

needless to say the npm run build and run start:prod works fine outside docker

there are few things to note here:

  1. nestjs is not defined in your package.json , and thus is not installed. i assume that you installed nestjs globally on your host (outside docker)
  2. even if nestjs would be defined in your package.json , npm will not install it as a global package

you could resolve it in a few ways:

  1. if you add nestjs to your package.json , it will be available to your project. if you wish to utilize package.json and make the packages available globally, then add /node-app/node_modules to your path (on your host or on the docker image)
     RUN npm install ENV PATH="/node-app/node_modules:${PATH}"
  2. install nestjs globally on your docker images
     RUN npm install -g nestjs
  3. leverage a tool which bundles your code and its dependencies, such as esbuild , to bundle all your dependencies into a single file. then you can distribute a single file without installing any dependencies

the issue is the missing @nestjs/cli to build your project. you can fix issues in 2 ways depending on how your project is maintained.

1- if you use automation tools to fetch the project, edit your dockerfile so that "one of" the following lines preceeds RUN npm run build . first one installs globally so you can use it as free command in shell, second can only be used in npm scripts. the build image will be removed automatically, so it does not matter which you use inside container but will differ if you share without containers.

> RUN npm install -g @nestjs/cli@7.5
> RUN npm i -D @nestjs/cli@7.5

2- if you own the project, either use npm i -D @nestjs/cli@7.5 or edit packages.json and add it manually before creating container

"devDependencies": {
        "@nestjs/cli": "7.5",
        "@nestjs/testing": "^7.4.2",

from the versions used in your packages.json , which are dated back to a year ago, version 7.5 seemed the appropriate one. currently this will install version 7.5.7 . if you still have the same setup, use nest --version to see what to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM