Docker logs auto delete script
π§° Step 1: Create the script
sudo tee /usr/local/bin/docker-log-cleanup.sh > /dev/null <<'EOF'
#!/bin/bash
# Docker log cleanup + prevention script
# Author: ChatGPT Automation
# Date: $(date)
LOG_LIMIT_SIZE="100m"
LOG_MAX_FILES="3"
DOCKER_CONFIG="/etc/docker/daemon.json"
echo "π§Ή Truncating existing Docker logs..."
find /var/lib/docker/containers/ -type f -name "*.log" -exec truncate -s 0 {} \;
echo "βοΈ Ensuring Docker log rotation policy..."
if [ -f "\$DOCKER_CONFIG" ]; then
cp "\$DOCKER_CONFIG" "\${DOCKER_CONFIG}.bak_$(date +%F_%H-%M-%S)"
fi
cat > "\$DOCKER_CONFIG" <<JSON
{
"log-driver": "json-file",
"log-opts": {
"max-size": "$LOG_LIMIT_SIZE",
"max-file": "$LOG_MAX_FILES"
}
}
JSON
echo "π Restarting Docker to apply config..."
systemctl restart docker
echo "β
Docker log cleanup complete."
EOFπ Step 2: Automate with cron
π§Ύ Step 3: Verify
β
What this gives you
Last updated