Docker Community Forums

Share and learn in the Docker community.

Docker exited with code 0 with nodejs, adonisjs and pm2

I have Dockerfile that installs pm2 and successfully creating a daemon but exits after. Not sure why.

Here’s my Dockerfile

FROM node:8.15-jessie

WORKDIR /app

COPY . .

RUN mv .env.example .env && \
    npm i -g @adonisjs/cli && \
    npm i -g pm2 && \
    npm i --quiet

CMD pm2 start server.js

and docker-compose.yml file

version: '3'
services:
  web:
    container_name: nbi-ws
    build: .
    volumes:
      - ./:/app
      - /app/node_modules
    ports:
      - "3331:3331"
    tty: true

doing docker logs shows

[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /app/server.js in fork_mode (1 instance)
[PM2] Done.
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ App name β”‚ id β”‚ version β”‚ mode β”‚ pid β”‚ status β”‚ restart β”‚ uptime β”‚ cpu β”‚ mem       β”‚ user β”‚ watching β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ nbi-ws   β”‚ 0  β”‚ 4.1.0   β”‚ fork β”‚ 23  β”‚ online β”‚ 0       β”‚ 0s     β”‚ 0%  β”‚ 27.8 MB   β”‚ root β”‚ disabled β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
 Use `pm2 show <id|name>` to get more details about an app
exited with code 0

Exactly because you start a daemon. A container needs a foreground process to stay alive. You find an example in the docs for pm2 that shows how to use it in a container and more informations if you follow the link to pm2.io.

2 Likes

@tekki I just saw it just now. :slight_smile: However, there’s another issue I am facing:

I think it is already running in port 3331

nbi-ws    | 2019-03-05T09:53:37: PM2 log: Launching in no daemon mode
nbi-ws    | 2019-03-05T09:53:37: PM2 log: App [server:0] starting in -fork mode-
nbi-ws    | 2019-03-05T09:53:37: PM2 log: App [server:0] online
nbi-ws    | info: serving app on http://127.0.0.1:3331

but it seems not accessible on the host with this config:

  web:
    container_name: nbi-ws
    build: .
    volumes:
      - ./:/app
      - /app/node_modules
    ports:
      - "3331:3331"
    tty: true

Just a guess: your application listens to 127.0.0.1 only instead of 0.0.0.0.

@tekki maybe. How do I rectify it? I added EXPOSED :port in the Dockerfile but still not doing it.

FROM node:8.15-jessie

WORKDIR /app

COPY . .

RUN mv .env.example .env && \
    npm i -g @adonisjs/cli && \
    npm i -g pm2 && \
    npm i --quiet --production

EXPOSE 3331

CMD ["pm2-runtime", "start", "server.js"]

This is a default setting in nodejs and is not related to Docker. Somewhere there is a host variable that has to be changed to 0.0.0.0. I’m sorry that I can’t tell you where to find it, I just remember I’ve seen this problem before.

1 Like

It is found in the .env of the project! I’m ecstatic! Thank you @tekki I owe you a beer, cheers mate!