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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github