Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

You edit NestJS code but the container keeps running the old version

ErrorCode changes on the host do not show up in the running container

Short answer

Hot reload in a container needs three things: the dev stage of your Dockerfile must be the one that runs, your edited files must reach the container, and the container must run a watch command. Check build.target in Compose first, then add a develop.watch rule that syncs src and rebuilds on package.json.

  • Applies to Docker Compose 2.x, NestJS CLI 11, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the command the running container actually uses and the image stage it was built from. If it is node dist/main, you are running the production stage, not a watcher.

    docker compose config | grep -n -A4 "build:"
    docker compose top api
  2. 2Match what you see, then jump to its fix

    1. 1the process list shows node dist/main and no nest processCompose builds the last (production) stage of the multistage DockerfileFix
    2. 2The container logs show the first start only; saving a file produces no recompile lineYour edited files never reach the containerFix
    3. 3Dependencies missing or different right after you added a volume that maps . to /appA bind mount of the whole project hides the node_modules built in the imageFix

What does the error look like?

  • Code changes on the host do not show up in the running container
  • nest start --watch prints nothing after a file is saved
  • The container starts the production command (node dist/main) even in development

Cause 1 of 3

Compose builds the last (production) stage of the multistage Dockerfile

1Check

Without a target, a multistage build produces the final stage. If your Dockerfile ends with a slim production stage, that is what runs.

grep -n "^FROM" Dockerfile
docker compose config | grep -n -B1 -A3 "target"

2Fix

Name the development stage in the Dockerfile and tell Compose to build exactly that stage with build.target. Give that stage a CMD that starts the watcher.

# Dockerfile
FROM node:22 AS dev
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
CMD ["npx", "nest", "start", "--watch"]

# compose.yaml
services:
  api:
    build:
      context: .
      target: dev

3Verify

Rebuild and check that the nest watcher is the running process.

docker compose up --build -d api
docker compose top api

Confirmed by docs.docker.com

Did this fix it?

Cause 2 of 3

Your edited files never reach the container

1Check

COPY puts a snapshot of the source into the image at build time. Later edits on the host are not seen unless you sync or mount them.

docker compose config | grep -n -E "watch|volumes"

2Fix

Add a Compose watch rule that syncs src into the container, and a rebuild rule for package.json. Watch works only for services that use build, and the container user must be able to write to the target path.

services:
  api:
    build:
      context: .
      target: dev
    develop:
      watch:
        - action: sync
          path: ./src
          target: /app/src
          ignore:
            - node_modules/
        - action: rebuild
          path: package.json

3Verify

Start in watch mode, save a file in src and read the container log.

docker compose up --watch
# in a second terminal
docker compose logs -f api

Confirmed by docs.docker.com

Did this fix it?

Cause 3 of 3

A bind mount of the whole project hides the node_modules built in the image

1Check

A bind mount hides whatever already exists at the target path in the container. Mounting your project folder over /app hides the dependencies that npm ci installed in the image and replaces them with the host folder.

docker compose config | grep -n -B2 -A4 "volumes"
docker compose exec api ls /app/node_modules | head

2Fix

Stop mounting the whole project. Use a develop.watch sync rule that ignores node_modules/ (Compose documents this as the purpose of watch next to bind mounts) so dependencies stay the ones installed in the image.

develop:
  watch:
    - action: sync
      path: ./src
      target: /app/src
      ignore:
        - node_modules/

3Verify

Remove the volumes entry, recreate the container, and confirm the dependencies exist.

docker compose up --build --watch
docker compose exec api ls /app/node_modules | head

Confirmed by docs.docker.com

Did this fix it?

How do I stop it coming back?

  • Keep one named dev stage in the Dockerfile and set build.target in the Compose file that developers use.
  • Use develop.watch with a rebuild rule for package.json so new dependencies reach the image.
  • Keep node_modules out of every sync or mount path.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.