在 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 就可以正常執行囉~