I am working on a Nest.js project which uses Nginx as a load balance, Redis for session management, and it connects to Postgres database. I want a situation whereby if I make changes to my codebase my docker container should update itself without me having to kill and restart the service. Find below my Dockerfile and docker-compose.yml content
Dockerfile
# Development stage
FROM node:18.18.2-slim AS development
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=development
COPY . .
RUN npm run build
# Production stage
FROM node:18.18.2-slim AS production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
COPY . .
COPY --from=development /usr/src/app/dist ./dist
# CMD ["node", "dist/main"]
CMD [ "npm", "run", "start:dev" ]
Thanks @rimelek. I was able to set the watch but nothing seems to happen when I save file changes. Here are my updated Dockerfile and docker-compose.yml
Dockerfile
# Development stage
FROM node:18.18.2-slim AS development
WORKDIR /usr/src/app
# Install nodemon globally
# RUN npm install -g nodemon
COPY package*.json ./
RUN npm install --only=development
# RUN npm install -g @nestjs/cli
COPY . .
RUN npm run build
# Production stage
FROM node:18.18.2-slim AS production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
# RUN npm install pm2 -g
# RUN npm install -g @nestjs/cli
COPY . .
COPY --from=development /usr/src/app/dist ./dist
CMD ["node", "dist/main"]
# CMD ["nodemon", "--watch", "src", "--exec", "ts-node", "dist/main"]
# CMD [ "npm", "run", "start:dev" ]
this is my VS code terminal
$ docker-compose watch
[+] Building 0.0s (0/0) docker:default
[+] Running 4/4
✔ Container lagbaja-db Started 0.0s
✔ Container api-lagbaja-redis-1 Started 0.0s
✔ Container api-lagbaja-nginx-1 Started 0.0s
✔ Container api-lagbaja-dev-1 Started 0.0s
watching [C:\Forge\lagbaja\api-lagbaja\scr C:\Forge\lagbaja\api-lagbaja\package.json]
Thanks for pointing this out. I made changes to the package.json flie and it triggered a rebuild. So, why isn’t it watching my src folder? I’ll do some tinkering and revert
@bluepuma77 it is syncing file changes now but I don’t see the changes when I call the modified endpoint
Syncing dev after changes were detected:
- C:\Forge\lagbaja\api-lagbaja\src\api\post\post.service.ts
Syncing dev after changes were detected:
- C:\Forge\lagbaja\api-lagbaja\src\api\post\post.service.ts
Syncing dev after changes were detected:
- C:\Forge\lagbaja\api-lagbaja\src\api\post\post.service.ts
Syncing dev after changes were detected:
- C:\Forge\lagbaja\api-lagbaja\src\api\post\post.service.ts
- C:\Forge\lagbaja\api-lagbaja\src\api\post\post.controller.ts