Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Docker»How to Clear Log Files of A Docker Container

    How to Clear Log Files of A Docker Container

    By RahulJune 21, 20222 Mins Read

    This tutorial will help you clear the log file on a Docker container. If your system is getting out of disk space and you found that the docker container’s log files are consuming high disk space. you can find the log file location and clear them with the help of this tutorial. While clearing log files of a docker container, you don’t need to stop it.

    Advertisement

    Clear Docker Container Log File

    Below are the 3 different options to clear log files of Docker containers. Choose any one of the below options to truncate the docker container log files.

    Some of the below options require a container id or name, which can be found with the docker ps -a command.

    • Option 1: In this option, first we will find the log file path and then truncate it. Use inspect option to find the log file name and location of a docker container.
      docker container inspect  --format='{{.LogPath}}' <container_name_or_id>
      

      As a result, you will get a log file path. Now truncate the log file with the following command.

      truncate -s 0 /path/to/logfile 
      

      Here -s is used to set the size of a file. You provided 0 as input, which means completely truncating the log file.

    • Option 2: You can combine both commands in a single command. Use the following command to truncate the log file of the specified Docker container.
      truncate -s 0 $(docker inspect --format='{{.LogPath}}' <container_name_or_id>) 
      
    • Option 3: Truncate the log files of all docker containers in your system.
      truncate -s 0 /var/lib/docker/containers/*/*-json.log 
      

    You can quickly truncate the docker log files using one of the above options.

    Wrap Up

    In this blog post, you have learned to truncate (clear) log files of a Docker container.

    Docker log
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Running a Cronjob Inside Docker: A Beginner’s Guide

    Running a Cronjob Inside Docker: A Beginner’s Guide

    Docker Run: A Beginner’s Guide to Run Docker Containers

    Docker Run: A Beginner’s Guide to Run Docker Containers

    Docker Build: A Beginner’s Guide to Building Docker Images

    Docker Build: A Beginner’s Guide to Building Docker Images

    View 4 Comments

    4 Comments

    1. Kieron on November 14, 2022 10:34 pm

      Thank you!! Great explanation solved my docker utilisation.

      Reply
    2. Craig on November 8, 2022 10:53 am

      truncate -s 0 /var/lib/docker/containers/*/*-json.log

      When trying to run this cmd, I get
      truncate: cannot open ‘/var/lib/docker/containers/*/*-json.log’ for writing: No such file or directory.

      I can see the file and filepath but cant understand why im getting this error?
      Any suggestions?

      Reply
    3. Kelly on August 18, 2022 7:02 pm

      Thank you for this article. My /var/lib/docker was 95% and Zabbix was sending me an email every few minutes and now I have resolved the issue.

      You are a breath of fresh air and can find easy solutions without all the technobabble.

      Reply
    4. Juraj on June 10, 2021 11:43 am

      oneline:
      docker inspect –format='{{.LogPath}}’ | xargs truncate -s 0

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.