By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
IT Infras HolicIT Infras HolicIT Infras Holic
  • News
  • Linux
    • Application
    • SELinux
    • Centos
    • Ubuntu
  • Docker
  • Web Server
    • Kong
    • Nginx
    • Openlitespeed
  • Database
  • Mikrotik
  • Windows
  • Mail
  • Tools
    • 2048
    • Fantasy Forest
    • Hextris
    • Crossword
Search
  • Privacy Policy
© 2024. All Rights Reserved.
Font ResizerAa
IT Infras HolicIT Infras Holic
Font ResizerAa
  • News
  • Linux
  • Docker
  • Web Server
  • Database
  • Mikrotik
  • Windows
  • Mail
  • Tools
Search
  • News
  • Linux
    • Application
    • SELinux
    • Centos
    • Ubuntu
  • Docker
  • Web Server
    • Kong
    • Nginx
    • Openlitespeed
  • Database
  • Mikrotik
  • Windows
  • Mail
  • Tools
    • 2048
    • Fantasy Forest
    • Hextris
    • Crossword
Follow US
© 2022 Foxiz News Network. Ruby Design Company. All Rights Reserved.
IT Infras Holic > Blog > Docker > How to Clear Docker Logs and Activate Log Rotate
DockerLinux

How to Clear Docker Logs and Activate Log Rotate

writer
Share
3 Min Read
SHARE

How to Clear Docker Logs and Activate Log Rotate

This note will guide you clear docker logs on a Docker container and activate log rotate to prevent disk getting full. If your system is getting 100% disk usage and you found that the docker container’s log files are consuming high disk space, you can find the log file location and clear them with the help of this tutorial. While clearing log files of a docker container, you don’t need to stop it

Contents
How to Clear Docker Logs and Activate Log RotateOption 1:Option 2:Option 3:Activate Log Rotate in Docker Logs

Below are the 3 options to clear docker logs of Docker containers. Choose any one of the below options to truncate the docker container log files.

Some of the below options require a container id or name, which can be found with the docker ps -a command.

 

Option 1:

In this option, first we will find the log file path and then truncate it. Use inspect option to find the log file name and location of a docker container.

1
docker container inspect  --format='{{.LogPath}}' <container_name_or_id>

As a result, you will get a log file path. Now truncate the log file with the following command.

1
truncate -s 0 /path/to/logfile

Here -s is used to set the size of a file. You provided 0 as input, which means completely truncating the log file.

 

Option 2:

You can combine both commands in a single command. Use the following command to truncate the log file of the specified Docker container.

1
truncate -s 0 $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)

 

Option 3:

Truncate the log files of all docker containers in your system.

1
truncate -s 0 /var/lib/docker/containers/*/*-json.log

You can quickly truncate the docker log files using one of the above options.

For me is prefer to choose option 3 with a modification like below.

1
sudo sh -c "truncate -s 0 /var/lib/docker/containers/*/*-json.log"

 

If you want to get the size of the logs

1
sudo sh -c "du -ch /var/lib/docker/containers/*/*-json.log"

 

If just to get the total of the size of the logs

1
sudo sh -c "du -ch /var/lib/docker/containers/*/*-json.log | grep total"

 

Activate Log Rotate in Docker Logs

Log rotate is a system utility that manages the automatic rotation and compression of log files. If log files were not rotated, compressed, and periodically pruned, they could eventually consume all available disk space on a system.

Example file in /etc/logrotate.d/docker-logs

1
2
3
4
5
6
7
8
9
/var/lib/docker/containers/*/*.log {
    rotate 7
    daily
    compress
    size=50M
    missingok
    delaycompress
    copytruncate
}

 

create config above and your docker logs will be rotate every week.

You Might Also Like

Monitoring File & Directory Changes using Bash Script

Vulnerability Checker CVE-2024-3094

Unleashing the Potential of Knowledge Management with Wiki.js

How to Install Apache Guacamole with Docker Compose

Unlocking Seamless Remote Access: Exploring the Power of Apache Guacamole

TAGGED: docker, docker logs, logrotate
Share This Article
Facebook Twitter Whatsapp Whatsapp LinkedIn Telegram Copy Link
Previous Article Install Kong Konga using Docker Compose Install Kong Konga using Docker Compose
Next Article java process manager Java Process Manager 2 (PM2) Cheat Sheet
1 Comment
  • best gold ira companies says:
    February 7, 2024 at 11:36 am

    Its like you read my mind! You seem to know a lot about
    this, like you wrote the book in it or something.
    I think that you could do with some pics to drive the message
    home a bit, but other than that, this is wonderful blog.
    A fantastic read. I’ll definitely be back.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Latest News

bash
Monitoring File & Directory Changes using Bash Script
Linux Application
vulnerability
Vulnerability Checker CVE-2024-3094
Linux
wiki.js
Unleashing the Potential of Knowledge Management with Wiki.js
Application Linux
Install Apache Guacamole
How to Install Apache Guacamole with Docker Compose
Application Linux

You Might also Like

How to Install Cyberpanel on Ubuntu 22.04
OpenlitespeedUbuntuWeb Server

Install Cyberpanel on Ubuntu 22.04

8 Min Read
How to map SFTP as a drive on Windows 10
LinuxWindows

How to map SFTP as a drive on Windows 10

3 Min Read
Follow US
© 2024
activity notes activity notes
Welcome Back!

Sign in to your account

Lost your password?