Docker log files : verify size and optionally truncate to 0 bytes

qwizzie

Well-known member
**** Obsolete due to dashmate now having docker log rotation ****
**** Adjusted 18th of March 2025 for script to search for 'container.log' instead of 'json.log'


Let Dashmate users verify the size of the Docker log files of the Platform and Core containers and optionally reset / truncate it to 0 bytes.
Only do this if these log files are getting very very big, as having log files in place to analyse during this beta phase of Platform activation is important.

Step 1 : nano logsize.sh
Step 2 : add following code

Bash:
#!/bin/bash

# Function to truncate log file
truncate_log() {
    log_file=$1
    echo "Truncating log file: $log_file"
    sudo truncate -s0 "$log_file"
    echo "Log file truncated."
}

# Iterate over all container IDs
for container_id in $(docker ps -q); do
  # Get the full container ID
  full_container_id=$(docker inspect --format='{{.Id}}' "$container_id")

  # Search for container.log inside the container's directory
  log_file=$(find "/var/lib/docker/containers/${full_container_id}" -type f -name "container.log" 2>/dev/null)

  # Check if the log file exists and then get its size
  if [ -n "$log_file" ]; then
    size=$(sudo du -sh "$log_file" | awk '{print $1}')
    echo "Container ID: $full_container_id - Log Size: $size"

    # Ask user if they want to truncate the log file
    while true; do
      read -p "Do you want to truncate this log file to 0 bytes? (y/n): " yn
      case $yn in
          [Yy]* ) truncate_log "$log_file"; break;;
          [Nn]* ) echo "Skipping truncation for $log_file"; break;;
          * ) echo "Please answer yes (y) or no (n).";;
      esac
    done
  else
    echo "Container ID: $full_container_id - Log file not found"
  fi
done

Ctrl + X (Save? y enter)
Step 3 : chmod +x logsize.sh
Step 4 : sudo ./logsize.sh
 
Last edited:
Back
Top