markWanka
markWanka

Reputation: 784

How to use docker with NestjS apps

I have a NestJS project with many apps, structure below:

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. 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?

thanks for any help

Upvotes: 1

Views: 1762

Answers (2)

Pavel
Pavel

Reputation: 668

It is possible to have just one Dockerfile for all apps.

FROM node:18-alpine As build

ARG SERVICE_NAME

WORKDIR /usr/src/app

COPY --chown=node:node package*.json yarn.lock ./

RUN yarn install --frozen-lockfile

COPY --chown=node:node . .

RUN yarn build $SERVICE_NAME

ENV NODE_ENV production
RUN yarn install --production --frozen-lockfile

USER node

###################
# PRODUCTION
###################

FROM node:18-alpine As production

ARG SERVICE_NAME
ENV NODE_ENV production

WORKDIR /usr/src/app
COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules
COPY --chown=node:node --from=build /usr/src/app/dist/apps/${SERVICE_NAME}/* .

USER node

CMD ["node", "main.js"]

Then the docker build command would look like:

docker build -t <tag-name> --build-arg SERVICE_NAME=<nestjs-app-name> .

Upvotes: 0

Hugo Sohm
Hugo Sohm

Reputation: 3310

You can have multiple Dockerfiles at your root with different filenames.

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

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

.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

Upvotes: 5

Related Questions