#!/bin/bash # Base Image Pre-Pull Script for Bakery-IA # This script pre-pulls all required base images to reduce Docker Hub usage # Run this script before building services to cache base images locally set -e # Function to check if required tools are available check_required_tools() { local missing_tools=() # Check for required tools for tool in docker curl jq; do if ! command -v "$tool" &> /dev/null; then missing_tools+=("$tool") fi done if [ ${#missing_tools[@]} -gt 0 ]; then echo "Error: Missing required tools: ${missing_tools[*]}" echo "Please install them before running this script." echo "" echo "On macOS (with Homebrew):" echo " brew install docker curl jq" echo "" echo "On Ubuntu/Debian:" echo " sudo apt-get install docker.io curl jq" echo "" echo "On CentOS/RHEL:" echo " sudo yum install docker curl jq" exit 1 fi } # Check for required tools check_required_tools echo "==========================================" echo "Bakery-IA Base Image Pre-Pull Script" echo "==========================================" echo "" # Docker Hub credentials (use the same as in your Kubernetes setup) DOCKER_USERNAME="uals" DOCKER_PASSWORD="dckr_pat_zzEY5Q58x1S0puraIoKEtbpue3A" # Authenticate with Docker Hub echo "Authenticating with Docker Hub..." echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin echo "✓ Authentication successful" echo "" # Define all base images used in the project # All images are cached in local registry for dev environment BASE_IMAGES=( # Service base images "python:3.11-slim" # Database images "postgres:17-alpine" "redis:7.4-alpine" "rabbitmq:4.1-management-alpine" # Utility images "busybox:1.36" "curlimages/curl:latest" "bitnami/kubectl:latest" # Alpine variants "alpine:3.18" "alpine:3.19" "alpine/git:2.43.0" # CI/CD images "gcr.io/kaniko-project/executor:v1.23.0" "gcr.io/go-containerregistry/crane:latest" "registry.k8s.io/kustomize/kustomize:v5.3.0" # Storage images "minio/minio:RELEASE.2024-11-07T00-52-20Z" "minio/mc:RELEASE.2024-11-17T19-35-25Z" # Geocoding "mediagis/nominatim:4.4" # Mail server (Mailu - from GHCR) "ghcr.io/mailu/nginx:2024.06" "ghcr.io/mailu/admin:2024.06" "ghcr.io/mailu/postfix:2024.06" "ghcr.io/mailu/dovecot:2024.06" "ghcr.io/mailu/rspamd:2024.06" ) # Registry configuration # Read from environment variables (set by Tiltfile or manually) # USE_LOCAL_REGISTRY=true to push images to local registry after pulling USE_LOCAL_REGISTRY="${USE_LOCAL_REGISTRY:-true}" echo "Registry configuration:" echo " USE_LOCAL_REGISTRY=$USE_LOCAL_REGISTRY" echo "" # Use local registry (kind registry) REGISTRY="localhost:5000" # If using local registry, verify it's running if [ "$USE_LOCAL_REGISTRY" = "true" ]; then echo "Checking local registry at $REGISTRY..." if curl -s http://$REGISTRY/v2/ >/dev/null 2>&1; then echo "✓ Local registry is accessible" else echo "⚠ Local registry is not accessible at $REGISTRY" echo "Will only pull images locally (no registry push)" USE_LOCAL_REGISTRY="false" fi fi echo "" echo "Base images to pre-pull:" echo "----------------------------------------" for image in "${BASE_IMAGES[@]}"; do echo " - $image" done echo "" echo "Starting pre-pull process..." echo "----------------------------------------" # Track success/failure FAILED_IMAGES=() SUCCESS_COUNT=0 # Pull each base image for image in "${BASE_IMAGES[@]}"; do echo "Pulling: $image" # Pull the image if ! docker pull "$image"; then echo " ⚠ Failed to pull $image" FAILED_IMAGES+=("$image") continue fi # Tag for registry if enabled if [ "$USE_LOCAL_REGISTRY" = "true" ]; then # Local registry format: replace /, :, -, and . with _ local_repo="$(echo $image | sed 's|/|_|g' | sed 's|:|_|g' | sed 's|-|_|g' | sed 's|\.|_|g' | tr '[:upper:]' '[:lower:]')" registry_image="$REGISTRY/${local_repo}:latest" docker tag "$image" "$registry_image" echo " Tagged as: $registry_image" # Push to registry if docker push "$registry_image"; then echo " ✓ Pushed to local registry" else echo " ⚠ Failed to push to registry (image still available locally)" fi fi echo " ✓ Successfully pulled $image" SUCCESS_COUNT=$((SUCCESS_COUNT + 1)) echo "" done echo "==========================================" echo "Base Image Pre-Pull Complete!" echo "==========================================" echo "" echo "Summary:" echo " - Total images: ${#BASE_IMAGES[@]}" echo " - Successfully pulled: $SUCCESS_COUNT" if [ ${#FAILED_IMAGES[@]} -gt 0 ]; then echo " - Failed: ${#FAILED_IMAGES[@]}" echo " - Failed images: ${FAILED_IMAGES[*]}" fi if [ "$USE_LOCAL_REGISTRY" = "true" ]; then echo " - Registry: Local ($REGISTRY)" else echo " - Registry: None (local Docker only)" fi echo "" # Exit with error if any images failed if [ ${#FAILED_IMAGES[@]} -gt 0 ]; then echo "⚠ Some images failed to pull. This may be due to Docker Hub rate limits." echo "Please try again later or configure Docker Hub credentials." exit 1 fi echo "✓ All images pulled successfully!"