方法一 :使用pm2-runtime可以
如何在docker里面同时启动多个pm2进程_祥仔先生的博客-CSDN博客_pm2-docker
方法二:pm2 启动的时候使用--no-daemon
如果在docker中需要记录pm2的日志到文件中,推荐使用下面的方法来启动服务
例如:
#!/bin/bash
pm2 start ./build/server.js --name app; pm2 start ./build/bin/crontab.js --name crontab --no-daemon
坑一: 首先我们来说一下坑,就是pm2-runtime默认是不记录日志到文件的。
commander.version(pkg.version)......option('--error ', 'error log file destination (default disabled)', '/dev/null').option('--output ', 'output log file destination (default disabled)', '/dev/null')....
所以在使用pm2-runtime启动项目的时候默认是不生成日志的。这个是需要注意的,需要自己指定。
坑二:如果我们在docker中打开了pm2的日志就需要考虑到,随着时间的推移日志的量会越来越大,可能会导致docker镜像的空间被占满。这里面就需要引入pm2-logrotate
1、安装pm2-logrotate的前提是pm2已经安装了
2、其次默认的日志一般会存放在/root/.pm2/logs下
DockerFile中可以添加如下的配置:
RUN npm i pm2 -g
RUN pm2 install pm2-logrotateRUN pm2 set pm2-logrotate:retain 15 && \pm2 set pm2-logrotate:max_size 300M && \pm2 set pm2-logrotate:compress true
max_size
(Defaults to 10M
): When a file size becomes higher than this value it will rotate it (its possible that the worker check the file after it actually pass the limit) . You can specify the unit at then end: 10G
, 10M
, 10K
retain
(Defaults to 30
file logs): This number is the number of rotated logs that are keep at any one time, it means that if you have retain = 7 you will have at most 7 rotated logs and your current one. compress
(Defaults to false
): Enable compression via gzip for all rotated logs dateFormat
(Defaults to YYYY-MM-DD_HH-mm-ss
) : Format of the data used the name the file of log rotateModule
(Defaults to true
) : Rotate the log of pm2's module like other apps workerInterval
(Defaults to 30
in secs) : You can control at which interval the worker is checking the log's size (minimum is 1
) 1
)单位为秒(控制模块检查log日志大小的循环时间,默认30s检查一次)rotateInterval
(Defaults to 0 0 * * *
everyday at midnight): This cron is used to a force rotate when executed. We are using node-schedule to schedule cron, so all valid cron for node-schedule is valid cron for this option. Cron style : * * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
复制代码
TZ
(Defaults to system time): This is the standard tz database timezone used to offset the log file saved. For instance, a value of Etc/GMT+1
, with an hourly log, will save a file at hour 14
GMT with hour 13
(GMT+1) in the log name. 下一篇:算法题 最大子数组和