How to Configure Docker Rotate Log on Amazon Linux 2

How to Configure Docker Rotate Log on Amazon Linux 2

DevOps engineers are always looking for innovative ways to improve business operations. One such situation was in a growing e-commerce company that required an efficient log management solution for its Docker containers. With thousands of orders processed daily, the company’s server logs expanded alarmingly and became increasingly difficult to handle.

This tutorial will walk you through configuring Docker Rotate Log on Amazon Linux 2 using a practical example from the e-commerce company’s project experience. This will help you understand the concept and why log rotation is crucial for maintaining a healthy system.

The E-Commerce Company’s Challenge

The e-commerce company’s microservices architecture utilized multiple Docker containers on Amazon Linux 2 instances. The business processed thousands of transactions daily, resulting in massive logs generated by various services like payment processing, product catalog updates, and user authentication.

As these logs grew exponentially over time, it became challenging for DevOps engineers to manage them effectively. Without proper log rotation, numerous issues could have arisen:

  1. Insufficient disk space.
  2. Longer backup times.
  3. Reduced performance due to increased I/O operations.
  4. Difficulty in identifying and troubleshooting critical issues.

The need for an automated solution to efficiently manage these logs was evident – enter Docker Rotate Log configuration.

Configuring Docker Rotate Log on Amazon Linux 2

To resolve this challenge, the e-commerce company implemented log rotation using Docker daemon configuration with --log-opt options on their Amazon Linux 2 instances.

Follow the steps below to configure your setup:

Step 1: Update Your System

Firstly, update your Amazon Linux ₂ system packages by running:

sudo yum update -y

Step 2: Install and Start Docker

Install the latest version of docker by running:

sudo amazon-linux-extras install docker -y

Then, start the Docker service and enable it at system boot:

sudo systemctl start docker
sudo systemctl enable docker

Step 3: Configure Docker Daemon

To configure the Docker daemon with log rotation options, create a new file named /etc/docker/daemon.json:

sudo touch /etc/docker/daemon.json

Open the file using your preferred text editor and add the following configuration:

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "5"
  }
}

In this example, we set up a JSON log driver with two options:

  • max-size: The maximum size of each log file before rotation. Here, we set it to 10 MB.
  • max-file: The maximum number of log files to retain. In this case, we chose to keep five files.

Adjust these values according to your specific requirements.

Step 4: Restart Docker Service

After saving your changes in the daemon.json file, restart the Docker service for your configuration changes to take effect:

sudo systemctl restart docker.service

This real-world scenario from an e-commerce company’s project experience shows how crucial rotating logs are for maintaining a healthy system. By implementing Docker Rotate Log on Amazon Linux ₂ instances using these configurations, you will have an automated solution that efficiently manages server logs generated by your services.

As a result of this project implementation in our e-commerce example, DevOps engineers could maintain system performance and troubleshoot issues effectively without being overwhelmed by massive volumes of logs.

Alternative: Configuring Log Rotation Using logrotate for Docker Logs on Amazon Linux 2

If you prefer not to modify the Docker daemon configuration for log rotation, there is an alternative method using logrotate. This approach involves creating a custom logrotate configuration file for Docker container logs.

Step 1: Install logrotate

First, update your Amazon Linux ₂ system packages by running:

sudo yum update -y

Next, install the logrotate package:

sudo yum install logrotate -y

Step 2: Create a Custom Logrotate Configuration File for Docker

Create a new file named /etc/logrotate.d/docker-containers:

sudo touch /etc/logrotate.d/docker-containers

Open the file using your preferred text editor and add the following configuration:

/var/lib/docker/containers/*/*.log {
    rotate 7
    daily
    compress
    size=50M
    missingok
    delaycompress
    copytruncate
}

In this example, we set up several options:

  • rotate: The number of times to rotate each log file before removing it. Here, we set it to seven rotations.
  • daily: Rotate logs daily.
  • compress: Compress rotated files.
  • size=50M: A maximum size of each log file before rotation. In this case, we set it to 50 MB.
  • missingok: Do not report an error if a log file is missing.
  • delaycompress: Postpone compression until after the second time a log rotates (to retain an uncompressed version).
  • copytruncate: Truncate the original log file in place after making a copy.

Adjust these values according to your specific requirements.

Step 3: Check Log Rotation Configuration

To ensure that your new configuration is correct, execute logrotate with the --debug flag:

sudo logrotate --debug /etc/logrotate.conf

Check the output for any issues or errors.

Step 4: Force Log Rotation (Optional)

If you want to force an immediate log rotation to test your configuration, use the --force flag:

sudo logrotate --force /etc/logrotate.conf

Conclusion

In this tutorial, we explored two solutions for managing Docker container logs on Amazon Linux ₂ instances – configuring log rotation using Docker daemon’s --log-opt options and setting up log rotation with the logrotate utility. Both methods provide efficient ways to manage log files in real-world scenarios, such as the e-commerce company example.

Selecting the most suitable approach depends on your project requirements and preferences. Configuring Docker daemon settings offers a more straightforward way to control log rotation directly through Docker. On the other hand, using logrotate allows for greater flexibility and customization of log management without altering Docker configurations.

Regardless of your chosen method, implementing a proper log rotation strategy is crucial for maintaining system performance, optimizing storage usage, troubleshooting issues effectively, and avoiding potential pitfalls associated with large logs.

By following this tutorial and applying the concepts learned to your projects, you’re taking a proactive step towards ensuring smooth business operations while keeping your infrastructure healthy and easily manageable.

Leave a Comment

Your email address will not be published. Required fields are marked *