Use nodemon with docker and docker-compose
Solution 1
The issue with nodemon with inspect
on restart. You can read more about the issue here. You can try the work around mentioned by nodemon team
"inspect": "kill-port --port 9229 && node --inspect=0.0.0.0:9229 build/startup.js",
"start_watch_inspect": "nodemon --delay 80ms --watch build/ build/startup.js --exec 'npm run inspect'",
You can make it working using below command if you can manage without inspect
"scripts": {
"start": "nodemon index.js"
}
This will work with mounting the directory
docker run --rm -v /home/myapp:/root --name test -it testnode
OR
copy code to docker build and update file inside the container will also work fine.
Solution 2
If you start nodemon with the -L
or --legacy-watch
flag, it will restart correctly when changes are detected. The script in your package.json
file would be:
{
"scripts": {
"start": "nodemon -L --inspect=0.0.0.0 index.js"
}
}
I came across this solution as a comment on this gist: https://gist.github.com/ksmithut/e126f7ddb40b760487a17e8b569a77b5#gistcomment-2725750
This can be found in the official documentation at https://github.com/remy/nodemon#application-isnt-restarting
Related videos on Youtube
Артем Дачевский
Updated on June 04, 2022Comments
-
Артем Дачевский almost 2 years
I'm using nodemon with docker-compose. Here is my Dockerfile:
FROM node:10 WORKDIR /usr/src/app COPY package*.json ./ RUN npm install COPY . . CMD ["npm", "start"]
My docker-compose.yml
version: '3' services: app: build: . volumes: - .:/usr/src/app - /usr/src/app/node_modules container_name: docker-node-mongo restart: always ports: - 3000:3000 - 9229:9229 command: npm start links: - mongo - redis mongo: container_name: mongo image: mongo ports: - "27017:27017" redis: image: redis:alpine volumes: - /var/redis/data:/data
And my package.json script:
{ "scripts": { "start": "nodemon --inspect=0.0.0.0 index.js" } }
According to the code inside of my working docker container, my code is updating, but I don't have any reload.