Prune Docker logs by age
Prune Docker logs by age with docker-logs-cleaner
Repo: https://codeberg.org/wollomatic/docker-logs-cleaner
The GDPR Challenge: Why Log Retention Matters
Imagine you’re running a small web service in the EU, logging user requests for debugging and security purposes. Under the General Data Protection Regulation (GDPR), you must ensure that personal data — including IP addresses or user identifiers in logs — isn’t retained longer than necessary (Side note: This website does not log anything). Keeping logs indefinitely isn’t just bad practice; it could expose you to compliance risks.
That’s where docker-logs-cleaner comes in. This lightweight tool helps you automatically prune Docker container logs older than a specified age, ensuring you meet data protection requirements without manual intervention. For example, you could set it to clear log entries after two weeks - a balance between operational needs and GDPR compliance.
docker-logs-cleaner is a Bash script designed to filter and remove old entries from Docker’s logs. It only works with the json-file logging driver.
It is a pragmatic solution for GDPR-compliant log management in Docker environments. It’s not a silver bullet, but it fills a gap for users who need simple, time-based log pruning without over-engineering their setup.
Disclaimer: This is an example script provided “as is”. It works in my environment, but it hasn’t been tested elsewhere. Please review the code, understand it, and test it in your own setup before using it!
Prerequisites
- Docker with
json-filelog driver jqinstalled for JSON processing- Root/sudo access to log files
How It Works
The script processes the container logfiles and removes entries older than a specified cutoff date. For example, to keep only the last 14 days of logs for a container named my-web-app, you’d run:
# Keep logs for the last 7 days for container "static-web-server"
./docker-logs-cleaner.sh static-web-server 7
# Default: 14 days (if no age is specified)
./docker-logs-cleaner.sh my-app
Modifying the files of another running process is not a good idea. Stop the container before running this script.
Contributing
This is an open-source project, and contributions are welcome! If you have ideas for improvements, feel free to open an issue or submit a pull request: codeberg.org/wollomatic/docker-logs-cleaner