0

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?

5
  • 1
    Is there any particular reason you need Docker here? Can you just use plain Node, run npm run start:dev locally, and skip all of the Docker-related setup you show in the question? Commented Oct 28 at 13:25
  • I put my app in a Docker container so that I can quickly find my development environment when I change machines (or maybe I'm the one who doesn't understand when to use Docker?)... Also, if I don't launch my app in a Docker environment, there are lots of things related to other containers that will prevent my app from working. Commented Oct 28 at 18:11
  • this is the correct usage of Docker if you want the portability between machines/non clashing with the local setup but you do get this headache as a price. Are you sure the files inside the Docker container are changing? Commented Oct 29 at 11:47
  • I did a bindmount, normally the files should have changed, right? Commented Oct 31 at 10:31
  • If you're running docker on Windows / WSL, in your docker compose you need to set the environment WATCHPACK_POLLING to "true", and volume your files into the docker container. Commented Nov 9 at 20:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.