Search code examples
dockerexpressdockerfilecontainersnodemon

Nodemon doesn't reload my app.js on my docker container


I decided to make a server with Express in a container and installed nodemon to watch and reload my code modifications, but, for some reason, the nodemon on container doesn't reload when I modify my code. How can I fix that?

My Dockerfile:

FROM node:14-alpine

WORKDIR /usr/app

RUN npm install -g nodemon

COPY . .

EXPOSE 3000

CMD ["npm","start"]

My package.json:

{
  "name": "prog-web-2",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon app.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/willonf/prog-web-2.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/willonf/prog-web-2/issues"
  },
  "homepage": "https://github.com/willonf/prog-web-2#readme",
  "dependencies": {
    "express": "^4.17.1"
  }
}

My app.js:

const express = require("express")

const app = express()

app.get("/", (req, res) => {
    res.end("Hello, World!")
});

app.listen(3000);

Solution

  • You are copying all of your files into the docker container.

    COPY . .
    

    So when you modify your local file, you are not modifying the file inside the docker container and the nodemon inside the docker container can't detect any changes.

    It is possible to get the behavior you want using Docker Volumes. You can configure them, so that the docker container shares the working directory with the host system. If you change a file on the host nodemon would detect changes in this case.

    The answer in this post is showing an example on how to accomplish that.