Search code examples
node.jsdockerdocker-composenodemon

Docker Nodemon not starting when saving


I developed my website on Ubuntu OS, but today I have moved to Windows. before moving to Windows Nodemon can work, but now if I click to save it does not restart.

Dockerfile

FROM node:current-alpine3.11

RUN mkdir /mbs_welfare

WORKDIR /mbs_welfare

COPY package.json /mbs_welfare/

RUN npm install

COPY . /mbs_welfare

RUN npm install -g nodemon

EXPOSE 3000

CMD ["nodemon","server.js"]

docker-compose.yml

version: "3.8"
services: 
    web:
        build: .
        restart: always
        ports: 
            - 3000:3000
        volumes: 
            - .:/mbs_welfare

    mysql_db:
        image: mysql:latest
        command: --default-authentication-plugin=mysql_native_password
        environment:
            MYSQL_ROOT_PASSWORD: ****
        ports:
            - 3306:3306
        volumes:
            - mysql_db:/var/lib/mysql

    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        ports:
            - 8000:80
        links:
            - mysql_db:db
        environment:
            MYSQL_USERNAME: root
            MYSQL_ROOT_PASSWORD: ****
            PMA_HOST: mysql_db

volumes:
    mysql_db:

package.json some code I tried to "start": "nodemon -L ./server.js" but it doesn't work

{
  "name": "welfare",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "dev": "nodemon ./server.js",
    "start": "nodemon ./server.js", 
    "test": "echo \"Error: no test specified\" && exit 1"
}

CMD

web_1 : [nodemon] 2.0.4
web_1 : [nodemon] to restart at any time, enter 'rs'
web_1 : [nodemon] watching path(s) *.*
web_1 : [nodemon] watching extensions: js,mjs,json
web_1 : [nodemon] starting 'node server.js'
web_1 : Server is running on port : 3000

Solution

  • nodemon needs the inotify-tools library to detect file changes in linux which does not exist on the alpine version of nodejs container. You can install it using apk add inotify-tools in your dockerfile.
    I remember that the older versions of Docker Desktop for Windows did not detect file changes on the files bind mounted from host machine (in other words inotify-tools filesystem watch did not work). This may be still an issue if you installed inotify-tools and it did not still detect file changes.
    You have to force nodemon to use polling on that case.
    Finally if none of these works for you I suggest switching to WSL on Windows which spares you a lot of headaches (I guess WSL2 has native docker support which is awesome)