Skip to content

Instantly share code, notes, and snippets.

@ericwastaken
Created July 23, 2025 15:50
Show Gist options
  • Save ericwastaken/3a4af0c5286289165b0bc52832e7bd35 to your computer and use it in GitHub Desktop.
Save ericwastaken/3a4af0c5286289165b0bc52832e7bd35 to your computer and use it in GitHub Desktop.
Configure Docker container logs with rotation using the json-file logging driver in Docker Compose. This README includes step-by-step setup, retention settings (~7 days), and troubleshooting tips.

Docker Log Rotation with json-file Logging Driver

This project uses the default Docker json-file logging driver, configured with log rotation to retain logs for approximately 7 days.

Overview

Docker containers by default log all output to a file on the host machine. Over time, these logs can grow very large unless you configure rotation.

We use Docker Compose to define logging options that:

  • Limit log file size
  • Retain a fixed number of rotated log files (approximating 7 days)

Logging Configuration

The logging driver and options are defined in docker-compose.yml:

services:
  myapp:
    image: myapp-image
    container_name: myapp
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "7"

Explanation:

  • max-size: "10m": Each log file will be capped at 10 megabytes.
  • max-file: "7": Docker will keep 7 log files before deleting the oldest.
  • This provides approximately 7 days of logs assuming ~10MB per day.

Usage

Start the container

docker-compose up -d

View Logs

docker logs myapp

Apply New Logging Settings

If you update the logging settings, you must recreate the container for the changes to take effect:

docker-compose down
docker-compose up -d

Docker does not update logging configs on existing containers.

Notes

  • Logs are stored in /var/lib/docker/containers/<container-id>/ on the host.
  • If you need to retain logs for longer than 7 days or archive them externally, consider using a centralized logging system (e.g., Fluent Bit, ELK Stack, or AWS CloudWatch).

Troubleshooting

  • If logs aren't rotating, ensure the container was recreated after logging config changes.
  • To confirm log settings, run:
docker inspect myapp | grep -A 5 LogConfig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment