简体   繁体   English

Docker 容器日志占用了我所有的磁盘空间

[英]Docker container logs taking all my disk space

I am running a container on a VM.我在虚拟机上运行一个容器。 My container is writing logs by default to /var/lib/docker/containers/CONTAINER_ID/CONTAINER_ID-json.log file until the disk is full.我的容器默认将日志写入 /var/lib/docker/containers/CONTAINER_ID/CONTAINER_ID-json.log 文件,直到磁盘已满。

Currently, I have to delete manually this file to avoid the disk to be full.目前,我必须手动删除此文件以避免磁盘已满。 I read that in Docker 1.8 there will be a parameter to rotate the logs .我读到在 Docker 1.8 中会有一个参数来旋转日志 What would you recommend as the current workaround?您会推荐什么作为当前的解决方法?

Docker 1.8 has been released with a log rotation option. Docker 1.8 已发布,带有日志轮换选项。 Adding:添加:

--log-opt max-size=50m 

when the container is launched does the trick.当容器启动时就可以了。 You can learn more at: https://docs.docker.com/engine/admin/logging/overview/您可以在以下位置了解更多信息: https : //docs.docker.com/engine/admin/logging/overview/

CAUTION: This is for docker-compose version 2 only注意:这仅适用于 docker-compose 版本 2

Example:例子:

version: '2'
services:
  db:
    container_name: db
    image: mysql:5.7
    ports:
      - 3306:3306
    logging:
      options:
        max-size: 50m

[This answer covers current versions of docker for those coming across the question long after it was asked.] [这个答案涵盖了 docker 的当前版本,适用于在提出问题很久之后才遇到的人。]

To set the default log limits for all newly created containers, you can add the following in /etc/docker/daemon.json:要为所有新创建的容器设置默认日志限制,您可以在 /etc/docker/daemon.json 中添加以下内容:

{
  "log-driver": "json-file",
  "log-opts": {"max-size": "10m", "max-file": "3"}
}

Then reload docker with systemctl reload docker if you are using systemd (otherwise use the appropriate restart command for your install).如果您使用的是 systemd,则使用systemctl reload docker docker(否则使用适当的重新启动命令进行安装)。

You can also switch to the local logging driver with a similar file:您还可以使用类似的文件切换到本地日志驱动程序:

{
  "log-driver": "local",
  "log-opts": {"max-size": "10m", "max-file": "3"}
}

The local logging driver stores the log contents in an internal format (I believe protobufs) so you will get more log contents in the same size logfile (or take less file space for the same logs).本地日志驱动程序以内部格式(我相信 protobufs)存储日志内容,因此您将在相同大小的日志文件中获得更多的日志内容(或者为相同的日志占用更少的文件空间)。 The downside of the local driver is external tools like log forwarders, may not be able to parse the raw logs.本地驱动程序的缺点是日志转发器等外部工具,可能无法解析原始日志。 Be aware the docker logs only works when the log driver is set to json-file , local , or journald .请注意, docker logs仅在日志驱动程序设置为json-filelocaljournald

The max-size is a limit on the docker log file, so it includes the json or local log formatting overhead. max-size是对 docker 日志文件的限制,因此它包括 json 或本地日志格式化开销。 And the max-file is the number of logfiles docker will maintain. max-file是 docker 将维护的日志max-file的数量。 After the size limit is reached on one file, the logs are rotated, and the oldest logs are deleted when you exceed max-file .在一个文件上达到大小限制后,日志将被轮换,当您超过max-file时,最旧的日志将被删除。

For more details, docker has documentation on all the drivers at: https://docs.docker.com/config/containers/logging/configure/有关更多详细信息,docker 有关于所有驱动程序的文档: https : //docs.docker.com/config/containers/logging/configure/

I also have a presentation covering this topic.我也有一个介绍这个主题的演讲。 Use P to see the presenter notes: https://sudo-bmitch.github.io/presentations/dc2019/tips-and-tricks-of-the-captains.html#logs使用P查看演示者笔记: https : //sudo-bmitch.github.io/presentations/dc2019/tips-and-tricks-of-the-captains.html#logs

Caution: this post relates to docker versions < 1.8 (which don't have the --log-opt option)注意:这篇文章涉及 docker 版本 < 1.8(没有--log-opt选项)

Why don't you use logrotate (which also supports compression)?为什么不使用 logrotate(它也支持压缩)?

/var/lib/docker/containers/*/*-json.log {
hourly
rotate 48
compress
dateext
copytruncate
}

Configure it either directly on your CoreOs Node or deploy a container (eg https://github.com/tutumcloud/logrotate ) which mounts /var/lib/docker to rotate the logs.直接在您的 CoreOs 节点上配置它或部署一个容器(例如https://github.com/tutumcloud/logrotate ),该容器挂载 /var/lib/docker 以旋转日志。

Pass log options while running a container.在运行容器时传递日志选项。 An example will be as follows一个例子如下

sudo docker run -ti --name visruth-cv-container  --log-opt max-size=5m --log-opt max-file=10 ubuntu /bin/bash

where --log-opt max-size=5m specifies the maximum log file size to be 5MB and --log-opt max-file=10 specifies the maximum number of files for rotation.其中--log-opt max-size=5m指定最大日志文件大小为 5MB,-- --log-opt max-file=10指定最大轮换文件数。

Example for docker-compose version 1: docker-compose 版本 1 的示例:

mongo:
  image: mongo:3.6.16
  restart: unless-stopped
  log_opt:
    max-size: 1m
    max-file: "10"

With compose 3.9, you can set a limit to the logs as below使用 compose 3.9,您可以对日志设置限制,如下所示

version: "3.9"
services:
  some-service:
    image: some-service
    logging:
      driver: "json-file"
      options:
        max-size: "200k"
        max-file: "10"

The example shown above would store log files until they reach a max-size of 200kB, and then rotate them.上面显示的示例将存储日志文件,直到它们达到 200kB 的最大大小,然后旋转它们。 The amount of individual log files stored is specified by the max-file value.存储的单个日志文件的数量由 max-file 值指定。 As logs grow beyond the max limits, older log files are removed to allow storage of new logs.随着日志增长超过最大限制,旧日志文件将被删除以允许存储新日志。

Logging options available depend on which logging driver you use可用的日志选项取决于您使用的日志驱动程序

  • The above example for controlling log files and sizes uses options specific to the json-file driver .上面controlling log files and sizes示例使用特定于json-file driver选项。 These particular options are not available on other logging drivers.这些特定选项在其他日志驱动程序上不可用。 For a full list of supported logging drivers and their options, refer to the logging drivers documentation.有关受支持的日志驱动程序及其选项的完整列表,请参阅日志驱动程序文档。

Note: Only the json-file and journald drivers make the logs available directly from docker-compose up and docker-compose logs.注意:只有json-filejournald驱动程序才能直接从journald -compose up 和 docker-compose 日志中获取日志。 Using any other driver does not print any logs.使用任何其他驱动程序不会打印任何日志。

Source: https://docs.docker.com/compose/compose-file/compose-file-v3/来源: https : //docs.docker.com/compose/compose-file/compose-file-v3/

Just in case you can't stop your container, I have created a script that performs the following actions (you have to run it with sudo):以防万一您无法停止容器,我创建了一个执行以下操作的脚本(您必须使用 sudo 运行它):

  1. Creates a folder to store compressed log files as backup.创建一个文件夹来存储压缩的日志文件作为备份。
  2. Looks for the running container's id (specified by the container's name).查找正在运行的容器的 id(由容器的名称指定)。
  3. Copy the container's log file to a new location (folder in step 1) using a random name.使用随机名称将容器的日志文件复制到新位置(步骤 1 中的文件夹)。
  4. Compress the previous log file (to save space).压缩以前的日志文件(以节省空间)。
  5. Truncates the container's log file by certain size that you can define.按您可以定义的特定大小截断容器的日志文件。

Notes:笔记:

  • It uses the shuf command.它使用shuf命令。 Make sure your linux distribution has it or change it to another bash-supported random generator.确保您的 linux 发行版具有它或将其更改为另一个支持 bash 的随机生成器。
  • Before use, change the variable CONTAINER_NAME to match your running container;使用前,更改变量CONTAINER_NAME以匹配您正在运行的容器; it can be a partial name (doesn't have to be the exact matching name).它可以是部分名称(不必是完全匹配的名称)。
  • By default it truncates the log file to 10M (10 megabytes), but you can change this size by modifying the variable SIZE_TO_TRUNCATE .默认情况下,它将日志文件截断为 10M(10 兆字节),但您可以通过修改变量SIZE_TO_TRUNCATE来更改此大小。
  • It creates a folder in the path: /opt/your-container-name/logs , if you want to store the compressed logs somewhere else, just change the variable LOG_FOLDER .它在路径中创建一个文件夹: /opt/your-container-name/logs ,如果您想将压缩日志存储在其他地方,只需更改变量LOG_FOLDER
  • Run some tests before running it in production.在生产中运行之前运行一些测试。
#!/bin/bash
set -ex

############################# Main Variables Definition:
CONTAINER_NAME="your-container-name"
SIZE_TO_TRUNCATE="10M"

############################# Other Variables Definition:
CURRENT_DATE=$(date "+%d-%b-%Y-%H-%M-%S")
RANDOM_VALUE=$(shuf -i 1-1000000 -n 1)
LOG_FOLDER="/opt/${CONTAINER_NAME}/logs"
CN=$(docker ps --no-trunc -f name=${CONTAINER_NAME} | awk '{print $1}' | tail -n +2)
LOG_DOCKER_FILE="$(docker inspect --format='{{.LogPath}}' ${CN})"
LOG_FILE_NAME="${CURRENT_DATE}-${RANDOM_VALUE}"

############################# Procedure:
mkdir -p "${LOG_FOLDER}"
cp ${LOG_DOCKER_FILE} "${LOG_FOLDER}/${LOG_FILE_NAME}.log"
cd ${LOG_FOLDER}
tar -cvzf "${LOG_FILE_NAME}.tar.gz" "${LOG_FILE_NAME}.log"
rm -rf "${LOG_FILE_NAME}.log"
truncate -s ${SIZE_TO_TRUNCATE} ${LOG_DOCKER_FILE}

You can create a cronjob to run the previous script every month.您可以创建一个 cronjob 来每月运行之前的脚本。 First run:第一次运行:

sudo crontab -e

Type a in your keyboard to enter edit mode.在键盘上输入a进入编辑模式。 Then add the following line:然后添加以下行:

0 0 1 * * /your-script-path/script.sh

Hit the escape key to exit Edit mode.按下Esc键退出编辑模式。 Save the file by typing :wq and hitting enter.输入:wq并按回车键保存文件。 Make sure the script.sh file has execution permissions.确保script.sh文件具有执行权限。

也可以使用docker run命令设置限制。

docker run -it -d -v /tmp:/tmp -p 49160:8080 --name web-stats-app --log-opt max-size=10m --log-opt max-file=5 mydocker/stats_app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM