#!/bin/bash # # Docker Registry Cache Setup Script # Sets up and configures Docker registry cache for faster builds and deployments # set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo -e "${GREEN}========================================${NC}" echo -e "${GREEN}Docker Registry Cache Setup${NC}" echo -e "${GREEN}========================================${NC}" # Function to check if service is running check_service() { local service=$1 if docker ps --format "table {{.Names}}" | grep -q "$service"; then echo -e "${GREEN}✓${NC} $service is running" return 0 else echo -e "${RED}✗${NC} $service is not running" return 1 fi } # Function to wait for service to be ready wait_for_service() { local service=$1 local url=$2 local max_attempts=30 local attempt=0 echo -n "Waiting for $service to be ready..." while [ $attempt -lt $max_attempts ]; do if curl -s -f "$url" > /dev/null 2>&1; then echo -e " ${GREEN}Ready!${NC}" return 0 fi echo -n "." sleep 2 attempt=$((attempt + 1)) done echo -e " ${RED}Timeout!${NC}" return 1 } # 1. Start Registry Cache echo -e "\n${YELLOW}1. Starting Registry Cache Service...${NC}" docker-compose -f docker-compose-registry-cache.yml up -d registry-cache # 2. Wait for registry to be ready wait_for_service "Registry Cache" "http://localhost:5000/v2/" # 3. Configure Docker daemon to use registry cache echo -e "\n${YELLOW}2. Configuring Docker daemon...${NC}" # Create daemon.json configuration cat > /tmp/daemon.json.tmp < Docker Engine" echo "3. Add the following configuration:" cat /tmp/daemon.json.tmp echo -e "\n4. Click 'Apply & Restart'" echo -e "\n${YELLOW}Press Enter when Docker Desktop has been configured...${NC}" read elif [[ "$OSTYPE" == "linux-gnu"* ]]; then # Linux - direct configuration echo "Configuring Docker daemon for Linux..." # Backup existing configuration if [ -f /etc/docker/daemon.json ]; then sudo cp /etc/docker/daemon.json /etc/docker/daemon.json.backup echo "Backed up existing daemon.json to daemon.json.backup" fi # Apply new configuration sudo cp /tmp/daemon.json.tmp /etc/docker/daemon.json # Restart Docker echo "Restarting Docker daemon..." sudo systemctl restart docker echo -e "${GREEN}Docker daemon configured and restarted${NC}" fi # 4. Test registry cache echo -e "\n${YELLOW}3. Testing Registry Cache...${NC}" # Pull a test image through cache echo "Pulling test image (alpine) through cache..." docker pull alpine:latest # Check if image is cached echo -e "\nChecking cached images..." curl -s http://localhost:5000/v2/_catalog | python3 -m json.tool || echo "No cached images yet" # 5. Configure buildx for multi-platform builds with cache echo -e "\n${YELLOW}4. Configuring Docker Buildx with cache...${NC}" # Create buildx builder with registry cache docker buildx create \ --name site11-builder \ --driver docker-container \ --config /dev/stdin < scripts/build-with-cache.sh <<'SCRIPT' #!/bin/bash # # Build script optimized for registry cache # SERVICE=$1 if [ -z "$SERVICE" ]; then echo "Usage: $0 " exit 1 fi echo "Building $SERVICE with cache optimization..." # Build with cache mount and registry cache docker buildx build \ --cache-from type=registry,ref=localhost:5000/site11-$SERVICE:cache \ --cache-to type=registry,ref=localhost:5000/site11-$SERVICE:cache,mode=max \ --platform linux/amd64 \ --tag site11-$SERVICE:latest \ --tag localhost:5000/site11-$SERVICE:latest \ --push \ -f services/$SERVICE/Dockerfile \ services/$SERVICE echo "Build complete for $SERVICE" SCRIPT chmod +x scripts/build-with-cache.sh # 7. Create cache warming script echo -e "\n${YELLOW}6. Creating cache warming script...${NC}" cat > scripts/warm-cache.sh <<'WARMSCRIPT' #!/bin/bash # # Warm up registry cache with commonly used base images # echo "Warming up registry cache..." # Base images used in the project IMAGES=( "python:3.11-slim" "node:18-alpine" "nginx:alpine" "redis:7-alpine" "mongo:7.0" "zookeeper:3.9" "bitnami/kafka:3.5" ) for image in "${IMAGES[@]}"; do echo "Caching $image..." docker pull "$image" docker tag "$image" "localhost:5000/$image" docker push "localhost:5000/$image" done echo "Cache warming complete!" WARMSCRIPT chmod +x scripts/warm-cache.sh # 8. Create registry management script echo -e "\n${YELLOW}7. Creating registry management script...${NC}" cat > scripts/manage-registry.sh <<'MANAGE' #!/bin/bash # # Registry cache management utilities # case "$1" in status) echo "Registry Cache Status:" curl -s http://localhost:5000/v2/_catalog | python3 -m json.tool ;; size) echo "Registry Cache Size:" docker exec site11_registry_cache du -sh /var/lib/registry ;; clean) echo "Running garbage collection..." docker exec site11_registry_cache registry garbage-collect /etc/docker/registry/config.yml ;; logs) docker logs -f site11_registry_cache ;; *) echo "Usage: $0 {status|size|clean|logs}" exit 1 ;; esac MANAGE chmod +x scripts/manage-registry.sh # 9. Summary echo -e "\n${GREEN}========================================${NC}" echo -e "${GREEN}Registry Cache Setup Complete!${NC}" echo -e "${GREEN}========================================${NC}" echo -e "\n${YELLOW}Available commands:${NC}" echo " - scripts/build-with-cache.sh # Build with cache" echo " - scripts/warm-cache.sh # Pre-cache base images" echo " - scripts/manage-registry.sh status # Check cache status" echo " - scripts/manage-registry.sh size # Check cache size" echo " - scripts/manage-registry.sh clean # Clean cache" echo -e "\n${YELLOW}Registry endpoints:${NC}" echo " - Registry: http://localhost:5000" echo " - Catalog: http://localhost:5000/v2/_catalog" echo " - Health: http://localhost:5000/v2/" echo -e "\n${YELLOW}Next steps:${NC}" echo "1. Run './scripts/warm-cache.sh' to pre-cache base images" echo "2. Use './scripts/build-with-cache.sh ' for faster builds" echo "3. Monitor cache with './scripts/manage-registry.sh status'" # Optional: Warm cache immediately read -p "Would you like to warm the cache now? (y/n) " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then ./scripts/warm-cache.sh fi