简体   繁体   English

如何将 docker 与 NestjS 应用程序一起使用

[英]How to use docker with NestjS apps

I have a NestJS project with many apps, structure below:我有一个包含许多应用程序的 NestJS 项目,结构如下:

my-project:
-- apps;
--- app-one
---- src
---- tsconfig.app.json
--- app-two
---- src
---- tsconfig.app.json
-- libs
-- package.json
-- etc...

I have two apps in one project: app-one and app-two , How to use docker to deploy app-one or app-two .我在一个项目中有两个应用程序: app-oneapp-two ,如何使用 docker 部署app-oneapp-two When I have one app I know how to do this, I will create Dockerfile at the package.json directory and build/run, but how to do this with nested apps?当我有一个知道如何执行此操作的应用程序时,我将在package.json目录中创建Dockerfile并构建/运行,但是如何使用嵌套应用程序执行此操作?

thanks for any help谢谢你的帮助

You can have multiple Dockerfiles at your root with different filenames.您可以在根目录下拥有多个具有不同文件名的 Dockerfile。

my-project:
-- apps;
--- app-one
---- src
---- tsconfig.app.json
--- app-two
---- src
---- tsconfig.app.json
-- libs
-- package.json
-- Dockerfile.app-one
-- Dockerfile.app-two

For the Dockerfiles, you just need to run your custom scripts to build the specified app对于 Dockerfiles,您只需要运行您的自定义脚本来构建指定的应用程序

Dockerfile.app-one

FROM node:12.17-alpine as builder

WORKDIR /build
COPY package.json yarn.lock ./
RUN yarn

COPY . .
RUN  yarn build:app-one

EXPOSE 3000

CMD [ "yarn", "start:app-one"]

package.json

"scripts": {
  "build:app-one": "nest build app-one",
  "build:app-two": "nest build app-two",
  "start:app-one": "nest start app-one",
  "start:app-two": "nest start app-two",
}

nest-cli.json

{
  "projects": {
    "app-one": {
      "type": "application",
      "root": "apps/app-one",
      "entryFile": "main",
      "sourceRoot": "apps/app-one/src",
      "compilerOptions": {
        "tsConfigPath": "apps/app-one/tsconfig.app.json",
        "assets": []
      }
    },
    "app-two": {
      "type": "application",
      "root": "apps/app-two",
      "entryFile": "main",
      "sourceRoot": "apps/app-two/src",
      "compilerOptions": {
        "tsConfigPath": "apps/app-two/tsconfig.app.json",
        "assets": []
      }
    },
  }
}

Then, specify the filename in your build/deploy tasks in your CI/CD然后,在 CI/CD 的构建/部署任务中指定文件名

.gitlab-ci.yml

image: docker:git

services:
  - docker:dind

stages:
  - build

build-app-one:
  stage: build
  script:
    - docker build . -f Dockerfile.app-one

build-app-two:
  stage: build
  script:
    - docker build . -f Dockerfile.app-two

If you need more informations, read the documentation about monorepo architecture in NestJS如果您需要更多信息,请阅读 NestJS 中有关monorepo 架构的文档

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

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