I'm running a NestJS application inside a Docker container, but the hot reload doesn't work. When I edit any .ts file on my host machine, the NestJS server doesn't restart automatically.
I have to stop the container and run it again to see changes
Dockerfile:
ARG NODE_VERSION=20.17.0
FROM node:${NODE_VERSION}-alpine as base
WORKDIR /usr/src/app
EXPOSE 3000
FROM base as dev
RUN --mount=type=bind,source=.npmrc,target=.npmrc \
--mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=package-lock.json,target=package-lock.json \
--mount=type=cache,target=/root/.npm \
npm ci --include=dev
COPY . .
CMD ["npm", "run", "start:dev"]
FROM base as prod
ENV NODE_ENV production
RUN --mount=type=bind,source=.npmrc,target=.npmrc \
--mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=package-lock.json,target=package-lock.json \
--mount=type=cache,target=/root/.npm \
npm ci --omit=dev
USER node
COPY . .
CMD ["node", "dist/main"]
FROM base as test
ENV NODE_ENV=test
RUN --mount=type=bind,source=.npmrc,target=.npmrc \
--mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=package-lock.json,target=package-lock.json \
--mount=type=cache,target=/root/.npm \
npm ci --include=dev \
COPY . .
RUN ["npm", "run", "test:all"]
compose.yaml file:
services:
app:
container_name: univers-app
build:
context: .
target: dev
env_file:
- .env
ports:
- 3000:3000
- 9229:9229
volumes:
- ./:/usr/src/app
- /usr/src/app/node_modules
depends_on:
smtp-server:
condition: service_healthy
db:
condition: service_healthy
cache:
condition: service_healthy
I'm starting my containers with docker compose up -d
Partial package.json file:
"scripts": {
//... omited for simplicity purpose
"start": "nest start",
"start:dev": "nest start --watch",
//... omited for simplicity purpose
},
How to make hot reload work?
npm run start:devlocally, and skip all of the Docker-related setup you show in the question?