某天在 linux 主機發現服務皆為異常,最後終於發現是 docker 的檔案大量增漲所造成
經查看之後發現是 docker 目錄突然佔了很大的空間
root:/home# du -h --max-depth=1
12M ./nuget
182G ./docker
root@:/home/docker# du -h --max-depth=1
136G ./containers
再往下看後發現是某個 container
root:/home/docker/containers# du -h --max-depth=1
660K ./a16553e7a96a35384cf35d1c47d99daba2d26d395857b50b6fec3b94aea00059
135G ./b96ed840c654f3895bdc2459d808be76e3cd37bd44307db50f37337a195c1ebb
再查看看到底是哪個服務
root@:/home/docker# docker ps | grep b96ed840
b96ed840c654 gitlab/gitlab-ce:16.7.6-ce.0 "/assets/wrapper" 3 months ago Up 4 weeks (healthy)
gitlab 理當不會產生這麼大的檔案,接著再往 container 裡面看看到底是什麼檔案在大
root@:/home/docker/containers/b96ed840# ls -lsh
total 135G
135G -rw-r----- 1 root root 135G Jun 21 07:08 b96ed840c654f3895bdc2459d808be76e3cd37bd44307db50f37337a195c1ebb-json.log
4.0K drwx------ 2 root root 4.0K Mar 6 06:13 checkpoints
32K -rw------- 1 root root 30K Jun 21 07:08 config.v2.json
4.0K -rw------- 1 root root 1.7K Jun 21 07:08 hostconfig.json
4.0K -rw-r--r-- 1 root root 15 May 21 05:47 hostname
4.0K -rw-r--r-- 1 root root 180 May 21 05:47 hosts
4.0K drwx--x--- 2 root root 4.0K Mar 6 06:13 mounts
4.0K -rw-r--r-- 1 root root 53 May 21 05:47 resolv.conf
4.0K -rw-r--r-- 1 root root 71 May 21 05:47 resolv.conf.hash
[container]-json.log 這個檔案是容器啟動後專門用來記執行 log 用的
也就是跟你在 docker logs [container id] 看到的東西是一樣的
然而這個東西卻累積到了上百g
首先先了解一下這是什麼東西
先在本機啟動一個 redis
C:\Users\User>docker run --name redis1 -d redis
a0d5aac6b60e298f040d5ec9278958bf6418f3c2bc61e3ed39e8c6f6547ded0c
接著到 \\wsl$\docker-desktop-data\data\docker\containers 找到相應的 container id 後,找尋 [container id]-json.log
打開檔案查看
{"log":"1:C 21 Jun 2024 07:25:15.698 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo\n","stream":"stdout","time":"2024-06-21T07:25:15.699408638Z"}
{"log":"1:C 21 Jun 2024 07:25:15.699 * Redis version=7.2.4, bits=64, commit=00000000, modified=0, pid=1, just started\n","stream":"stdout","time":"2024-06-21T07:25:15.69948834Z"}
{"log":"1:C 21 Jun 2024 07:25:15.699 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf\n","stream":"stdout","time":"2024-06-21T07:25:15.699492641Z"}
{"log":"1:M 21 Jun 2024 07:25:15.699 * monotonic clock: POSIX clock_gettime\n","stream":"stdout","time":"2024-06-21T07:25:15.699602644Z"}
{"log":"1:M 21 Jun 2024 07:25:15.701 * Running mode=standalone, port=6379.\n","stream":"stdout","time":"2024-06-21T07:25:15.701314398Z"}
{"log":"1:M 21 Jun 2024 07:25:15.702 * Server initialized\n","stream":"stdout","time":"2024-06-21T07:25:15.702608339Z"}
{"log":"1:M 21 Jun 2024 07:25:15.702 * Ready to accept connections tcp\n","stream":"stdout","time":"2024-06-21T07:25:15.702799245Z"}
接著我們再到主控台查看 log 內容
C:\Users\User>docker logs redis1
1:C 21 Jun 2024 07:25:15.698 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 21 Jun 2024 07:25:15.699 * Redis version=7.2.4, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 21 Jun 2024 07:25:15.699 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 21 Jun 2024 07:25:15.699 * monotonic clock: POSIX clock_gettime
1:M 21 Jun 2024 07:25:15.701 * Running mode=standalone, port=6379.
1:M 21 Jun 2024 07:25:15.702 * Server initialized
1:M 21 Jun 2024 07:25:15.702 * Ready to accept connections tcp
可以發現這兩個是一樣的,只是輸出格式不同,但內容是一樣的
接著我們啟動一個 reids 是不用寫 log 的
C:\Users\User>docker run --name redis2 --log-driver none -d redis
fed1cfc7e91276b822e39340d5b8d01cc011b5504f477602d6c118c330291688
從 container log 就完全看不到 json.log 的檔案
甚至從 cmd 直接查看也沒有東西可以展示
C:\Users\User>docker logs redis2
Error response from daemon: configured logging driver does not support reading
另外一種方式是限制 log 成長大小
C:\Users\User>docker run --name redis3 -p 11111:6379 --log-opt max-size=1k -d redis
9a6384ffed6c9a4ae72936f2b8e9b82ec0fb2cfc56b110de1303b46674a0fc27
參考資料
https://stackoverflow.com/a/72970501
https://stackoverflow.com/a/32091923