logrotate
logrotate 是 linux 中用于日志清理的工具,详见 man 文档。推荐结合 cron 自动清理服务日志。
使用例子
/etc/logrotate.d/napi
文件,清理配置:
/var/log/napi/*.log {
rotate 3
compress
size 100M
daily
missingok
notifempty
copytruncate
dateformat -%Y%m%d%H%M
dateext
}
Dockerfile 文件,打包配置:
FROM alpine:latest
RUN set -eux; \
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories; \
apk --no-cache add tini tzdata procps curl ca-certificates bash neovim logrotate; \
apk --no-cache upgrade --available; \
mv /etc/periodic/daily/logrotate /etc/periodic/15min/logrotate; \
mkdir -p /var/log/napi/;
ENV TZ=Asia/Shanghai EDITOR=nvim PAGER=less
...
COPY ./configs/logrotate /etc/logrotate.d/napi
COPY ./scripts/bash/entrypoint.sh /data/service/napi/entrypoint.sh
ENTRYPOINT ["tini", "-g", "--"]
CMD ["/data/service/napi/entrypoint.sh"]
entrypoint.sh 文件,启动配置:
#!/usr/bin/env bash
# start crond in background (by default) and set log level to 7
crond -d 7 2>&1
exec /data/service/napi/bin/cnapi