如何在不退出的情况下运行docker images(nodejs server)

时间:2016-06-06 15:23:54

标签: node.js docker dockerfile pm2 docker-container

创建的docker镜像将运行perl脚本,该脚本使用pm2命令在内部触发节点服务器。

使用dockerfile

创建的图像
FROM rm/node:4.0
EXPOSE 3000
EXPOSE 3030
EXPOSE 7030

WORKDIR /Reader_Manager/SISPlatform
CMD perl build_scripts/devdeploy.pl

当我使用下面的命令在后台运行图像时,它正在执行脚本。但在脚本完成后退出。

[dkanagaraj@localhost docker_test]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
rm/node             8.0                 716f9b91b9f2        23 minutes ago      1.376 GB
rm/node             7.0                 805fced7c1c8        3 hours ago         1.376 GB
rm/node             6.0                 4b0746c90363        3 hours ago         1.376 GB
rm/node             5.0                 912588196f44        7 hours ago         1.376 GB
rm/node             4.0                 22d40a764333        3 days ago          1.376 GB
rm/node             3.0                 dc344c502819        3 days ago          999.1 MB
rm/node             2.0                 bee03055f04c        3 days ago          824.7 MB
rm/node             1.0                 b9fa60c9f544        3 days ago          488.8 MB
docker.io/centos    latest              a65193109361        3 days ago          196.7 MB

[dkanagaraj@localhost docker_test]$ docker run -d rm/node:8.0
a2d1780ce3dcf102fd2a0eb4d6632f6934f8c0a5362e0312b117e8da77c1c242
Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning.

[dkanagaraj@localhost docker_test]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                          NAMES
a2d1780ce3dc        rm/node:8.0         "/bin/sh -c 'perl bui"   8 seconds ago       Up 7 seconds        3000/tcp, 3030/tcp, 7030/tcp   goofy_brattain

您可以看到上面它正在运行并暴露给3个端口。但它在perl脚本结束后退出。

[dkanagaraj@localhost docker_test]$ docker ps -a |head -2
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
a2d1780ce3dc        rm/node:8.0         "/bin/sh -c 'perl bui"   9 minutes ago       Exited (0) 8 minutes ago                        goofy_brattain

以下是容器日志的输出

[dkanagaraj@localhost docker_test]$ docker logs a2d1780ce3dc |tail -25
[PM2][WARN] No process found
npm WARN package.json Dependency 'chai' exists in both dependencies and devDependencies, using 'chai@^3.5.0' from dependencies
npm WARN package.json Dependency 'nodemon' exists in both dependencies and devDependencies, using 'nodemon@^1.8.1' from dependencies
npm WARN cannot run in wd sis-helpdesk@1.0.0 node postinstall.js (wd=/Reader_Manager/SISPlatform/Auth)
npm WARN package.json rm@1.0.0 No description
npm WARN package.json rm@1.0.0 No repository field.
npm WARN package.json rm@1.0.0 No README data
npm WARN package.json Dependency 'underscore' exists in both dependencies and devDependencies, using 'underscore@^1.8.3' from dependencies
npm WARN package.json rm@1.0.0 No description
npm WARN package.json rm@1.0.0 No repository field.
npm WARN package.json rm@1.0.0 No README data
┌─────────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────────────┬──────────┐
│ App name    │ id │ mode │ pid │ status │ restart │ uptime │ memory      │ watching │
├─────────────┼────┼──────┼─────┼────────┼─────────┼────────┼─────────────┼──────────┤
│ AuthServer  │ 0  │ fork │ 74  │ online │ 0       │ 2s     │ 63.191 MB   │ disabled │
│ SISRMServer │ 1  │ fork │ 107 │ online │ 0       │ 1s     │ 63.355 MB   │ disabled │
│ SEOSServer  │ 2  │ fork │ 138 │ online │ 0       │ 0s     │ 13.957 MB   │ disabled │
└─────────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app


        [Output] - Successfully executed 'npm run-script dev-server-linux' inside '/Reader_Manager/SISPlatform/SEOS-TSMWrapper'

        [Output] - Current working directory is /Reader_Manager/SISPlatform

        [Output] - Checking the errors in the logs '/Reader_Manager/SISPlatform/logs/pm2/err'


        [Output] - No error in the log '/Reader_Manager/SISPlatform/logs/pm2/err/auth_error-0.log'


        [Output] - No error in the log '/Reader_Manager/SISPlatform/logs/pm2/err/seos_error-2.log'


        [Output] - No error in the log '/Reader_Manager/SISPlatform/logs/pm2/err/sisrm_error-1.log'

2 个答案:

答案 0 :(得分:1)

问题是你的入口点在它完成时退出,即使后台运行守护进程也是如此。例如,如果您创建一个启动perl脚本的bash脚本,然后启动某个日志的尾部-f(您的节点日志或应用程序日志)并将其用作入口点,您将看到您的容器保持运行状态。

答案 1 :(得分:0)

谢谢大家的回答。

在dockerfile的CMD行中添加pm2日志(运行守护进程来检查应用程序的状态)修复了这个问题。

CMD perl build_scripts/devdeploy.pl; pm2 logs