node.jsdockernodemonfignode-supervisor

Nodemon is not working in Docker environment


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/');

Solution

  • 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/