我有一个运行nginx的docker容器,它将日志写入/var/log/nginx
Logrotate安装在docker容器中,nginx的logrotate配置文件已正确设置。但是,logrotate不会自动旋转日志。手动强制日志旋转以通过logrotate -f /path/to/conf-file
旋转日志按预期工作。
我的结论是,某些东西并没有触发cron开火,但我找不到原因。
这是运行nginx的docker容器的 Dockerfile :
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/
docker-compose 文件:
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
有人能指出我为什么nrotx日志没有被logrotate自动轮换的正确方向?
修改
我可以将这个问题的根本原因跟踪到没有在容器上运行的cron服务。一种可能的解决方案是找到一种方法使容器同时运行nginx和cron服务。
答案 0 :(得分:11)
正如关于我的问题的修改所述,问题是来自CMD
的{{1}}仅启动nginx:1.11
进程。解决方法是将以下命令放在我的Dockerfile
nginx
这将启动nginx,因为CMD service cron start && nginx -g 'daemon off;'
启动它,并启动cron服务。
Dockerfile看起来像:
nginx:1.11
答案 1 :(得分:3)
我从this link找到了一个方法,其核心思想是在外部使用logrotate
,并且conf如下:
$ sudo vi /etc/logrotate.d/test
/home/test/logs/*log {
rotate 90
missingok
ifempty
sharedscripts
compress
postrotate
docker exec -it nginx-test systemctl reload nginx > /dev/null 2>/dev/null || true
endscript
}
答案 2 :(得分:-1)
docker exec --user root `docker container ls --filter "name=nginx" --format "{{.Names}}"` nginx -s reopen