简体   繁体   中英

dockerizing a nodejs application - should pm2 ls work when I first start the container

I have the following express / node application:

FROM node:12
WORKDIR /usr/share/jtapp
COPY  package*.json ./
RUN apt-get update
RUN apt-get install -y net-tools
RUN npm install
RUN npm install pm2@latest -g
#RUN npm ci --only=production
COPY . .
EXPOSE 3000
RUN pm2 start server.js

It seems to build fine. But when I start the image and attach via bash, there are no pm2 processes running.

Here are the results of the build:

Sending build context to Docker daemon  319.5kB                                                                                                                                                                                              Step 1/10 : FROM node:12                                                                                                                                                                                                                      ---> cfcf3e70099d                                                                                                                                                                                                                           Step 2/10 : WORKDIR /usr/share/jtapp                                                                                                                                                                                                     ---> Using cache                                                                                                                                                                                                                             ---> 2c2e2e1e92f2                                                                                                                                                                                                                           Step 3/10 : COPY  package*.json ./                                                                                                                                                                                                            ---> Using cache                                                                                                                                                                                                                             ---> 57829fd5e9d7                                                                                                                                                                                                                           Step 4/10 : RUN apt-get update                                                                                                                                                                                                                ---> Using cache                                                                                                                                                                                                                             ---> c0bbfed43ca8                                                                                                                                                                                                                           Step 5/10 : RUN apt-get install -y net-tools                                                                                                                                                                                                  ---> Using cache                                                                                                                                                                                                                             ---> 0b34759d8298                                                                                                                                                                                                                           Step 6/10 : RUN npm install                                                                                                                                                                                                                   ---> Using cache                                                                                                                                                                                                                             ---> 6abc42a9f8a3                                                                                                                                                                                                                           Step 7/10 : RUN npm install pm2@latest -g                                                                                                                                                                                                     ---> Running in 2fafacd302bf                                                                                                                                                                                                                /usr/local/bin/pm2 -> /usr/local/lib/node_modules/pm2/bin/pm2                                                                                                                                                                                /usr/local/bin/pm2-dev -> /usr/local/lib/node_modules/pm2/bin/pm2-dev                                                                                                                                                                        /usr/local/bin/pm2-docker -> /usr/local/lib/node_modules/pm2/bin/pm2-docker                                                                                                                                                                  /usr/local/bin/pm2-runtime -> /usr/local/lib/node_modules/pm2/bin/pm2-runtime               
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.1.2 (node_modules/pm2/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ pm2@4.4.1
added 185 packages from 191 contributors in 10.165s
Removing intermediate container 2fafacd302bf
 ---> 11d15b993ca2
Step 8/10 : COPY . .
 ---> efab8283bbd9
Step 9/10 : EXPOSE 3000
 ---> Running in aae343a75727
Removing intermediate container aae343a75727
 ---> b97825863317
Step 10/10 : RUN pm2 start server.js
 ---> Running in f0d14c61a3a6
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /usr/share/jtapp/server.js in fork_mode (1 instance)
[PM2] Done.
┌─────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼───────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ server    │ default     │ 1.0.0   │ fork    │ 28       │ 0s     │ 0    │ online    │ 0%       │ 28.2mb   │ root     │ disabled │
└─────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
Removing intermediate container f0d14c61a3a6
 ---> 859f8479f3a6
Successfully built 859f8479f3a6

Then this is how I start the container and attach to it:

lab:/var/lib/git/jtapp# docker run -d -it --name jtapp -p 3000:3000 jtapp bash
d62927716ab593376c4e38f7d4d05007223ce2adfcb4997e722b81ad7260f114
lab:/var/lib/git/jtapp # docker exec -it jtapp  bash
root@d62927716ab5:/usr/share/jtapp # pm2
usage: pm2 [options] <command>

pm2 -h, --help             all available commands and options
pm2 examples               display pm2 usage examples
pm2 <command> -h           help on a specific command

Access pm2 files in ~/.pm2
root@d62927716ab5:/usr/share/jtapp #

But I'm able to start everything manually once i'm inside the container, like this:

root@d62927716ab5:/usr/share/jtapp# pm2 start server.js
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /usr/share/jtapp/server.js in fork_mode (1 instance)
[PM2] Done.
┌─────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼───────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ server    │ default     │ 1.0.0   │ fork    │ 37       │ 0s     │ 0    │ online    │ 0%       │ 28.4mb   │ root     │ disabled │
└─────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

EDIT 1

Changed:

 RUN pm2 start server.js 

to

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

And then tried to rebuild / start a container. when i do a "pm2 ls" it still comes back empty.

root@a8b7eb21d307:/usr/share/jtapp# pm2 ls
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘

EDIT 2

When i remove the "-d" option in my run command:

lab:/var/lib/git/jtappd# docker run  -it --name jtapp -p 3000:3000 jtapp  bash
root@3cf4855372ec:/usr/share/jtapp# pm2 ls
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
┌─────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
└─────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
root@3cf4855372ec:/usr/share/jtapp#

EDIT 3

So the Dockerfile looks like this now:

FROM node:12
WORKDIR /usr/share/jtapp
COPY  package*.json ./
RUN apt-get update
RUN apt-get install -y net-tools
RUN npm install
RUN npm install pm2@latest -g
COPY . .
EXPOSE 3000
CMD ["npm", "run", "start:pm2"]

ecosystem.config.js looks like:

lab:/var/lib/git/jtapp# cat ecosystem.config.js
module.exports = {
  apps: [{
    name: 'jtapp',
    script: './server.js', //entrypoint
    instances: 1,
    autorestart: true, // pm2 to restart if this app fails/stops
    max_memory_restart: '1G'
  }]
}

and package.json's script section:

 1 {
 2   "name": "nodeserver",
 3   "version": "1.0.0",
 4   "description": "simple node server",
 5   "main": "index.js",
 6   "scripts": {
 7     "test": "echo \"Error: no test specified\" && exit 1",
 8     "start": "nodemon server.js",
 9     "start:pm2": "pm2 start ecosystem.config.js --no-daemon"
10   },

The Dockerfile builds with no errors. But when I start the container... server.js is not running. pm2 is installed.

But there are no problems starting it manually using the ecosystem file once I'm logged into the container as you can see below:

root@d51117d757a3:/usr/share/jtapp# pm2 start ecosystem.config.js
[PM2][WARN] Applications jtapp not running, starting...
[PM2] App [jtapp ] launched (1 instances)
┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name     │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ jtapp    │ default     │ 1.0.0   │ cluster │ 36       │ 0s     │ 0    │ online    │ 0%       │ 32.6mb   │ root     │ disabled │
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

The best approach would be using ecosystem.config.js . Put this file in your project's root dir:


module.exports = {
  apps: [{
    name: 'app',
    script: 'src/index.js', // Your entry point
    instances: 1,
    autorestart: true, // THIS is the important part, this will tell PM2 to restart your app if it falls over
    max_memory_restart: '1G'
  }]
}

then install pm2 as npm module in your project and add this script to your package.json

...
scripts: {
...
  "start:pm2":"pm2 start ecosystem.config.js --no-daemon"
}

and finally use this CMD in your dockerfile

CMD [ "npm", "run", "start:pm2" ]

EDIT 1

you can find a sample project here with original dockerfile:

https://github.com/golkhandani/pm2_docker/tree/master

在此处输入图像描述

EDIT 2

I have just tested the repo with another Manjaro linux and here is commands

git clone https://github.com/golkhandani/pm2_docker.git

docker build pm2_docker/ --tag pm2docker:test

docker run -p 3000:3000 -t pm2docker:test

the -p 3000:3000 arg should be added to access locally to your server.

these are additional screenshots that show command and results:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM