How to Clear Docker Logs for a Container

When Docker containers run, they continuously generate log entries, recording everything from system errors to operational information. These logs are essential for troubleshooting and monitoring the health and performance of containers. However, without proper management, these log files can grow to consume significant disk space, potentially leading to issues with storage capacity and system performance.

In such scenarios, it becomes necessary to clear these logs. In this blog post, we'll begin by understanding Docker's default logging driver. Then, we'll explore how to clear Docker container logs efficiently using log rotation. Let's get started!

Understanding Default Docker Log Driver

In Docker, container logs are based on the concept that an application runs as PID 1 inside the container and directs its standard output (STDOUT) and standard error (STDERR) to the logging driver. The Docker daemon then uses the logging driver to forward these logs to configured destinations.

The default logging driver in Docker is json-file. This driver writes logs in a JSON format to a file, which is meant to be exclusively managed by the Docker daemon. You should never interact with these log files directly using external tools, as it may disrupt Docker's logging mechanism and lead to unexpected behaviors.

Note: You might come across resources that suggest using commands like truncate to clear Docker logs. However, this approach is risky and not recommended, as it can interfere with how Docker handles its log files.

To confirm which logging driver Docker is using, run the following command: 

docker info --format '{{.LoggingDriver}}' 

The output should typically be json-file, as shown below: 

docker info command output

It’s important to note that the json-file logging driver does not enable log rotation by default. This means that, unless configured otherwise, this driver will continue writing to the same log file without limiting its size or the number of files. Over time, this can lead to very large log files, potentially consuming significant disk space.

In the next section, we’ll learn how to explicitly configure log rotation settings for the json-file driver to clear Docker logs for a container.

Clearing Docker Logs Using Log Rotation

Log rotation is the process of automatically managing log files to prevent them from growing excessively large. This usually involves limiting the size of each log file and the number of log files kept. When a log file reaches a certain size, the system starts a new log file, and the oldest files are deleted to make room for new ones.

In Docker, log rotation can be configured either at the individual container level or globally for all containers

When you configure log rotation settings for a specific container, these settings apply only to that container. However, it requires you to manually set the log rotation parameters each time you start a new container. 

On the other hand, setting up log rotation globally means that your specified log management policies will automatically apply to every new container you create without the need for individual configuration.

Implementing log rotation at the container level

Run the following command to run a container named logger-demo in detached mode:

docker run -d --name logger-demo --log-driver json-file --log-opt max-size=1k --log-opt max-file=2 ubuntu /bin/bash -c 'i=0; while true; do echo "Hello World $((i++)) - A quick brown fox jumps over the lazy dog"; sleep 1; done'

Here is the output after executing the command:

docker run command output

In this command: 

  • --log-driver json-file: Specifies that the container should use the json-file log driver. 
  • --log-opt max-size=1k: This option sets the maximum size of a log file to 1 kilobyte. Once a log file reaches this size, Docker will close the current log file and start a new one. Note that the size is specified as a positive integer followed by a unit: k for kilobytes, m for megabytes, and g for gigabytes. The default max-size value is -1 (unlimited).
  • --log-opt max-file=2: This sets the maximum number of log files to keep. In this case, Docker will keep up to 2 log files for each container. When this limit is reached, the oldest log file will be deleted when a new one is created. Note that the default max-file value is 1.
  • /bin/bash -c 'i=0; while true; do echo "Hello World $((i++)) - A quick brown fox jumps over the lazy dog"; sleep 1; done': This is the command that the container will execute. It starts a bash shell that runs an infinite loop (while true). In each iteration of the loop, the script outputs a string Hello World followed by an incrementing number and a static message. It then pauses for 1 second (sleep 1) before repeating. Note that I specifically designed this command to rapidly fill the log file, so that we can observe log rotation in action.

Next, run the following command to see the logs of the logger-demo container:

docker logs logger-demo

You’ll see an output similar to this: 

docker logs command output

Now, wait for about 30 seconds and run the command again. The new output will look something like this:

docker logs command output

Notice the gap in the sequence numbers – it jumps from 10 to 40. This gap is concrete evidence that log rotation has occurred. Let me explain:

When we first checked the logs, we saw entries like Hello World 0 through Hello World 10.This was the content of the current log file at that moment.

When we checked the logs the second time, we saw entries starting from Hello World 40, skipping the sequence from Hello World 11 to Hello World 39. This gap indicates that the previous log file (which would have contained Hello World 11 to Hello World 39) reached its size limit of 1KB and was rotated out. The new log entries are now being written to a fresh log file.

Implementing log rotation globally

We can configure log rotation globally through the Docker Desktop GUI. Follow the steps below: 

Step 1: Open Docker Desktop:

Docker Desktop

Step 2: Click on the Settings icon (highlighted in red in the image below) to open the Settings page: 

Docker Desktop

Step 3: Once on the Settings page, select Docker Engine:

Docker Settings page

You will see the current Docker daemon JSON configuration, which might look similar to this:

{
  "builder": {
    "gc": {
      "defaultKeepStorage": "20GB",
      "enabled": true
    }
  },
  "experimental": false,
  "features": {
    "buildkit": true
  }
}

To include global log rotation settings, you'll need to add the log-driver and log-opts keys to this JSON structure. Your updated configuration should look like this:

{
  "builder": {
    "gc": {
      "defaultKeepStorage": "20GB",
      "enabled": true
    }
  },
  "experimental": false,
  "features": {
    "buildkit": true
  },
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "1k",
    "max-file": "2"
  }
}

Adjust the max-size and max-file values based on your specific use case.

Finally, click the Apply & Restart button to save your changes and restart the Docker daemon for the changes to take effect.

Note that these changes will only apply to new containers created after the Docker daemon restarts. Existing containers will continue to use the logging configuration that was in effect when they were created.

Conclusion

In this blog post, we learnt how to clear Docker logs for a container using log rotation. 

Interested in learning more about Docker? Check out the following courses from KodeKloud: 

  • Docker for the Absolute Beginner: This course will help you understand Docker using lectures and demos. You’ll get a hands-on learning experience and coding exercises that will validate your Docker skills.  Additionally, assignments will challenge you to apply your skills in real-life scenarios.
  • Docker Certified Associate Exam Course: This course covers all the required topics from the Docker Certified Associate Exam curriculum. The course offers several opportunities for practice and self-assessment. There are hundreds of research questions in multiple-choice format, practice tests at the end of each section, and multiple mock exams that closely resemble the actual exam pattern.