#NestJS Microservices - Docker Hot Reloading

1 messages · Page 1 of 1 (latest)

celest sandal
#

Do you guys know guides/docs to implement Docker hot reloading in NestJS Microservices? I couldn't find a good solution. I mean there seems to be a workaround that involves modifying tsconfig file (watchOptions), but it's a weird solution, and I don't know if that could cause other issues.

sharp stream
trim mason
#

@celest sandal could you please elaborate on what exactly you would want to have? Maybe a dummy example?

celest sandal
# trim mason <@868272584926588938> could you please elaborate on what exactly you would want ...

Sure, I've been trying to learn NestJS microservices following this Udemy course: https://www.udemy.com/course/nestjs-microservices-build-deploy-a-scaleable-backend

Here's the current Dockerfile of the reservations app:
`
FROM node:alpine As development

WORKDIR /usr/src/app

COPY package.json ./
COPY pnpm-lock.yaml ./

RUN npm install -g pnpm
RUN pnpm install
RUN pnpm install @nestjs/cli

COPY . .

RUN pnpm run build

FROM node:alpine As production

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

WORKDIR /usr/src/app

COPY package.json ./
COPY pnpm-lock.yaml ./

RUN npm install -g pnpm
RUN pnpm install --prod

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

CMD ["node", "dist/apps/reservations/main"]
`

And here's the docker-compose.yml file:
services: reservations: build: context: . dockerfile: ./apps/reservations/Dockerfile target: development command: pnpm run start:dev reservations ports: - '3000:3000' volumes: - .:/usr/src/app - /usr/src/app/node_modules

celest sandal
# trim mason <@868272584926588938> could you please elaborate on what exactly you would want ...

Even though I set up a bind mount for the app, it doesn't seem to work, and in addition, I have strange behavior with the app. After I ran docker-compose up --build, I added to console.log() statement to the app, one in the main.ts inside the bootstrap function, and one in the constructor of the ReservationsModule class, but hot reload didn't work, and after rebuilding the Image only the console.log() statement in the ReservationsModule class was printed, and the one in the main.ts file was not printed. I'm not sure what's going on, I googled a lot, but I couldn't find a confirmed solution. While comments suggested that I had to modify watchOptions in the tsconfig.json file, others mentioned that it slows down the hot reload, and it's not recommended.

#

I'm gonna try Compose Watch if doesn't work out

mint saddle
#

ive solved this by adding the procps package to my alpine image.

RUN apk add procps
mellow cloud
#

@celest sandal Hello, did you manage to find a valable solution to your issue ?
As far as i'm concerned I encountered the exact same issue with the exact same code (same tutorial).
I'm under typescript 5.1.3 and neither the watchOptions solution nor the @mint saddle 's one (if I'm not mistaking by just putting the command RUN apk add propcs in the dockerfile of the reservation service) works.

celest sandal
# mellow cloud <@868272584926588938> Hello, did you manage to find a valable solution to your ...

Hi Sam, yes I found the solution, the problem is that your project directory is inside the Windows file system which works poorly with Docker if you can access your project directory from an Ubuntu terminal that doesn't necessarily mean that it's inside a Linux file system, which is what confused me in the first place, so the solution would be to move the project to Linux file system (the easiest way would be to save the changes the github, and then cd into Linux file system and clone the project again)

mellow cloud
#

@celest sandal Thank you for your quick reply, humm this is strange as I previously moved my repo from window to ~/adirectory/anotherdir

celest sandal
#

So if your project path is something like this /mnt/c/Users/username/projects/sleepr it means that your project is inside Windows directory

mellow cloud
#

Yes it was previously on /mnt/... but I moved it to the home dir of the linux filesystem afaik

celest sandal
#

you need to move it out from /mnt

#

Oh

#

So what exactly don't work right now?

#

I mean doesn't work

mellow cloud
#

the hot reload

celest sandal
#

give me a minute

mellow cloud
#

The only difference from you is that I didnt clone again the repo, I just locally moved it

celest sandal
#

So the hot reload doesn't work at all? did you tried in few files?

mellow cloud
#

I tried in the main.ts file from the reservations app, I will try in another file

celest sandal
#

Yeah that didn't work for me either

#

in main.ts

celest sandal
mellow cloud
#

I just added a console.log in the controller but still not hot reload.

celest sandal
#

hmm

#

You sure WSL2 is enabled?

mellow cloud
#

Do we agree that the "hot reload" is reflected in the vscode terminal ? i.e the server is launched again ?

celest sandal
#

Yes I guess

mellow cloud
#

also I checked in docker desktop that unbuntu is selected (and i also set it as the default wsl )

celest sandal
#

I'll try to launch my app right now and test hot reload

celest sandal
mellow cloud
#

What the heck...I don't understand why the log aren't displayed in your side. Maybe they interfer with pino ?
On my side, the app doesn't restart at all nevermind the file I modify

celest sandal
#

I tried both console log and nestjs logger, it's same for both

#

whatever the problem is it's not hotreload

mellow cloud
#

I'm trying to reinstall a more recent ubuntu, but I think it wouldnt change my issue

celest sandal
#

But the rest of application seems to work fine

mellow cloud
#

Did you tried to get rid of pino and test again with a simple console.log ?

celest sandal
#

let me try

celest sandal
#

Just watch the video to see, it seems like classes besides Modules don't get immediately initialized

mellow cloud
#

Ah yes you're right

#

is it working if you remove the line where you call the constructor and instead put a console.log in a get or post method in the controller and call that endpoint ?

#

Upgrading the distro to ubuntu22.04 doesnt change anything

celest sandal
#

I didn't try that, now I'm busy, I'll tell you when I try it