Docker Apache2 要做 logrotate 失败解法

在 Linux 跑 Docker 要对 Apache 的 Log 做 logrotate,会遇到 Apache 没有 reload 的问题

  • 注:会看到 logrotate 有执行,但是 Log 档持续写 access.log.1,若 Apache reload 成功,应该要写 access.log

Docker Apache2 要做 logrotate 失败解法

于 /etc/logrotate.d/apache2 可以看到此行:

  • invoke-rc.d apache2 reload 2>&1 | logger -t apache2.logrotate

执行看看 invoke-rc.d apache2 status 会出现下述错误:

  • invoke-rc.d: could not determine current runlevel
  • invoke-rc.d: policy-rc.d denied execution of status.
  • invoke-rc.d: emulating initscript action "status", returning "unknown"

一个比较简单的作法,可以把那行改成

  • apache2 restart 或 apache2 graceful

另外一个是查看 policy-rc.d,内容如下:

  • cat /usr/sbin/policy-rc.d
    #!/bin/sh  # For most Docker users, "apt-get install" only happens during "docker build", # where starting services doesn't work and often fails in humorous ways. This # prevents those failures by stopping the services from attempting to start.  exit 101

这里面提到 exit 101 的主因是因为 docker build 的时候,有 apt-get install,怕 service 会自动启动,造成失败

既然都已经在执行,已经过了 Dockerfile 的阶段 (或者说,policy-rc.d 的内容,在 Dockerfile 的 apt install 之后,就可以改回原始样貌了)

将 policy-rc.d 改回 (exit 101 → exit 0)

#!/bin/sh  # For most Docker users, "apt-get install" only happens during "docker build", # where starting services doesn't work and often fails in humorous ways. This # prevents those failures by stopping the services from attempting to start.  exit 0
  • 注:sed -i 's/exit 101/exit 0/g' /usr/sbin/policy-rc.d

这样子 Apache 的 Logrotate 就可以正常执行囉~

分享到Facebook