Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 停靠nodejs应用程序-当我第一次启动容器时,pm2 ls是否应该工作_Node.js_Docker - Fatal编程技术网

Node.js 停靠nodejs应用程序-当我第一次启动容器时,pm2 ls是否应该工作

Node.js 停靠nodejs应用程序-当我第一次启动容器时,pm2 ls是否应该工作,node.js,docker,Node.js,Docker,我有以下express/node应用程序: 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 serv

我有以下express/node应用程序:

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
它似乎建得很好。但是当我启动映像并通过bash连接时,没有pm2进程在运行

以下是构建的结果:

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
然后,以下是启动容器并将其连接到容器的方式:

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 #
编辑1

更改:

 RUN pm2 start server.js 

然后尝试重建/启动容器。 当我做“pm2 ls”时,它还是空的

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   │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
编辑2

删除run命令中的“-d”选项时:

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#
编辑3

Dockerfile现在看起来像这样:

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看起来像:

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'
  }]
}
和package.json的脚本部分:

 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   },
Dockerfile生成时没有错误。但当我启动容器时。。。server.js未运行。安装了pm2

但在我登录到容器后,使用生态系统文件手动启动它不会有任何问题,如下所示:

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 │
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

最好的方法是使用
ecosystem.config.js
。 将此文件放入项目的根目录:


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'
  }]
}
然后将pm2作为npm模块安装到您的项目中,并将此脚本添加到您的
包.json

...
scripts: {
...
  "start:pm2":"pm2 start ecosystem.config.js --no-daemon"
}
最后在dockerfile中使用此CMD

CMD [ "npm", "run", "start:pm2" ]
编辑1

您可以在此处找到带有原始dockerfile的示例项目:

编辑2

我刚刚用另一个
Manjaro
linux和 这是命令

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

docker build pm2_docker/ --tag pm2docker:test

docker run -p 3000:3000 -t pm2docker:test
应添加
-p 3000:3000
参数以在本地访问服务器

以下是显示命令和结果的附加屏幕截图:


实际上需要“路径”吗?我看不出有人在用它?我们将实施此功能,并查看它是否有效。谢谢。对不起,我会更新我的答案。实际上,它将用于设置脚本,但您可以自己创建脚本路径,所以我尝试了,但它仍然无法启动pm2。正在更新我的帖子,并提供详细信息。几分钟后查看“编辑3”。我用一个示例项目更新了我的答案。删除pm2的全局安装,并在docker运行时暴露端口,给我运行的服务器
CMD [ "npm", "run", "start:pm2" ]
git clone https://github.com/golkhandani/pm2_docker.git

docker build pm2_docker/ --tag pm2docker:test

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