我有一个运行 nginx 的 docker 容器,它正在将日志写入/var/log/nginx
Logrotate 安装在 docker 容器中,并且 nginx 的 logrotate 配置文件已正确设置。尽管如此,logrotate 仍不会自动旋转日志。手动强制日志旋转通过以下方式旋转日志logrotate -f /path/to/conf-file
按预期工作。
我的结论是,有些东西没有触发 cron ,但我找不到原因。
这是Dockerfile对于运行 nginx 的 docker 容器:
FROM nginx:1.11
# Remove sym links from nginx image
RUN rm /var/log/nginx/access.log
RUN rm /var/log/nginx/error.log
# Install logrotate
RUN apt-get update && apt-get -y install logrotate
# Copy MyApp nginx config
COPY config/nginx.conf /etc/nginx/nginx.conf
#Copy logrotate nginx configuration
COPY config/logrotate.d/nginx /etc/logrotate.d/
And the docker 撰写 file:
version: '2'
services:
nginx:
build: ./build
restart: always
ports:
- "80:80"
- "443:443"
volumes:
- ./auth:/etc/nginx/auth
- ./certs:/etc/nginx/certs
- ./conf:/etc/nginx/conf
- ./sites-enabled:/etc/nginx/sites-enabled
- ./web:/etc/nginx/web
- nginx_logs:/var/log/nginx
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "1"
volumes:
nginx_logs:
networks:
default:
external:
name: my-network
以下是内容:/etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
dateext
missingok
rotate 30
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
postrotate
[ -s /run/nginx.pid ] && kill -USR1 `cat /run/nginx.pid`
endscript
}
内容/etc/cron.daily/logrotate
#!/bin/sh
test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf
内容/etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# uncomment this if you want your log files compressed
#compress
# packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
missingok
monthly
create 0664 root utmp
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0660 root utmp
rotate 1
}
# system-specific logs may be configured here
有人可以指出为什么 nginx 日志没有被 logrotate 自动旋转的正确方向吗?
EDIT
我可以追踪这个问题的根本原因是 cron 服务没有在容器上运行。一个可能的解决方案是想办法让容器同时运行nginx和cron服务。