How to clear the logs properly for a Docker container?

Better Stack Team
Updated on April 14, 2023

To clear the logs for a Docker container, you can use the logrotate command, which is included in most Linux distributions. Here are the steps:

First, you need to access the logs for the container you want to clear. You can do this using the following command:

docker logs container_name > logs.txt

Replace `container_name` with the name or ID of the container, and `logs.txt` with the name of the file where you want to save the logs.

Once you have saved the logs to a file, you can rotate them using the logrotate command. To do this, create a new configuration file in the /etc/logrotate.d/ directory using your favorite text editor. For example:

sudo nano /etc/logrotate.d/docker-container-name

Replace `docker-container-name` with the name of the container whose logs you want to clear. In the new configuration file, add the following lines:

/path/to/logs.txt {
    size 100M
    create 0644 root root
        docker exec container_name truncate -s0 /path/to/logs.txt

Replace `/path/to/logs.txt` with the path to the log file you saved in step 1, and `container_name` with the name or ID of the container.

This configuration file tells `logrotate` to:

- Ignore missing log files (`missingok`)
- Don't rotate the log file if it's empty (`notifempty`)
- Rotate the log file if it exceeds 100MB in size (`size 100M`)
- Create new log files with permissions of 0644 and owned by root (`create 0644 root root`)
- Compress rotated log files (`compress`)
- Defer compression until the next rotation cycle (`delaycompress`)
- Run the specified script after the log file is rotated (`postrotate`)
- The script truncates the log file to 0 bytes, effectively clearing it (`docker exec container_name truncate -s0 /path/to/logs.txt`)
- End the script block (`endscript`)

Save and close the configuration file.

Before running the rotation, you can test the configuration by running the following command:

sudo logrotate --verbose /etc/logrotate.d/docker-container-name

This will show you what `logrotate` will do, without actually performing any actions.

Once you are satisfied with the configuration, you can run the rotation by using the following command:

sudo logrotate --force /etc/logrotate.d/docker-container-name

This will force `logrotate` to rotate the logs for the specified container.

Note: The above steps assume that you are running the Docker container on a Linux host. If you are using Docker for Windows or Docker for Mac, the steps may differ. Please consult the Docker documentation for your platform.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack