node.jsdockerdocker-composedockerfiledocker-volume

Docker nodemon doesn't hot reload even though the volumes has been set up


So I have a simple node js app that I want to dockerize.

const express = require("express");
const app = express();
const port = process.env.PORT || 8080;

app.get("/", (req, res) => {
  res.json({
    random: Math.random(),
  });
});

app.listen(port, () => {
  console.log(`Listening on port ${port}`);
});

And here's the package.json file

  "scripts": {
    "dev": "nodemon server.js"
  },

To dockerize it and make my development easier I create a Dockerfile and docker-compose file for it like so

FROM node:18-alpine

RUN npm install -g nodemon

WORKDIR /app

COPY package*.json .

RUN npm install

COPY . .

EXPOSE 8080

CMD [ "npm", "run" ,"dev" ]

services:
  api:
    build: .
    container_name: random-api
    volumes:
      - ./:/app
    environment:
      - PORT=8080
    ports:
      - "4000:8080"

As you can see I have set up the docker volumes, but strangely the nodemon is not reloading when the code change. am I missing something? what should I do to make the dockerize app reflect the change


Solution

  • With Docker for Windows watching a mapped folder, you need to use nodemon --legacy-watch.

    User Meyey said here:

    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 legacy mode.