简体   繁体   中英

Nodemon is not restarting using docker container

I've been trying to make my server restart when I make changes to my app.js file for two days without success now. Im using docker desktop and nodemon. And trying to follow the guide given by my teacher. I was told to make this work i should use volume, so in the terminal i run:

docker build --tag=jade:latest .

docker run -v /Users/Jake/Documents/AdvancedWeb/JadeWeb/application/src:/src -p 8080:8080 jade

On the second row I have probably tried a hundred different syntaxes, this seems to be the one closest to working and is what makes the most sense to me. It starts the server and nodemon tells me it's waiting for changes.

Dockerfile:

FROM node:13.3.0-stretch
EXPOSE 8080
WORKDIR /application
COPY package.json package.json
RUN npm install
COPY src src
CMD ["npm", "run", "start"] 
// I have tried ALOT of variants of CMD aswell, like ["npm", "run", "start", "src/app.js"], 
// ["npm", "run", "dev"], ["node", "src/app.js"] ["nodemon", "src/app.js"]
// ["npm", "start"]

package.json:

    {
  "name": "application",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon --legacy-watch src/app",
    "dev": "nodemon src/app -L"
  },
  "devDependencies": {
    "nodemon": "^2.0.2"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

So I forgot to close the server for a while, and apparently it does restart, but it takes about 10 seconds for the process to start, then the server is down for a few seconds, and then it restart. Also, after making a change and saving my app.js file, i get [nodemon] restarting due to changes...

7 times before it accually restarts.

If i just do npm run start nodemon works as intended. (A single fast restart)

I had the same problem. Searched everywhere. Every possible solution. From attaching -L for --legacy-watch to --watch in scripts with nodemon command. My problem was in docker-compose.yml. In your app service, have a volumes attribute. Volumes basically maps the code you are writing in your system to the container environment. So, if I write,

volumes: -"./application:/application"

It means that the application folder in which your nodejs app lies maps to /application in container. Now, whatever changes you make in your system, will be reflected in the container code. Since, nodemon is already active in the node, it will restart!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM