#NestJS Microservices - Docker Hot Reloading
1 messages · Page 1 of 1 (latest)
Related: #1220732902464028692 message
🙏
@celest sandal could you please elaborate on what exactly you would want to have? Maybe a dummy example?
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
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
ive solved this by adding the procps package to my alpine image.
RUN apk add procps
@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.
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)
@celest sandal Thank you for your quick reply, humm this is strange as I previously moved my repo from window to ~/adirectory/anotherdir
So if your project path is something like this /mnt/c/Users/username/projects/sleepr it means that your project is inside Windows directory
Yes it was previously on /mnt/... but I moved it to the home dir of the linux filesystem afaik
you need to move it out from /mnt
Oh
So what exactly don't work right now?
I mean doesn't work
the hot reload
give me a minute
The only difference from you is that I didnt clone again the repo, I just locally moved it
that doesn't matter
So the hot reload doesn't work at all? did you tried in few files?
I tried in the main.ts file from the reservations app, I will try in another file
Add a console inside ReservationsModule
I just added a console.log in the controller but still not hot reload.
Do we agree that the "hot reload" is reflected in the vscode terminal ? i.e the server is launched again ?
Yes I guess
enabled, in version 2 for both ubuntu and docker something
also I checked in docker desktop that unbuntu is selected (and i also set it as the default wsl )
I'll try to launch my app right now and test hot reload
I recorded my screen while testing hot reload if you pay attention you'll see that hot reload is working as the app being restarted whenever I change any file, however when I add a console.log, not every log is being rendered, depending on the file
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
I tried both console log and nestjs logger, it's same for both
whatever the problem is it's not hotreload
I'm trying to reinstall a more recent ubuntu, but I think it wouldnt change my issue
But the rest of application seems to work fine
Did you tried to get rid of pino and test again with a simple console.log ?
let me try
Sam, I tried your suggestion, but it had the same result, however I think this is default behavior of NestJS
Just watch the video to see, it seems like classes besides Modules don't get immediately initialized
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
I didn't try that, now I'm busy, I'll tell you when I try it