SkillAgentSearch skills...

Logrotate

Dockerized Logrotate. Attach, Compress, Rotate!

Install / Use

/learn @blacklabelops/Logrotate
About this skill

Quality Score

0/100

Supported Platforms

Zed

README

Dockerized Logrotate

Open Issues Stars on GitHub Docker Stars Docker Pulls

This container can crawl for logfiles and rotate them. It is a side-car container for containers that write logfiles and need a log rotation mechanism. Just hook up some containers and define your backup volumes.

Supported tags and respective Dockerfile links

| Distribution | Version | Tag | Dockerfile | |--------------|--------------|--------------|------------| | Logrotate Alpine | latest, 1.3 | latest, 1.3 | Dockerfile |

Make It Short

In short, this container can rotate all your Docker logfiles just by typing:

$ docker run -d \
  -v /var/lib/docker/containers:/var/lib/docker/containers \
  -v /var/log/docker:/var/log/docker \
  -e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker" \
  blacklabelops/logrotate

This will rotate all your Docker logfiles on a daily basis up to 5 times.

You want to do it hourly? Just type:

$ docker run -d \
  -v /var/lib/docker/containers:/var/lib/docker/containers \
  -v /var/log/docker:/var/log/docker \
  -e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker" \
  -e "LOGROTATE_INTERVAL=hourly" \
  blacklabelops/logrotate

This will put logrotate on an hourly schedule.

How To Attach to Logs

In order to attach the side-car container to your logs you have to hook your log file folders inside volumes. Afterwards specify the folders logrotate should crawl for log files. The container attaches by default to any file ending with .log inside the specified folders.

Environment variable for specifying log folders: LOGS_DIRECTORIES. Each directory must be separated by a whitespace character.

Example:

LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker

Example Logrotating all Docker logfiles:

$ docker run -d \
  -v /var/lib/docker/containers:/var/lib/docker/containers \
  -v /var/log/docker:/var/log/docker \
  -e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker" \
  blacklabelops/logrotate

This will logrotate any logfile(s) under /var/lib/docker/containers, /var/log/docker (or subdirectories of them).

Customize Log File Ending

You can define the file endings fluentd will attach to. The container will by default crawl for files ending with .log. This can be overriden and extended to any amount of file endings.

Example:

$ docker run -d \
  -v /var/lib/docker/containers:/var/lib/docker/containers \
  -v /var/log/docker:/var/log/docker \
  -e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker" \
  -e "LOG_FILE_ENDINGS=json xml" \
  blacklabelops/logrotate

Crawls for file endings .json and .xml.

Set the Log interval

Logrotate can rotate logfile according to the following intervals:

  • hourly
  • daily
  • weekly
  • monthly
  • yearly

You can override the default setting with the environment variable LOGROTATE_INTERVAL.

Example:

$ docker run -d \
  -v /var/lib/docker/containers:/var/lib/docker/containers \
  -v /var/log/docker:/var/log/docker \
  -e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker" \
  -e "LOGROTATE_INTERVAL=hourly" \
  blacklabelops/logrotate

This will logrotate logfile(s) on hourly basis.

Set the Number of Rotations

The default number of rotations is five. Further rotations will delete old logfiles. You can override the default setting with the environment variable LOGROTATE_COPIES.

Example:

$ docker run -d \
  -v /var/lib/docker/containers:/var/lib/docker/containers \
  -v /var/log/docker:/var/log/docker \
  -e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker" \
  -e "LOGROTATE_COPIES=10" \
  blacklabelops/logrotate

Will create 10 daily logs before deleting old logs.

Set Maximum File size

Logrotate can do additional rotates, when the logfile exceeds a certain file size. You can specifiy file size rotation with the environment variable LOGROTATE_SIZE.

Valid example values:

  • 100k: Will rotate when log file exceeds 100 kilobytes.
  • 100M: Will rotate when log file exceeds 100 Megabytes.
  • 100G: Will rotate when log file exceeds 100 Gigabytes.
$ docker run -d \
  -v /var/lib/docker/containers:/var/lib/docker/containers \
  -v /var/log/docker:/var/log/docker \
  -e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker" \
  -e "LOGROTATE_SIZE=10M" \
  blacklabelops/logrotate

This will logrotate when logfile(s) reaches 10M+.

Set Log File compression

The default logrotate setting is nocompress. In order to enable logfile compression you can set the environment variable LOGROTATE_COMPRESSION to compress.

Example:

$ docker run -d \
  -v /var/lib/docker/containers:/var/lib/docker/containers \
  -v /var/log/docker:/var/log/docker \
  -e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker" \
  -e "LOGROTATE_COMPRESSION=compress" \
  blacklabelops/logrotate

This will compress the logrotated logs.

Turn Off Log File delaycompress

When compression is turned on, delaycompress will be set by default. To turn this off, set the environment variable LOGROTATE_DELAYCOMPRESS to false.

Example:

$ docker run -d \
  -v /var/lib/docker/containers:/var/lib/docker/containers \
  -v /var/log/docker:/var/log/docker \
  -e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker" \
  -e "LOGROTATE_COMPRESSION=compress" \
  -e "LOGROTATE_DELAYCOMPRESS=false" \
  blacklabelops/logrotate

This will compress all logrotated logs, including the most recent one.

Set logrotate mode

By default, logrotate will use copytruncate mode to create a new rotated file, however certain log collection applications won't work properly with this configuration. To use a different option, such as create <mode> <owner> <group>, set the environment variable LOGROTATE_MODE.

Example:

$ docker run -d \
  -v /var/lib/docker/containers:/var/lib/docker/containers \
  -v /var/log/docker:/var/log/docker \
  -e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker" \
  -e "LOGROTATE_MODE=create 0644"
  blacklabelops/logrotate

This will rename the current log file, and create a new one in its place

Set the Output directory

By default, logrotate will rotate logs in their respective directories. You can specify a directory for keeping old logfiles with the environment variable LOGROTATE_OLDDIR. You can specify a full or relative path.

Example:

$ docker run -d \
  -v /var/lib/docker/containers:/var/lib/docker/containers \
  -v /var/log/docker:/var/log/docker \
  -v $(pwd)/logs:/logs/ \
  -e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker" \
  -e "LOGROTATE_OLDDIR=/logs" \
  blacklabelops/logrotate

Will move old logfiles in the local directory logs/.

Set the Cron Schedule

You can set the cron schedule independently of the logrotate interval. You can override the default schedule with the enviroment variable LOGROTATE_CRONSCHEDULE.

Example:

$ docker run -d \
  -v /var/lib/docker/containers:/var/lib/docker/containers \
  -v /var/log/docker:/var/log/docker \
  -e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker" \
  -e "LOGROTATE_CRONSCHEDULE=* * * * * *" \
  blacklabelops/logrotate

This will logrotate on go-cron schedule * * * * * * (every second).

Log and View the Logrotate Output

You can specify a logfile for the periodical logrotate execution. The file is specified using the environment variable LOGROTATE_LOGFILE. Must be a full path!

Example:

$ docker run -d \
  -v /var/lib/docker/containers:/var/lib/docker/containers \
  -v /var/log/docker:/var/log/docker \
  -v $(pwd)/logs:/logs \
  -e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker" \
  -e "LOGROTATE_INTERVAL=hourly" \
  -e "LOGROTATE_LOGFILE=/logs/logrotatecron.log" \
  blacklabelops/logrotate

You will be able to see logrotate output every minute in file logs/logrotatecron.log.

Logrotate Commandline Parameters

You can define the logrotate commandline parameters with the environment variable LOGROTATE_PARAMETERS.

v: Verbose

d: Debug, Logrotate will be emulated but never executed!

f: Force

Example for a typical testrun:

$ docker run -d \
  -v /var/lib/docker/containers:/var/lib/docker/containers \
  -v /var/log/docker:/var/log/docker \
  -v $(pwd)/logs:/logs \
  -e "LOGS_DIRECTORIES=/var/lib/docker/containers /var/log/docker" \
  -e "LOGROTATE_PARAMETERS=vdf" \
  -e "LOG_FILE=/logs/cron.log" \
  blacklabelops/logrotate

Will run logrotate with: /usr/bin/logrotate -dvf

Logrotate Status File

Logrotate must remember when files have been rotated when using time intervals, e.g. 'daily'. The status file will be written by default to the container volume but you can specify a custom location with the environment variable LOGROTATE_STATUSFILE.

Example:

$ docker run -d \
  -e "LOGROTATE_INTERVAL=hourly" \
  -e "LOGROTATE_STATUSFILE=/logrotate-status/logrotate.status" \
  -e "ALL_LOGS_DIRECTORIES=/var/log" \
  -e "LOGROTATE_PARAMETERS=vf" \
  blacklabelops/logrotate

Writes the latest status file each logrotation. Reads status files at each start.

Log and View the Cron Output

You can specify

View on GitHub
GitHub Stars262
CategoryDevelopment
Updated21d ago
Forks109

Languages

Shell

Security Score

95/100

Audited on Mar 13, 2026

No findings