Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodemon doesn't reload in docker container when files change

I read many threads sabout this but no one solves anything.

Some say you have to add --legacy-watch (or -L) to the nodemon command. Others shows several different configurations and apparently nodody really knows what you gotta do to achieve server restarting when a file change at the volume inside a docker container.

Here my configuration so far:

Dockerfile:

FROM node:latest

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# install nodemon globally
RUN npm install nodemon -g

# Install dependencies
COPY package*.json ./
RUN npm install

# Bundle app source
COPY . /usr/src/app

# Exports
EXPOSE 3000

CMD ["npm", "start"]

docker-compose.yml

version: '3.1'

services:
    node:
        build: .
        user: "node"
        volumes:
        - ./:/usr/src/app
        ports: 
            - 3000:3000
        depends_on: 
            - mongo
        working_dir: /usr/src/app
        environment:
        - NODE_ENV=production
        expose:
        - "3000"
    mongo:
        image: mongo
        expose:
        - 27017
        volumes:
        - ./data/db:/data/db
        environment:
            MONGO_INITDB_ROOT_USERNAME: root
            MONGO_INITDB_ROOT_PASSWORD: example

package.json

{
  "name": "node-playground",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon -L"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "ejs": "^2.7.1",
    "express": "^4.17.1",
    "mongoose": "^5.7.1"
  },
  "devDependencies": {
    "nodemon": "^1.19.2"
  }
}

I tried many different setups as well. Like not installing globally nodemon but only as a project dependency. And also running the command at the docker-compse.yml, and i believe many others I don't remember right now. Nothing.

If someone has any cetainty about this, please help. Thanks!!!!

like image 553
Marcos Di Paolo Avatar asked Sep 15 '19 16:09

Marcos Di Paolo


People also ask

What does nodemon do in Docker?

— Nodemon In other words, this guy is there to supervise the main process running in the container, and to restart it if it detects some changes on surrounding files. The result service is developed with Node.js so it’s very easy to run it with nodemon instead of with the default node command.

Does nodemon react to dockerfile changes made on HTML files?

As we can see, the command defined in in the docker-compose.yml file overrides the one defined in the Dockerfile. For nodemon to react on changes made on html files located in the views folder, we will change the command specified in the docker-compose.yml file a little bit and make it look like the following.

How does node server react to changes in Docker containers?

Server notices changes to Javascript and restarts the Node.js server. So mirroring files to Docker containers will reflect all changes in the container. The npm run build:watch from the application will catch the changes and generate output files in the lib folder so the npm run dev:run will restart the server every time it has been triggered.

What is the use of Docker volumes?

Docker volumes is a feature to mirror files through your local machine and Docker environment. You can also share volumes over containers and reuse them to cache dependencies. Your goal is to watch any changes on local .ts files and mirror those changes in the container. Even though the files and the node_modules folder are in the same path.


1 Answers

Try it! This worked for me:

Via the CLI, use either --legacy-watch or -L for short. More informations here.

like image 57
Carlos Cortez Avatar answered Nov 15 '22 07:11

Carlos Cortez