3

I build a Docker image with Node.js and pm2. I started the container with:

docker run -d --name test -p 22 myImage

Then I go inside the container with:

docker exec -it test /bin/bash

In the container, exec the command:

pm2 list

And it stuck here:

enter image description here

P.s.: My application works well in the Docker container, if I add CMD pm2 start app.js in the Dockerfile.

2 Answers 2

6

If your dockerfile CMD is a pm2 command, you have you include --no-daemon arg option so pm2 runs in the foreground and so your docker container continues to run.

An example Dockerfile CMD:

CMD ["pm2", "start", "app.js", "--no-daemon"]

Otherwise, without --no-daemon, pm2 launches as a background process and docker thinks the execution of the pm2 command is done running and stops.

See https://github.com/Unitech/PM2/issues/259

2
CMD ["pm2-docker", "pm2.yaml"]

This is the new approach.

Please do not use previous approaches.

2
  • Why is this the new way? Doesn't this just create a duplicate of the docker-compose.yml file? Commented Nov 3, 2017 at 21:51
  • This is the right way using pm2 and docker. This way each single container runs one thread on every core and load balances between cores. Alternatively, you can just use docker only and have one container runnning on each core.
    – danday74
    Commented Nov 6, 2017 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.