Nodemon is not working in Docker environment
Asked Answered
L

7

20

I'm using Docker with fig to build NodeJS dev-env.

While I using nodemon to watch the server.js, changing server.js won't restart the server.

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

But while I changed from nodemon to supervisor, then it worked!

CMD ["supervisor", "/nodeapp/server.js"]

Does anyone know where the problem is?

More informations are below:


My fig folder structure:

app/server.js
    package.json
    node_modules/
fig.yml
Dockerfile

fig.yml:

nodejs:
  build: .
  ports:
    - "8080:8080"

Dockerfile:

RUN apt-get update --fix-missing
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

# NVM
RUN curl -sL https://deb.nodesource.com/setup | sudo bash - && \
  apt-get install -y nodejs

VOLUME ./app:/nodeapp
WORKDIR /nodeapp

RUN rm /bin/sh && ln -s /bin/bash /bin/sh && \
  npm install -g nodemon mocha supervisor
CMD ["nodemon", "/nodeapp/server.js"]

Server.js: (sample code from NodeJS website)

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello 12\n');
}).listen(8080);

console.log('Server running at http://127.0.0.1:8080/');
Lowder answered 1/12, 2014 at 10:45 Comment(0)
W
3

first up - VOLUME ./app:/nodeapp does not do what you want - you're creating a directory in the image called /app:/nodeapp - and so at no point is the server.js file getting onto your image.

test using docker run --rm -it yourimagename ls -la

changing your Dockerfile to

FROM ubuntu

RUN apt-get update --fix-missing
RUN apt-get install -yq curl
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

# NVM
RUN curl -sL https://deb.nodesource.com/setup | sudo bash - && \
  apt-get install -y nodejs

#VOLUME ./app:/nodeapp
ADD     app /nodeapp
WORKDIR /nodeapp

RUN rm /bin/sh && ln -s /bin/bash /bin/sh && \
  npm install -g nodemon mocha supervisor
CMD ["nodemon", "/nodeapp/server.js"]

gets me:

mini:nodaemon sven$ docker run --rm -it -p 8080:8080 nodaemon     
2 Dec 02:27:52 - [nodemon] v1.2.1
2 Dec 02:27:52 - [nodemon] to restart at any time, enter `rs`
2 Dec 02:27:52 - [nodemon] watching: *.*
2 Dec 02:27:52 - [nodemon] starting `node /nodeapp/server.js`
Server running at http://127.0.0.1:8080/
Wolfie answered 2/12, 2014 at 2:29 Comment(1)
Thanks for your reply. There's a mistake in my Dockerfile. Actually, what I want is: My nodejs env is in the Docker container. While I change my code in the project folder on my host OS, the container will detect the changed file and then restart the server.js automatically. It's my mistake that I didn't describe my problem well. Can Docker do that?Lowder
E
64

There is a special option --legacy-watch to enable nodemon's legacy watching mode:

nodemon --legacy-watch --watch ./your/files/here --exec "npm run some-command"

Here is the respective nodemon docs as @Jonathan pointed out in comments and the Docker issue.

Elastic answered 14/12, 2018 at 8:31 Comment(4)
This is apparently the recommended way to get nodemon working inside a Docker container github.com/remy/nodemon#application-isnt-restartingMaines
-L shorthand, but this also worked for meSelia
Really simple and great solution! Props!Photographer
That is fucking amazing solution, that is really working in my docker container node js serverMaryland
N
6

This is how I do it:

You will need nodemon version 1.3.0-5 for this (npm i -g nodemon@dev)

.dockerignore:

node_modules/*

Dockerfile:

FROM node:0.10

WORKDIR /nodeapp
ADD ./package.json /nodeapp/package.json
RUN npm install --production

ADD ./app /nodeapp/app

EXPOSE 8080

CMD ["node", ".", "--production"]

package.json:

{
  "name": "fig-nodemon",
  "version": "1.0.0",
  "description": "",
  "main": "./app/server.js",
  "scripts": {
    "nodemon": "fig up -d && fig run nodejs npm i --development && nodemon -x \"fig kill nodejs && fig build nodejs && fig start nodejs && fig logs nodejs\""
  },
  "author": "",
  "license": "MIT"
}

fig.yml:

nodejs:
  build: .
  command: node . --development
  volumes:
    - ./app:/nodeapp/app
  ports:
    - "8080:8080"

app/server.js:

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello 13\n');
}).listen(8080);

console.log('Server running at http://127.0.0.1:8080/');

then I run npm run nodemon to get started.

Nathalie answered 9/12, 2014 at 20:35 Comment(0)
W
3

first up - VOLUME ./app:/nodeapp does not do what you want - you're creating a directory in the image called /app:/nodeapp - and so at no point is the server.js file getting onto your image.

test using docker run --rm -it yourimagename ls -la

changing your Dockerfile to

FROM ubuntu

RUN apt-get update --fix-missing
RUN apt-get install -yq curl
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

# NVM
RUN curl -sL https://deb.nodesource.com/setup | sudo bash - && \
  apt-get install -y nodejs

#VOLUME ./app:/nodeapp
ADD     app /nodeapp
WORKDIR /nodeapp

RUN rm /bin/sh && ln -s /bin/bash /bin/sh && \
  npm install -g nodemon mocha supervisor
CMD ["nodemon", "/nodeapp/server.js"]

gets me:

mini:nodaemon sven$ docker run --rm -it -p 8080:8080 nodaemon     
2 Dec 02:27:52 - [nodemon] v1.2.1
2 Dec 02:27:52 - [nodemon] to restart at any time, enter `rs`
2 Dec 02:27:52 - [nodemon] watching: *.*
2 Dec 02:27:52 - [nodemon] starting `node /nodeapp/server.js`
Server running at http://127.0.0.1:8080/
Wolfie answered 2/12, 2014 at 2:29 Comment(1)
Thanks for your reply. There's a mistake in my Dockerfile. Actually, what I want is: My nodejs env is in the Docker container. While I change my code in the project folder on my host OS, the container will detect the changed file and then restart the server.js automatically. It's my mistake that I didn't describe my problem well. Can Docker do that?Lowder
H
3

The server will restart automatically after save if you put the following code in the package.json file:

"nodemonConfig": {
    "legacyWatch": true
}

This is also mentioned here.

Handbill answered 27/12, 2022 at 21:25 Comment(0)
H
1

Change your backend package.json file nodemon script to below. More info https://github.com/remy/nodemon#application-isnt-restarting

"start": "nodemon -L index.js --ignore './tests'"

Hemo answered 25/6, 2023 at 9:17 Comment(0)
O
1

There is a special option for node, it works in containers.

node --watch-path=/app server.js

node -version 18.0.0+

Orit answered 21/10, 2023 at 19:45 Comment(0)
G
0

For me, I had to configure nodemon legacy-watch AND docker's watch.

package.json:

"dev": "nodemon --watch \"./**\" --legacy-watch --ext \"js\" --exec \"node src/index.js\""

Compose:

services:
  frontend:
    build: ./frontend
    command: npm run dev
    develop:
      watch:
        - action: sync
          path: ./frontend/src
          target: /app/src
          ignore: 
            - node_modules/
        - action: rebuild
          path: ./frontend/package.json

    ports:
      - "8080:8080"

I tried one without the other and vice versa and it does not work. Only both together work and give me live update/refresh.

Geier answered 29/5 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.