<p>You should keep at least one service running in foreground to keep the container running, you can do that using <code>ENTRYPOINT</code> or <code>CMD</code> or both. For more info check <a href="https://www.ctl.io/developers/blog/post/dockerfile-entrypoint-vs-cmd/" rel="nofollow noreferrer">Dockerfile: ENTRYPOINT vs CMD</a>.</p>

<p>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 <a href="https://hub.docker.com/r/keymetrics/pm2-docker-alpine/" rel="nofollow noreferrer">the keymetrics/pm2-docker-alpine project</a>, for example.</p>

CC BY-SA 3.0
2017-10-12T11:29:06.523Z
2017-10-12T18:10:59.173Z
2017-10-12T18:10:59.173Z
Wissam Roujoulah
<p>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</p>

<pre><code>  nodejs-enduser:
    image: your-image-name
    restart: always
    volumes:
      - /pathA:/pathB  
</code></pre>

<p>pathA is full path to some logging dir on host where container is launched from</p>

<p>pathB is path processes write to from inside container</p>

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

<p>Alternative approach is to spin up an empty container then once running you log into it using <code>docker exec -ti container-id bash</code>  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</p>

<pre><code>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"]
</code></pre>

<p>now build above Dockerfile</p>

<pre><code>docker build --tag stens_ubuntu .
</code></pre>

<p>and run</p>

<pre><code>docker run -d  stens_ubuntu  sleep infinity
</code></pre>

<p>now log into it</p>

<pre><code>docker exec -it $( docker ps | grep stens_ubuntu | cut -d' ' -f1 )  bash
</code></pre>

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

CC BY-SA 3.0
2017-10-13T02:57:44.643Z
Thanks, I have tried to log in to the container but failed, because the container only run less than a second.
spacetrack
CC BY-SA 3.0
2017-10-13T19:06:51.507Z
@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.
Xiong Chiamiov
CC BY-SA 3.0
2017-10-12T12:40:08.020Z
2017-10-12T13:02:44.267Z
2017-10-12T13:02:44.267Z
Scott Stensland
<p>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. </p>

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

CC BY-SA 3.0
2017-10-13T02:58:40.463Z
Thank you I have tried using CMD but it do nothing
spacetrack
CC BY-SA 3.0
2017-10-14T20:12:09.507Z
Can you upload your Dockerfile? My feeling is that pm2 will daemonise itself. Maybe you should consider using [pm2-docker](http://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/)
boca
CC BY-SA 3.0
2017-10-15T14:01:26.037Z
you can see my question, I have upload my dockerfile there
spacetrack
CC BY-SA 3.0
2017-10-12T17:09:21.970Z
2017-10-12T18:11:54.943Z
2017-10-12T18:11:54.943Z
boca
<p>I am using docker compose to build two containers, one is nodejs with pm2 and another is nginx.</p>
<p>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 <code>docker ps -a</code>:</p>
<p><a href="https://i.sstatic.net/kgHub.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kgHub.png" alt="docker ps -a" /></a></p>
<p>I have used <code>docker attach 'ID'</code> but it still restarted.</p>
<p>Using <code>docker logs</code> shows nothing, but jenkins says that pm2 was started successfully:</p>
<p><a href="https://i.sstatic.net/yUpyb.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/yUpyb.png" alt="jenkins output log" /></a></p>
<p>This is the content of my Dockerfile:</p>
<pre><code>FROM node:8    
MAINTAINER test    
RUN npm install -g pm2

WORKDIR /var/www/marketing    

EXPOSE 80    

RUN npm install    
RUN pm2 start npm -- stage
</code></pre>
<p>If any of you could help me, I will update this question if you guys have any request.
Thank you</p>

CC BY-SA 3.0
2017-10-12T06:49:17.730Z
Shouldn't the last line be a CMD and not a RUN ?
Tensibai
CC BY-SA 3.0
2017-10-13T02:53:38.120Z
@Tensibai I have used CMD, but still restarted
spacetrack
<docker><node.js>
Docker Container always stopped and started
spacetrack
# Docker Container always stopped and started

(Asked by: username_0 on 2017-10-12)

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:

I have used docker attach 'ID' but it still restarted.
Using docker logs shows nothing, but jenkins says that pm2 was started successfully:

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

**Tags:** docker, node.js

> **Comments:**
> **username_1**: Shouldn't the last line be a CMD and not a RUN ?  
> **username_0**: @username_1 I have used CMD, but still restarted  

---

## 3 answers

### Answer by username_2 on 2017-10-12. Score: 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](https://www.ctl.io/developers/blog/post/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](https://hub.docker.com/r/keymetrics/pm2-docker-alpine/), for example.


---

### Answer by username_3 on 2017-10-12. Score: 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

> **Comments:**
> **username_0**: Thanks, I have tried to log in to the container but failed, because the container only run less than a second.  
> **username_4**: @username_0 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.  

---

### Answer by username_5 on 2017-10-12. Score: 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.

> **Comments:**
> **username_0**: Thank you I have tried using CMD but it do nothing  
> **username_5**: Can you upload your Dockerfile? My feeling is that pm2 will daemonise itself. Maybe you should consider using [pm2-docker](http://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/)  
> **username_0**: you can see my question, I have upload my dockerfile there  

---

