4

I am using docker compose to build two containers, one is nodejs with pm2 and another is nginx.

The nginx one runs well but the nodejs with pm2 one keeps restarting: it runs for one second, then stops for ~10 seconds, then starts again and runs for one second, as you can see this is the output of docker ps -a:

docker ps -a

I have used docker attach 'ID' but it still restarted.

Using docker logs shows nothing, but jenkins says that pm2 was started successfully:

jenkins output log

This is the content of my Dockerfile:

FROM node:8    
MAINTAINER test    
RUN npm install -g pm2

WORKDIR /var/www/marketing    

EXPOSE 80    

RUN npm install    
RUN pm2 start npm -- stage

If any of you could help me, I will update this question if you guys have any request. Thank you

2
  • Shouldn't the last line be a CMD and not a RUN ?
    – Tensibai
    Oct 12, 2017 at 6:49
  • @Tensibai I have used CMD, but still restarted
    – spacetrack
    Oct 13, 2017 at 2:53

3 Answers 3

6

You should keep at least one service running in foreground to keep the container running, you can do that using ENTRYPOINT or CMD or both. For more info check Dockerfile: ENTRYPOINT vs CMD.

You can also use a docker image for pm2 from docker hub, that will save your time and you can check the Dockerfile for it to see how it is written. Check the keymetrics/pm2-docker-alpine project, for example.

4

To give visability to the error messages mount a volume to your container so after it crashes the logs are available ... problem otherwise is once crashed the logs go away ... for example this docker-compose.yaml excerpt is how to mount a volume

  nodejs-enduser:
    image: your-image-name
    restart: always
    volumes:
      - /pathA:/pathB  

pathA is full path to some logging dir on host where container is launched from

pathB is path processes write to from inside container

If you wrap your docker launch using something like supervisord then its config defines where standard out / stderr are written to ... that is /pathB ... then the app inside the container just writes to stdout / stderr

Alternative approach is to spin up an empty container then once running you log into it using docker exec -ti container-id bash Once inside you then manually issue your Dockerfile commands to install and execute the app ( nodejs ) where you can then see the errors as they happen ... here is the Dockerfile for this do nothing container

FROM ubuntu:16.04

ENV TERM linux
ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update
RUN apt-get install -y wget  curl   net-tools

# COPY .bashrc /root/
#  list your normal Dockerfile code COPY steps here 

CMD ["/bin/bash"]

now build above Dockerfile

docker build --tag stens_ubuntu .

and run

docker run -d  stens_ubuntu  sleep infinity

now log into it

docker exec -it $( docker ps | grep stens_ubuntu | cut -d' ' -f1 )  bash

here you are at the command prompt inside the running container where you now issue whichever commands you need to run your normal app as listed in your original Dockerfile ... beauty is now you are interactively seeing the error happen ... until it crashes however the messages will be shown

2
  • Thanks, I have tried to log in to the container but failed, because the container only run less than a second.
    – spacetrack
    Oct 13, 2017 at 2:57
  • @spacetrack The way you deal with that is by doing a Docker run with --entrypoint overridden to bash, and then you can use that shell to run the command and see what it does. Oct 13, 2017 at 19:06
2

Docker monitors the process started by ENTRYPOINT or CMD and since you don’t have such entry in your Dockerfile your container will terminate early in its lifecycle.

Also be aware that the processes that are started via ENTRYPOINT or CMD should run in foreground, not started as a daemon.

3
  • Thank you I have tried using CMD but it do nothing
    – spacetrack
    Oct 13, 2017 at 2:58
  • Can you upload your Dockerfile? My feeling is that pm2 will daemonise itself. Maybe you should consider using pm2-docker
    – boca
    Oct 14, 2017 at 20:12
  • you can see my question, I have upload my dockerfile there
    – spacetrack
    Oct 15, 2017 at 14:01

Your Answer

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

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