Skip to content

lazybytez/logrotate-container

 
 

Logrotate Container

gh-commit-badge gh-contributors-badge gh-stars-badge

Description

A small side-car container that discovers log files and rotates them using the standard logrotate utility. It is intended to be attached to workloads that write logs to disk and need automated rotation/retention.

This fork modernizes the project and bringt up to date containers.

Overview

This container:

  • Generates a logrotate configuration based on environment variables.
  • Runs logrotate on a schedule (interval or cron schedule).
  • Supports compression, size thresholds, custom olddir, statusfile and scripts to run before/after rotation.

Usage

Rotate Docker container logs (daily, keep 5 rotations):

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" \
ghcr.io/lazybytez/logrotate-container

Rotate hourly:

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" \
ghcr.io/lazybytez/logrotate-container

Docker Compose example:

services:
    logrotate:
        image: ghcr.io/lazybytez/logrotate-container:latest
        environment:
            LOGS_DIRECTORIES: /config/log/app/
            LOGROTATE_INTERVAL: daily
            LOGROTATE_SIZE: 1G
        volumes:
            - app_data:/config

Environment variables

  • LOGS_DIRECTORIES (required) - space separated directories to scan, e.g. "/var/log /var/lib/docker/containers"
  • LOG_FILE_ENDINGS - space separated file extensions (default: "log")
  • LOGROTATE_INTERVAL - hourly|daily|weekly|monthly|yearly (affects logrotate rules)
  • LOGROTATE_CRONSCHEDULE - cron expression for the schedule; default is suitable for interval
  • LOGROTATE_COPIES - number of rotated copies to keep (default 5)
  • LOGROTATE_SIZE - trigger rotate when file exceeds size (e.g. 100k, 10M)
  • LOGROTATE_COMPRESSION - set to "compress" to enable compression
  • LOGROTATE_DELAYCOMPRESS - "false" to disable default delaycompress (when compression enabled)
  • LOGROTATE_MODE - e.g. "create 0644" to change rotate mode (default: copytruncate)
  • LOGROTATE_OLDDIR - directory to move old logs into
  • LOGROTATE_STATUSFILE - path to logrotate status file
  • LOGROTATE_PARAMETERS - raw flags passed to logrotate (e.g. "vdf")
  • LOGROTATE_PREROTATE_COMMAND / LOGROTATE_POSTROTATE_COMMAND - scripts/commands to run
  • LOGROTATE_AUTOUPDATE - regenerate logrotate configuration on each cron execution

Examples & notes

  • To capture Docker JSON logs: set LOGS_DIRECTORIES to /var/lib/docker/containers and mount that path into the container.
  • To keep rotated logs in a separate volume: mount a host dir and set LOGROTATE_OLDDIR to that mount point.
  • If compression is enabled but you want the newest rotated file compressed immediately: set LOGROTATE_DELAYCOMPRESS=false.

Limitations

  • Containers in read-only mode are not supported (this image generates a logrotate configuration on startup).

Troubleshooting

  • Check the cron log (if you configured LOG_FILE) and the logrotate logfile (LOGROTATE_LOGFILE).
  • Ensure mounted paths are accessible by the container user.
  • Use LOGROTATE_PARAMETERS=v or vf to troubleshoot behavior without making changes.
  • Ensure the container is not running in read-only mode.

Contributing

If you want to take part in contribution, like fixing issues and contributing directly to the code base, please visit the How to Contribute document.

Useful links

About

Logrotate container image based on blacklabelops/logrotate

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Shell 79.9%
  • Dockerfile 17.9%
  • JavaScript 2.2%