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

2024-05-17

我有一个运行 nginx 的 docker 容器,它正在将日志写入/var/log/nginxLogrotate 安装在 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服务。


正如我的问题的编辑所述,问题是CMD from nginx:1.11才刚刚开始nginx过程。解决方法是将以下命令放在我的 Dockerfile 上

CMD service cron start && nginx -g 'daemon off;'

这将启动 nginxnginx: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;'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Logrotate - nginx 日志不在 docker 容器内旋转 的相关文章

随机推荐

  • Azure 逻辑应用 - 从 Blob 事件获取 Blob 内容

    当通过 http 创建 blob 时 我的逻辑应用程序会收到 blob 事件 我使用事件网格订阅 当发生 Blob Created 事件时 它会通过 Webhook 触发逻辑应用程序 http 触发器收到的典型 blob 事件如下所示 to
  • 将多个文件从每个可能的 EOL 转换为 CRLF

    我的文件包含所有可以想象到的 EOL 我想一次性将它们标准化 而不是像我们谈论几千个那样一个接一个地进行 我知道如何手动完成它们 所以请不要解释 我认为所有可能的情况是 从最常见到最不常见 CRLF LF CR CRLF CRCR CRLF
  • JQGrid - 在编辑表单中显示附加列

    我有一个 JQGrid 表 有 30 多个列 我认为这些列的内联编辑对用户来说并不友好 所以我想在网格模式下显示几列 并仅当用户打开该行的编辑表单时显示所有列 这可能吗 在教程中找不到这个 先感谢您 如果你想show并且不要编辑列 然后使用
  • Pandas 数据帧中任意两连续行之间差异的平均值

    我有一个数据框 name date quantity A 2016 12 02 20 A 2016 12 04 5 A 2016 11 30 10 B 2016 11 30 10 我想做的是计算 对于任何一对连续的名称的日期 按时间顺序连续
  • AVCaptureDevice 找不到任何设备

    这行代码是我今天遇到的问题 macOS 应用程序 NSArray devices AVCaptureDevice devicesWithMediaType AVMediaTypeVideo 我更新Xcode后 系统总是让我空着devices
  • 我正在尝试向我的 vue.js 项目添加背景图像

    我想添加覆盖整个页面的背景图像 然而现在看起来是这样的 我希望它跨越整个网页 在 vue js 中这将如何完成 我还想要一个动画工具栏 以便当页面不滚动时工具栏是透明的并呈现背景图像的外观 当它滚动时 工具栏将具有当前的蓝色 这是我的小提琴
  • iOS:滚动视图仅在键盘出现后才起作用

    我制作了滚动视图 其中有很多文本字段 我添加了更新的 TPKeyBoardAvoidingScrollView 并将其添加到滚动视图的文件所有者中 我在 h 文件中添加了插座 在 m 文件中综合并添加了行 self view addSubv
  • TOMCAT 6 中的 PHP - 异常

    我一直在努力融入PHP in APACHE TOMCAT 6依照指示second answer为了QUESTION https stackoverflow com questions 779246 run a php app using t
  • 仅打印“docker-container ls -la”输出中的“Names”列

    发出时docker container ls la命令 输出如下所示 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a67f0c2b1769 busybox tail f dev
  • GOMAXPROCS 默认值是多少?

    不设置同名环境变量时是否保证GOMAXPROCS设置为1 此代码显示的值 package main import runtime fmt func getGOMAXPROCS int return runtime GOMAXPROCS 0
  • 在 Google 表格中创建每周/每月/每年折线图,以汇总同一周/月/年范围内的金额

    我想创建一个代表两列数据的折线图 F 输入日期 和 H 美元金额 X 轴应为日期 Y 轴应为美元金额 问题是我希望折线图上的日期代表给定周 月或年输入的所有金额的总和 这张照片是 YouTube 分析的 它创建了一个与我想在工作表中创建的类
  • jQuery Clockpicker afterHourSelect

    早上好 这是我在 stackoverflow 上的第一个问题 所以请怜悯 我正在使用jQuery 时钟选择器插件 https weareoutman github io clockpicker 对于某些形式 对于我的一些用户来说 时钟选择器
  • 如何向 UIView 添加大小调整手柄?

    我试图根据用户请求在运行时动态创建视图 UIImageView 和 UITextView 然后允许用户移动它们并调整它们的大小 除了调整大小之外 我的一切都工作得很好 我尝试使用捏合手势识别器 但发现它对于我想要的东西来说太笨拙了 因此 我
  • 在 .Net 应用程序中使用 Active Directory Web 服务

    我正在尝试构建一个 Net 应用程序来询问 Active Directory 编辑 我需要使用 Web 服务来执行此操作 因为我将使用需要使用 Web 服务的第三方工作流工具从 Sharepoint 工作流与 AD 进行通信 根据我的研究
  • 如何定义 nullptr 以支持 C++03 和 C++11? [复制]

    这个问题在这里已经有答案了 可能的重复 将 nullptr 向后移植 到 C C 0x 之前的程序 https stackoverflow com questions 8747005 backporting nullptr to c pre
  • 我可以在 firebase android 中加载另一个用户个人资料图像吗?

    如果我有其他用户的电子邮件但我以其他用户身份登录 我是否可以加载其他用户的个人资料图像 如果您使用 Firebase Storage 那么从技术上讲是的 它只是一个您可以从中检索任何文件的文件系统 如果不伪造您的应用程序 获取 api 密钥
  • 文件夹.文件的相对路径

    我有一个 Excel 文件 在同一文件夹中还有一个包含我想要包含的 CSV 文件的文件夹 使用 来自文件夹 查询 第一步将给出以下查询 Folder Files D OneDrive Documents Health Concept2 现在
  • 获取所有查询字符串对并初始化字典的最佳方法

    我想将所有键 值对存储在我的查询字符串中 www example com a 2 b 3 c 34 进入字典 有没有一种快速的方法可以做到这一点 而无需手动循环所有项目 Try HttpUtility ParseQueryString 它给
  • 应用程序实例是否始终在任何活动之前创建?

    在 Android 中 您可以通过扩展 Application 类并在 Manifest 中声明名称来提供您自己的 Application 类实现 我的问题是 这个实现是否总是在初始活动之前创建 或者活动可以在应用程序实例有时间创建之前启动
  • Logrotate - nginx 日志不在 docker 容器内旋转

    我有一个运行 nginx 的 docker 容器 它正在将日志写入 var log nginxLogrotate 安装在 docker 容器中 并且 nginx 的 logrotate 配置文件已正确设置 尽管如此 logrotate 仍不