Help us understand the problem. What is going on with this article?

Dockerでlogを削除する、ログを溜めない方法

概要

こんにちは、はにおかさい(@haniokasai )です。
自分のサーバーで容量が足りなくなってしまい、dockerのログをどかそうと思いました。
ログを消す・溜めない方法を調べました。

環境

  • Ubuntu 18.04 on XFS
  • docker
    • overlayfs on XFS
    • 19.03.5

すでにあるログを削除

特定のコンテナのログを消す

コンテナ名(ID)を入力し、そのログパスを取得。そこに無を流し込めばログが消えます。

echo "" > $(docker inspect --format='{{.LogPath}}' コンテナ名orコンテナID)

全コンテナのログを消す

forでコンテナ名(ID)を回します。

#削除対象の確認
for CONTID in $(docker ps -qa); do echo $(docker inspect --format='{{.LogPath}}' $CONTID ); done
#実際の削除
for CONTID in $(docker ps -qa); do echo "" > $(docker inspect --format='{{.LogPath}}' $CONTID ); done

たまに、空白のコンテナidが入り込んで

bash: $(docker inspect --format='{{.LogPath}}' $CONTID ): ambiguous redirect

となりますが、無視して問題ありません(条件分けする理由もないだろう。)

ログ溜めない方法

新たなコンテナでログを溜めない方法は以下のものです。

はじめに、serviceを編集します。

vi /lib/systemd/system/docker.service

ExecStartを編集すればよい。
- max-fileは保管をする世代の数
- maz-sizeは最大サイズ

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --log-opt max-size=10m --log-opt max-file=1

こんなかんじ

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --log-opt max-size=10m --log-opt max-file=1
ExecReload=/bin/kill -s HUP $MAINPID
(以下略)                        

systemctlとdockerをリロードします。

systemctl daemon-reload
systemctl reload docker

Ref

https://stackoverflow.com/questions/42510002/how-to-clear-the-logs-properly-for-a-docker-container
https://www.unix.com/shell-programming-and-scripting/173276-how-loop-through-space-separated-values.html

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした