Search code examples
node.jsdockernodemon

nodemon not restarting server on code change in docker container


I'm running my node.js application in a docker container, and nodemon is not reloading on changes. Here is my dockerfile:

WORKDIR /app
COPY package*.json ./

RUN yarn install
COPY . /app

EXPOSE 8080
CMD ["yarn", "dev"] 

I am building an image with this command:

docker image build -t test7 .

and then running it with :

docker run -it -v "$(pwd)/app:/app/target_dir" test7 bash

I do yarn dev inside container, it starts the server once, but does no reload.

here's package.json file :

    "name": "self-learning",
      "version": "1.0.0",
      "main": "index.js",
      "license": "MIT",
      "scripts": {
        "start": "node index.js",
        "dev": "nodemon --legacy-watch index.js"
      },
      "dependencies": {
        "cors": "^2.8.5",
        "express": "^4.17.1",
        "express-winston": "^4.0.3",
        "multer": "^1.4.2",
        "nodemon": "^2.0.2",
        "pg": "^7.18.2",
        "winston": "^3.2.1"
      }
    }

Is there any way around it? How can I see changes without rebuilding a container?


Solution

  • I think, you need to include a ENTRYPOINT instruction. For me changing the ENTRYPOINT instruction worked. First it did not work, but then when I included --legacy-watch flag, it started working.

    Changed ENTRYPOINT from the following

    ENTRYPOINT [ "nodemon", "--inspect=0.0.0.0","./src/server.js" ]
    

    to

    ENTRYPOINT [ "nodemon", "--legacy-watch", "--inspect=0.0.0.0", "./src/server.js"]
    

    My full docker file is as follows. Its a multi target docker file.

    FROM node:alpine as debug
    
    WORKDIR /work/
    
    COPY ./package.json /work/package.json
    RUN npm install
    RUN npm install -g nodemon
    
    COPY ./ /work/src/
    
    #ENTRYPOINT [ "nodemon", "--inspect=0.0.0.0","./src/server.js" ]
    ENTRYPOINT [ "nodemon", "--legacy-watch", "--inspect=0.0.0.0", "./src/server.js"]
    
    FROM node:alpine as prod
    
    WORKDIR /work/
    COPY ./package.json /work/package.json
    RUN npm install
    COPY ./ /work/
    
    CMD node .