#!/bin/bash # Docker-in-Docker Cleanup Script # This script provides a simple way to clean up the DinD environment # by restarting the DinD container, which gives a fresh environment. set -e # Color codes for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Logging functions log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } show_help() { cat << EOF Docker-in-Docker Cleanup Script Usage: $0 [OPTIONS] Options: --dry-run Show what would be done without executing --help|-h Show this help message Examples: $0 # Clean up DinD environment $0 --dry-run # Show what would be done EOF } # Parse command line arguments DRY_RUN="false" while [[ $# -gt 0 ]]; do case $1 in --dry-run) DRY_RUN="true" shift ;; --help|-h) show_help exit 0 ;; *) log_error "Unknown option: $1" show_help exit 1 ;; esac done # Main cleanup function cleanup_dind() { echo echo "==================================================================================" echo " ๐Ÿงน Docker-in-Docker Cleanup ๐Ÿงน" echo "==================================================================================" echo # Check if DinD container exists if ! docker ps -a --format "{{.Names}}" | grep -q "^ci-cd-dind$"; then log_error "DinD container 'ci-cd-dind' not found!" log_info "Creating new DinD container..." if [ "$DRY_RUN" = "true" ]; then log_info "DRY RUN: Would create DinD container" return fi docker run -d \ --name ci-cd-dind \ --privileged \ --restart unless-stopped \ -p 2376:2376 \ -v ci-cd-data:/var/lib/docker \ -v /var/run/docker.sock:/var/run/docker.sock \ docker:dind log_success "DinD container created successfully" return fi # Check if DinD container is running if docker ps --format "{{.Names}}" | grep -q "^ci-cd-dind$"; then log_info "DinD container is running" if [ "$DRY_RUN" = "true" ]; then log_info "DRY RUN: Would stop and restart DinD container" log_info "DRY RUN: This would clear all CI/CD artifacts and give fresh environment" return fi log_info "Stopping DinD container..." docker stop ci-cd-dind log_info "Removing DinD container..." docker rm ci-cd-dind log_info "Creating fresh DinD container..." docker run -d \ --name ci-cd-dind \ --privileged \ --restart unless-stopped \ -p 2376:2376 \ -v ci-cd-data:/var/lib/docker \ -v /var/run/docker.sock:/var/run/docker.sock \ docker:dind # Wait for DinD to start log_info "Waiting for DinD to start..." sleep 10 # Test DinD connectivity if timeout 30 bash -c 'until docker exec ci-cd-dind docker version >/dev/null 2>&1; do sleep 1; done'; then log_success "DinD container is ready!" else log_error "DinD container failed to start properly" exit 1 fi else log_info "DinD container exists but is not running" if [ "$DRY_RUN" = "true" ]; then log_info "DRY RUN: Would remove and recreate DinD container" return fi log_info "Removing existing DinD container..." docker rm ci-cd-dind log_info "Creating fresh DinD container..." docker run -d \ --name ci-cd-dind \ --privileged \ --restart unless-stopped \ -p 2376:2376 \ -v ci-cd-data:/var/lib/docker \ -v /var/run/docker.sock:/var/run/docker.sock \ docker:dind # Wait for DinD to start log_info "Waiting for DinD to start..." sleep 10 # Test DinD connectivity if timeout 30 bash -c 'until docker exec ci-cd-dind docker version >/dev/null 2>&1; do sleep 1; done'; then log_success "DinD container is ready!" else log_error "DinD container failed to start properly" exit 1 fi fi echo echo "==================================================================================" log_success "DinD cleanup completed successfully!" echo "==================================================================================" echo log_info "Benefits of this cleanup:" log_info " โœ… Fresh Docker environment for CI/CD" log_info " โœ… No resource contention with Harbor" log_info " โœ… Clean state for Rust testing" log_info " โœ… Isolated CI/CD operations" echo } # Show current DinD status show_status() { echo "==================================================================================" echo " ๐Ÿ“Š DinD Status ๐Ÿ“Š" echo "==================================================================================" echo if docker ps -a --format "{{.Names}}" | grep -q "^ci-cd-dind$"; then log_info "DinD Container Status:" docker ps -a --filter "name=ci-cd-dind" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" echo if docker ps --format "{{.Names}}" | grep -q "^ci-cd-dind$"; then log_info "DinD Docker Info:" docker exec ci-cd-dind docker info --format "{{.ServerVersion}}" 2>/dev/null || log_warning "Cannot connect to DinD Docker daemon" echo log_info "DinD Images:" docker exec ci-cd-dind docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" 2>/dev/null || log_warning "Cannot list DinD images" echo log_info "DinD Containers:" docker exec ci-cd-dind docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Image}}" 2>/dev/null || log_warning "Cannot list DinD containers" else log_warning "DinD container is not running" fi else log_warning "DinD container does not exist" fi echo "==================================================================================" } # Main execution if [ "$DRY_RUN" = "true" ]; then echo echo "==================================================================================" echo " ๐Ÿšจ DRY RUN MODE ๐Ÿšจ" echo " No changes will be made" echo "==================================================================================" echo show_status cleanup_dind else show_status cleanup_dind fi