#!/bin/bash # LittleShop Docker Build Script # This script builds and optionally deploys LittleShop using Docker set -e echo "==========================================" echo "LittleShop Docker Build & Deploy Script" echo "==========================================" echo # Variables IMAGE_NAME="littleshop" IMAGE_TAG="latest" CONTAINER_NAME="littleshop" ENV_FILE=".env" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Functions print_success() { echo -e "${GREEN}✓${NC} $1" } print_warning() { echo -e "${YELLOW}⚠${NC} $1" } print_error() { echo -e "${RED}✗${NC} $1" } # Check for .env file if [ ! -f "$ENV_FILE" ]; then if [ -f ".env.production" ]; then echo "Creating .env from .env.production template..." cp .env.production .env print_success ".env file created from template" else print_error ".env file not found. Please create one from .env.production template" exit 1 fi else print_success ".env file found" fi # Parse command line arguments BUILD_ONLY=false PUSH_TO_REGISTRY=false REGISTRY_URL="" while [[ $# -gt 0 ]]; do case $1 in --build-only) BUILD_ONLY=true shift ;; --push) PUSH_TO_REGISTRY=true shift ;; --registry) REGISTRY_URL="$2" PUSH_TO_REGISTRY=true shift 2 ;; --help) echo "Usage: $0 [options]" echo "Options:" echo " --build-only Build the image without starting containers" echo " --push Push image to registry" echo " --registry URL Specify registry URL (implies --push)" echo " --help Show this help message" exit 0 ;; *) print_error "Unknown option: $1" exit 1 ;; esac done # Step 1: Build the Docker image echo echo "1. Building Docker image..." docker build -t ${IMAGE_NAME}:${IMAGE_TAG} . if [ $? -eq 0 ]; then print_success "Docker image built successfully: ${IMAGE_NAME}:${IMAGE_TAG}" else print_error "Docker build failed" exit 1 fi # Step 2: Push to registry if requested if [ "$PUSH_TO_REGISTRY" = true ]; then echo echo "2. Pushing to registry..." if [ ! -z "$REGISTRY_URL" ]; then FULL_IMAGE_NAME="${REGISTRY_URL}/${IMAGE_NAME}:${IMAGE_TAG}" docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${FULL_IMAGE_NAME} docker push ${FULL_IMAGE_NAME} print_success "Image pushed to ${FULL_IMAGE_NAME}" else docker push ${IMAGE_NAME}:${IMAGE_TAG} print_success "Image pushed to default registry" fi fi # Step 3: Run containers (unless build-only) if [ "$BUILD_ONLY" = false ]; then echo echo "3. Starting containers with Docker Compose..." # Stop existing containers docker-compose down 2>/dev/null || true # Start new containers docker-compose up -d if [ $? -eq 0 ]; then print_success "Containers started successfully" else print_error "Failed to start containers" exit 1 fi # Step 4: Wait for health check echo echo "4. Waiting for application to be healthy..." MAX_ATTEMPTS=30 ATTEMPT=0 while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do if docker exec ${CONTAINER_NAME} curl -f http://localhost:5000/api/catalog/products > /dev/null 2>&1; then print_success "Application is healthy!" break fi ATTEMPT=$((ATTEMPT + 1)) echo -n "." sleep 2 done if [ $ATTEMPT -eq $MAX_ATTEMPTS ]; then print_error "Health check failed after $MAX_ATTEMPTS attempts" echo "Check logs with: docker logs ${CONTAINER_NAME}" exit 1 fi # Step 5: Display status echo echo "5. Deployment Status:" echo "====================" docker-compose ps echo print_success "Deployment complete!" echo echo "Application is running at: http://localhost:5100" echo "Admin panel: http://localhost:5100/Admin" echo echo "Useful commands:" echo " View logs: docker logs -f ${CONTAINER_NAME}" echo " Stop containers: docker-compose down" echo " Restart: docker-compose restart" echo " Shell access: docker exec -it ${CONTAINER_NAME} /bin/bash" else print_success "Build complete (build-only mode)" fi echo echo "==========================================" echo "Build completed at $(date)" echo "==========================================="