Search code examples
dockerdocker-composedockerfiledocker-swarmdocker-volume

NodeJS is not detecting change in Docker Bind Mount until Swarm is restarted


I'm building a NodeJS application on Docker in Swarm mode (single node). I'm using bind mount volume for NodeJS source code. Everything runs perfectly and I can see the output in localhost from NodeJS and Express, but when I change something in NodeJS code (which is in a bind mount volume), nothing changes. I have to restart my service to observe the changes. Earlier when I was working with Docker Compose only, it never happened, but now when I have switched to Swarm, I'm experiencing problems.

I'm using Docker 18 with Visual Studio Code 1.39 on macOS 10.14.6

Dockerfile

FROM node:12-alpine
WORKDIR /node-dir
COPY package*.json ./
RUN npm install

docker-compose.yml file

# Docker-compose.yml
version: '3.7'

services:

    node-service:

        image: node-img:1.0

        ports:
            - 80:3000
        working_dir: "/node-dir"

        volumes:
        - ./node-dir/source:/node-dir/source

        networks:
            - ness-net

        command: npm start

networks:

    ness-net:

I also read that it could be due to the inodes, most editors when saving the file breaks the link. But it was working correctly under docker-compose with Visual Studio Code, its behaviour is changed only in Docker Swarm.

Update: I served a static html file using Nginx with bind mount, and I can easily change that file using VS Code and it's reflected. Its only NodeJS which is not detecting changes in a file.


Solution

  • If your volume mapping is correct, the source code changes should reach your node.js app container.

    You can verify it by inspecting the source code inside the container after you make a change on docker host.

    I'm currently in development mode, and I have to test the source code repeatedly so I want to use bind mounts to make development and testing easier.

    However, your source code change won't be effective until node process inside the container reloads and picks up the changes.

    In order to achieve this you have to use nodemon. Nodemon will pick the changes in the source code and reload node process along with the changes.

    Another, longer alternative would be building new docker image and then updating your app using: docker service update --image=...

    You can also use tilt to automate all of the above actions.