0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Dockerコンテナから出力されるログのローテート

Posted at

Dockerコンテナを「開発」用途ではなく「公開サービス」として使いたい場合、ログローテートが問題になってきます。

結論から言うとログローテートは、

ホストマシンの logrotate.d に任せた方が楽だし安全です

httpd や php-fpm 等、特殊な事をしていないコンテナなら docker exec [コンテナ名] httpd -k graceful や、docker exec [コンテナ名] kill -USR1 1

例えばですが squid など、entrypoint.sh 内で tail でファイルを掴まれてしまっているような特殊な環境の場合は、docker restart squid

を postrotate に入れると良いです。

検証した環境は Docker v28.4.0 です

docker compose up -d で起動したコンテナであっても、docker restart [コンテナ名] は何処からでも打てます。※dockerコマンドを打てる権限 (通常はroot権限) は必要です。

docker restart コンテナ名 は docker-compose.yml で depends_on が定義されていたり、healthcheck が定義されていても、特定のコンテナだけを再起動可能です。

下記は database コンテナに healthcheck を定義し、hogehogeコンテナでは depends_on + service_healthy で database コンテナを必要としている構成で、docker restart database を実行した結果です。

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
httpd:latest 26 minutes ago Up 26 minutes 0.0.0.0:80->80/tcp, [::]:80->80/tcp, 0.0.0.0:443->443/tcp, [::]:443->443/tcp httpd
秘密 26 minutes ago Up 26 minutes 80/tcp hogehoge
mariadb:latest "docker-entrypoint.s…" 26 minutes ago Up 7 seconds (healthy) 3306/tcp database

database コンテナだけが Up 7 seconds (healthy) となっているので、他のコンテナに影響を与えていないことが分かります。

ホストマシン側の logrotate.d/[xxx] 設定

postrotate の箇所だけを記載しますが、下記のような感じです。

httpdコンテナ等
    postrotate
        docker exec [コンテナ名] httpd -k graceful > /dev/null 2>&1 || true
    endscript
Squid等 entrypoint.sh が特殊なもの
    postrotate
        docker restart [コンテナ名] > /dev/null 2>&1 || true
    endscript

ホストマシン側でログローテートする最大の利点は、

Docker Hub の公式イメージに手を加えることなく、ログローテートできる、という点です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?