#!/bin/bash # Docker Build and Test Script for Wall Color Visualizer API set -e # Exit on error echo "🐳 Wall Color Visualizer - Docker Build Script" echo "==============================================" echo "" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Function to print colored output print_success() { echo -e "${GREEN}✓ $1${NC}" } print_error() { echo -e "${RED}✗ $1${NC}" } print_warning() { echo -e "${YELLOW}⚠ $1${NC}" } print_info() { echo -e "ℹ $1" } # Parse command line arguments BUILD_TYPE="${1:-huggingface}" # Default to huggingface IMAGE_NAME="wall-color-api" CONTAINER_NAME="wall-color-api-test" case $BUILD_TYPE in "huggingface"|"hf") DOCKERFILE="Dockerfile" PORT=7860 print_info "Building for Hugging Face (port $PORT)" ;; "local") DOCKERFILE="Dockerfile.local" PORT=8000 print_info "Building for local development (port $PORT)" ;; "compose") print_info "Using Docker Compose" echo "" docker-compose up --build exit 0 ;; *) print_error "Unknown build type: $BUILD_TYPE" echo "Usage: $0 [huggingface|local|compose]" exit 1 ;; esac echo "" # Check if Docker is installed if ! command -v docker &> /dev/null; then print_error "Docker is not installed!" echo "Please install Docker first: https://docs.docker.com/get-docker/" exit 1 fi print_success "Docker is installed" # Check if Dockerfile exists if [ ! -f "$DOCKERFILE" ]; then print_error "Dockerfile '$DOCKERFILE' not found!" exit 1 fi print_success "Dockerfile found: $DOCKERFILE" echo "" print_info "Step 1: Building Docker image..." echo "" # Build the Docker image if docker build -f "$DOCKERFILE" -t "$IMAGE_NAME:$BUILD_TYPE" .; then print_success "Docker image built successfully!" else print_error "Docker build failed!" exit 1 fi echo "" print_info "Step 2: Stopping any existing containers..." # Stop and remove existing container if running if docker ps -a | grep -q "$CONTAINER_NAME"; then docker stop "$CONTAINER_NAME" 2>/dev/null || true docker rm "$CONTAINER_NAME" 2>/dev/null || true print_success "Cleaned up existing container" fi echo "" print_info "Step 3: Starting container..." echo "" # Run the container if docker run -d \ --name "$CONTAINER_NAME" \ -p "$PORT:$PORT" \ "$IMAGE_NAME:$BUILD_TYPE"; then print_success "Container started successfully!" else print_error "Failed to start container!" exit 1 fi echo "" print_info "Step 4: Waiting for API to be ready..." # Wait for the API to be ready MAX_ATTEMPTS=30 ATTEMPT=0 while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do if curl -s "http://localhost:$PORT/health" > /dev/null 2>&1; then print_success "API is ready!" break fi ATTEMPT=$((ATTEMPT + 1)) if [ $ATTEMPT -eq $MAX_ATTEMPTS ]; then print_error "API failed to start within 30 seconds" echo "" echo "Container logs:" docker logs "$CONTAINER_NAME" exit 1 fi echo -n "." sleep 1 done echo "" echo "" print_success "Deployment successful!" echo "" echo "==============================================" echo "📊 Container Information:" echo "==============================================" echo "Container Name: $CONTAINER_NAME" echo "Image: $IMAGE_NAME:$BUILD_TYPE" echo "" echo "🌐 Access URLs:" echo " - API Root: http://localhost:$PORT/" echo " - Health Check: http://localhost:$PORT/health" echo " - API Docs: http://localhost:$PORT/docs" echo "" echo "🔧 Useful Commands:" echo " - View logs: docker logs -f $CONTAINER_NAME" echo " - Stop: docker stop $CONTAINER_NAME" echo " - Remove: docker rm $CONTAINER_NAME" echo " - Shell access: docker exec -it $CONTAINER_NAME /bin/bash" echo "" echo "==============================================" # Test the API echo "" print_info "Running quick API test..." echo "" HEALTH_RESPONSE=$(curl -s "http://localhost:$PORT/health") echo "Health check response:" echo "$HEALTH_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$HEALTH_RESPONSE" echo "" print_success "All tests passed!" echo "" print_warning "Press Ctrl+C to stop viewing logs, container will keep running" echo "" # Follow logs docker logs -f "$CONTAINER_NAME"