Pm2: PM2 & Docker

Created on 27 May 2015  路  6Comments  路  Source: Unitech/pm2

Hi,
I've implemented PM2 in Docker with a volume mounted for testing.
I want that everytime the file changes it restarts to show the changes. For that purpose, I'm executing pm2 this way "pm2 start src/index.js --watch --no-daemon". The process run fine, but file changes are not reflected.
In my dev enviroment it works as expected, but not in docker.
Is that a bug/incompatibility?

EDIT: I changed my Dockerfile because there was a timezone difference, that seems to be a common issue with this kind of tasks ( I'm using Mac + Kitematic ), but the problem persists.
Now time of files is correctly in sync.
I've also checked the content, and everything is fine, but PM2 is not able to detect changes on the files to restart the server.

Most helpful comment

Shared volumes and file watching are known to not to work very well together. Since you are using a docker+Mac OSX I guess VirtualBox (NFS) is the problem. Try passing usePolling: true as described in https://github.com/paulmillr/chokidar/issues/242. It works for us but comes with a higher CPU cost.

Example processes.json:

{
  "apps" : [{
    ...
    "watch"      : true,
    "watch_options": {
      "usePolling": true
    }
  }]
}

All 6 comments

Just tried it locally without docker, works fine,

Can you share with us a procedure to reproduce the bug?

thanks

It's a problem with Docker and mounted volumes only.
Example: docker run -d -p 8080:8080 --name node_dev -v $PWD:/app/ trk/nodejs_dev

Using this Dockerfile:
FROM google/nodejs
RUN npm install pm2 -g
WORKDIR /app
CMD []

ENV NODE_ENV dev
ENV TZ=Atlantic/Canary
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

ENTRYPOINT ["/nodejs/bin/npm", "start"]

Other libraries as nodemon have the same problem: https://github.com/remy/nodemon/issues/419
It can be related to some time problems. I have my Docker container synced to use the same timezone as the system but it's still not working.

Shared volumes and file watching are known to not to work very well together. Since you are using a docker+Mac OSX I guess VirtualBox (NFS) is the problem. Try passing usePolling: true as described in https://github.com/paulmillr/chokidar/issues/242. It works for us but comes with a higher CPU cost.

Example processes.json:

{
  "apps" : [{
    ...
    "watch"      : true,
    "watch_options": {
      "usePolling": true
    }
  }]
}

Thanks @madebyherzblut, closing this.

To reduce the CPU cost change the interval and binaryInterval option (default 100 and 300).

I've struggled with this problem for a couple of days, and finally found a solution that works for me (even though this should be fixed in the mount-setup virtualbox, IMHO)

Nothing of the above worked for me, so I ended up creating a small bash-script that starts my servers, restarts the pm2 instance inside docker from the host OS and tails docker logs. To avoid zombie processes, I'vce included a trap that kills the docker processes started by docker-compose

#!/bin/bash

function clean_up {
  docker-compose kill
  exit
}

trap clean_up SIGHUP SIGINT SIGTERM

docker-compose build
docker-compose start

# tail all logs 
docker-compose logs&

# fswatch is available for linux and osx.
fswatch -0 src | while read -d "" event
  do
    echo "file changed: ${event}"
    image=$(docker ps | grep <docker app-name> | cut -d " " -f1 )
    docker exec -t $image ./node_modules/.bin/pm2 restart <pm2 app-name>
  done

Official Docker image released that includes:

  • Alpine Linux
  • Node.js 4 prebuilt
  • PM2 v2 with pm2-docker command
  • dumb-init to handle signals in a clean way
  • Optional Keymetrics integration

Here is the link to the Docker hub: https://hub.docker.com/r/keymetrics/pm2-docker-alpine/
Github repository: https://github.com/keymetrics/pm2-docker-alpine

Was this page helpful?
0 / 5 - 0 ratings

Related issues

morfies picture morfies  路  3Comments

shaunwarman picture shaunwarman  路  3Comments

phra picture phra  路  3Comments

webchaz picture webchaz  路  3Comments

alexpts picture alexpts  路  3Comments