How can I configure Docker container log rotation on Ubuntu?
Asked by
fold-over
I wanted to limit the amount of disk space Docker container logs can consume on Ubuntu. The solution I used was to edit or create `/etc/docker/daemon.json` and configure the `json-file` logging driver with a 10 MB maximum size per file and up to 5 rotated files:
```json
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "5"
}
}
```
After saving the file, restart Docker with `sudo systemctl restart docker`. This applies the defaults to newly created containers, so existing containers may need to be recreated before they use the new logging settings.
Answers
Another option is Dockerโs `local` logging driver if you donโt need logs stored as JSON. Itโs designed to be more efficient and includes log rotation by default, so it can be a good alternative when you mainly want readable container logs without managing JSON files.
Reply to this answer
That configuration works well for keeping JSON logs under control. One important detail is that changing `daemon.json` generally affects newly created containers; existing ones may need to be recreated. Itโs also worth checking Dockerโs documentation because the logging configuration guidance can be easy to miss during the installation follow-up steps.
Reply to this answer
Taking containers down can clear or remove their accumulated logs, but thatโs more of a cleanup workaround than automatic rotation. Keeping regular backups is still important, and setting size and file limits prevents logs from growing unchecked between maintenance tasks.
Reply to this answer
Add your answer
Answers are reviewed before publication.
FerryQueue: Simply stopping and starting containers may not always recreate them with the new logging settings. Recreate them or explicitly verify the logging driver and options on each container.