Logrotate - nginx日志不在docker容器内旋转

daz*_*ito 16 logrotate nginx docker dockerfile docker-compose

我有一个运行nginx的/var/log/nginx docker 容器,它正在将日志写入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/
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

以下是:/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
}
Run Code Online (Sandbox Code Playgroud)

/etc/cron.daily/logrotate的内容

#!/bin/sh

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf
Run Code Online (Sandbox Code Playgroud)

/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
Run Code Online (Sandbox Code Playgroud)

有人能指出我正确的方向为什么nginx日志不会被logrotate自动旋转?

编辑

我可以将这个问题的根本原因追溯到没有在容器上运行的cron服务.一种可能的解决方案是找到一种方法使容器同时运行nginx和cron服务.

daz*_*ito 12

正如在我的问题编辑中所说,问题是CMDnginx:1.11仅开始这个nginx过程.解决方法是将以下命令放在我的Dockerfile上

CMD service cron start && nginx -g 'daemon off;'
Run Code Online (Sandbox Code Playgroud)

这将启动nginx作为nginx:1.11启动它以及启动cron服务.

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/

# Start nginx and cron as a service
CMD service cron start && nginx -g 'daemon off;'
Run Code Online (Sandbox Code Playgroud)


tou*_*one 5

我从此链接中找到了一种方法,其核心思想是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
}
Run Code Online (Sandbox Code Playgroud)

  • 更好的是,在 postrotate 中执行此操作: `if test $(docker ps -q --filter "name=nginx"); 然后 docker exec nginx sh -c 'kill -USR1 $(cat /var/run/nginx.pid)'; 菲` (2认同)