简体   繁体   English

如何使用 nodejs/ts 正确构建 Docker?

[英]How to properly build the Docker with nodejs/ts?

Been struggling for a couple days to run the project in prod and it just keeps showing different errors.几天来一直在努力在 prod 中运行该项目,它只是不断显示不同的错误。 The last one(hopefully) is:最后一个(希望)是:

> rimraf dist && tsc -p tsconfig.build.json
tsc-watch/test/fixtures/failing.ts(2,1): error TS1005: '}' expected.

But tsconfig.build.json is fine:但是tsconfig.build.json很好:

{
  "extends": "./tsconfig.json",
  "exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}

It happens after I try to build/run the docker container with: docker-compose up --force-recreate --build它发生在我尝试构建/运行 docker 容器后: docker-compose up --force-recreate --build

Here's what I have in the Dockerfile:这是我在 Dockerfile 中的内容:

FROM node:11-stretch
ADD ./* $HOME/
RUN npm install
RUN npm run build
CMD npm run start:prod

And the package.json和 package.json

 { "name": "mvp-api", "private": true, "version": "0.0.1", "description": "MVP API", "license": "MIT", "scripts": { "build": "rimraf dist && tsc -p tsconfig.build.json", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "start": "ts-node -r tsconfig-paths/register src/main.ts", "start:dev": "tsc-watch -p tsconfig.build.json --onSuccess \"node dist/main.js\"", "start:debug": "tsc-watch -p tsconfig.build.json --onSuccess \"node --inspect-brk dist/main.js\"", "start:prod": "node dist/main.js", "lint": "tslint -p tsconfig.json -c tslint.json", "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" }, "dependencies": { "@nestjs/common": "^6.5.3", "@nestjs/core": "^6.5.3", "@nestjs/jwt": "^6.1.1", "@nestjs/microservices": "^6.0.4", "@nestjs/passport": "^6.1.0", "@nestjs/platform-express": "^6.0.4", "@nestjs/swagger": "^3.1.0", "@nestjs/typeorm": "^6.1.3", "@nestjs/websockets": "^6.0.4", "@nestrx/twilio": "0.0.2", "@types/mailgun-js": "^0.22.3", "@types/qrcode": "^1.3.4", "aws-sdk": "^2.513.0", "cache-manager": "^2.10.0", "cache-manager-mongodb": "^0.2.1", "cache-manager-redis-store": "^1.5.0", "class-transformer": "^0.2.3", "class-validator": "^0.10.0", "dotenv": "^8.0.0", "hbs": "^4.0.4", "is-base64": "^0.1.0", "libphonenumber-js": "^1.7.22", "mailgun-js": "^0.22.0", "mysql": "^2.17.1", "nestjs-config": "^1.4.3", "onesignal-node": "^2.1.1", "passport": "^0.4.0", "passport-jwt": "^4.0.0", "qrcode": "^1.4.1", "randomstring": "^1.1.5", "reflect-metadata": "0.1.13", "rimraf": "2.6.3", "rxjs": "6.4.0", "swagger-ui-express": "^4.0.7", "twilio": "^3.33.4", "typeorm": "^0.2.18", "vcard-generator": "0.0.7" }, "devDependencies": { "@nestjs/testing": "6.1.1", "@types/cache-manager": "^2.10.0", "@types/express": "4.16.1", "@types/jest": "24.0.11", "@types/node": "11.13.4", "@types/randomstring": "^1.1.6", "@types/supertest": "2.0.7", "jest": "24.7.1", "prettier": "1.17.0", "supertest": "4.0.2", "ts-jest": "24.0.2", "ts-loader": "^6.0.4", "ts-node": "8.1.0", "tsc-watch": "2.2.1", "tsconfig-paths": "3.8.0", "tslint": "5.16.0", "typescript": "3.4.3", "webpack": "^4.39.2", "webpack-cli": "^3.3.6", "webpack-node-externals": "^1.7.2" }, "jest": { "moduleFileExtensions": [ "js", "json", "ts" ], "rootDir": "src", "testRegex": ".spec.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" }, "collectCoverageFrom": [ "**/*.(t|j)s" ], "coverageDirectory": "../coverage", "testEnvironment": "node" } }

It used to build fine with these configurations but after purging all containers/images and trying to recreate everything it just doesn't work.它曾经使用这些配置构建良好,但是在清除所有容器/图像并尝试重新创建所有内容之后它就不起作用了。

Does anyone know how to get it to run properly in prod?有谁知道如何让它在产品中正常运行?

Apparently the problem was that the node_modules would keep copying over.显然问题是 node_modules 会继续复制。

Adding a .dockerignore file with the following content solves the problem:添加具有以下内容的.dockerignore文件可以解决问题:

node_modules

Now I can run the docker container successfully.现在我可以成功运行 docker 容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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