In Docker Windows, nodemon does not detect file changes

My DockerFile

# Use the official Node.js 14 image as the base image
FROM node:14

# Create and set the working directory
WORKDIR /app

# Copy the package.json and package-lock.json files to the working directory
COPY package.json .

# Install the dependencies
RUN npm install

RUN npm install -g nodemon

# Copy the index.js file to the working directory
COPY index.js ./

# Expose port 3000
EXPOSE 3000

# Start the server
CMD ["nodemon", "--watch", ".", "--legacy-watch", "index.js"]


index.js

const express = require('express');
const app = express();

// Define a route for our API endpoint that returns a sample array
app.get('/sample-array', (req, res) => {
  const sampleArray = ["apple", "mango","jackfruit"];
  res.send(sampleArray);
});

// Start the server and listen for incoming requests
app.listen(3000, () => {
  console.log('Server listening on port 3000');
});

docker run -p 3000:3000 my-node-app

Docker run code is working fine and I’m getting results using postman. However, nodemon is unable to detect any changes to the file. I’ve tried a lot of options, but nothing works

Of which file? Your example docker run command does not map any files into the container.

Let’s assume for a minute you left out the volume declaration in your command, but actually mapped a Windows path to a container path.

Now here is the catch: Docker Desktop for Windows effectively runs in a VM (the Linux utility vm of WSL, which then runs Docker Desktop in a Distribution). If nodemon polled the filesystem for changes: it would work. But it uses chokidar to leverage filesystem events, which itself depends on inotify filesystem notifications. If you write data into the Windows folder mapped to the container path, it bypasses the Linux kernel and as such inotify never receives a filesystem event.

You need to use the legacy mode: https://github.com/remy/nodemon/blob/master/faq.md#help-my-changes-arent-being-detected

1 Like

Thank you for your response. I am completely new to Docker and apologize for the basic questions. Here any changes including changes in ‘index.js’ are also not working.

I have updated the Docker code CMD ["nodemon", "-L", "index.js"] but still not detecting code changes.

Is there anything I am missing?

I still need an answer for this. So far things don’t really add up. I need to understand what you do and how you do it.

1 Like

I was missing volume mapping while running the docker run.

I have rebuilt and run the following code.

docker run -v D:\learning\my-node-app\data:/app/data -p 3000:3000 my-node-app

But it doesn’t detect when I am updating code in my index.js file. Such as adding one more item into the array(sampleArray), nodemon is not detected and I have to rebuild the docker image to get the reflection.

My Dockerfile

# Use the official Node.js 14 image as the base image
FROM node:14

# Create and set the working directory
WORKDIR /app

# Copy the package.json and package-lock.json files to the working directory
COPY package.json .

# Install the dependencies
RUN npm install

RUN npm install -g nodemon

# Copy the index.js file to the working directory
COPY index.js ./

# Expose port 3000
EXPOSE 3000

# Start the server
CMD ["npm", "run","dev"]

I am really sorry, I was mapped into a different folder by looking at an example online,

I ran the following command again and it worked nicely!


docker run -v D:\learning\my-node-app:/app -p 3000:3000 my-node-app

Thank you very much for the support and I am sorry for the silly mistake :frowning: (I am a complete beginner in Docker :slight_smile: )

I am glad it is working now.

Can you describe the working solution? It will help other forum users (and especially Docker beginners like yourself) having the same problem, to not just see a discussion, but also see a post about how it was actually solved.

1 Like

This actually fixed the issue for me!
Imagine me trying to have chatGPT help me for 2 hours straight, it was going around in circles like a mindless automaton, then your reply saved the day! THX!!!