68 lines
2.0 KiB
Bash
Executable File
68 lines
2.0 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Log file path
|
|
LOG_FILE="/opt/phx/log/docker_cleanup.log"
|
|
|
|
# Function to log informational messages with timestamp
|
|
log_info() {
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') [INFO] $1" | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
# Function to log error messages with timestamp
|
|
log_error() {
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') [ERROR] $1" | tee -a "$LOG_FILE" >&2
|
|
}
|
|
|
|
# Start Docker Cleanup
|
|
log_info "========== Docker Cleanup Started =========="
|
|
|
|
# Remove stopped containers
|
|
log_info "Removing all stopped Docker containers..."
|
|
if docker container prune -f >> "$LOG_FILE" 2>&1; then
|
|
log_info "Stopped Docker containers removed successfully."
|
|
else
|
|
log_error "Failed to remove stopped Docker containers."
|
|
fi
|
|
|
|
# Remove unused images
|
|
log_info "Removing all unused Docker images..."
|
|
if docker image prune -af >> "$LOG_FILE" 2>&1; then
|
|
log_info "Unused Docker images removed successfully."
|
|
else
|
|
log_error "Failed to remove unused Docker images."
|
|
fi
|
|
|
|
# Remove unused volumes
|
|
log_info "Removing all unused Docker volumes..."
|
|
if docker volume prune -f >> "$LOG_FILE" 2>&1; then
|
|
log_info "Unused Docker volumes removed successfully."
|
|
else
|
|
log_error "Failed to remove unused Docker volumes."
|
|
fi
|
|
|
|
# Remove unused networks
|
|
log_info "Removing all unused Docker networks..."
|
|
if docker network prune -f >> "$LOG_FILE" 2>&1; then
|
|
log_info "Unused Docker networks removed successfully."
|
|
else
|
|
log_error "Failed to remove unused Docker networks."
|
|
fi
|
|
|
|
# Optional: Remove dangling build cache
|
|
log_info "Removing dangling Docker build cache..."
|
|
if docker builder prune -f >> "$LOG_FILE" 2>&1; then
|
|
log_info "Dangling Docker build cache removed successfully."
|
|
else
|
|
log_error "Failed to remove dangling Docker build cache."
|
|
fi
|
|
|
|
# Optional: Restart Docker Service
|
|
log_info "Restarting Docker service to apply changes..."
|
|
if systemctl restart docker; then
|
|
log_info "Docker service restarted successfully."
|
|
else
|
|
log_error "Failed to restart Docker service."
|
|
fi
|
|
|
|
# Completion log
|
|
log_info "========== Docker Cleanup Completed ==========" |