build.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/bin/bash
  2. # Inventory Management System Docker Build Script
  3. # This script builds and deploys the complete inventory solution
  4. set -e
  5. echo "🚀 Building Inventory Management System..."
  6. # Load environment variables from .env file
  7. if [ -f .env ]; then
  8. echo "📝 Loading environment variables from .env..."
  9. export $(cat .env | grep -v '^#' | xargs)
  10. else
  11. echo "❌ .env file not found. Please copy .env.example to .env and configure it."
  12. exit 1
  13. fi
  14. # Check if Docker is installed
  15. if ! command -v docker &> /dev/null; then
  16. echo "❌ Docker is not installed. Please install Docker first."
  17. exit 1
  18. fi
  19. # Check if Docker Compose is installed
  20. if ! command -v docker-compose &> /dev/null; then
  21. echo "❌ Docker Compose is not installed. Please install Docker Compose first."
  22. exit 1
  23. fi
  24. # Validate required environment variables
  25. if [ -z "$DB_HOST" ] || [ "$DB_HOST" = "your-external-db-host" ]; then
  26. echo "❌ DB_HOST is not configured in .env. Please set your external database host."
  27. exit 1
  28. fi
  29. if [ -z "$DB_USER" ] || [ "$DB_USER" = "your-db-username" ]; then
  30. echo "❌ DB_USER is not configured in .env. Please set your database username."
  31. exit 1
  32. fi
  33. if [ -z "$DB_PASS" ] || [ "$DB_PASS" = "your-db-password" ]; then
  34. echo "❌ DB_PASS is not configured in .env. Please set your database password."
  35. exit 1
  36. fi
  37. # Create uploads directory if it doesn't exist
  38. if [ ! -d uploads ]; then
  39. echo "📁 Creating uploads directory..."
  40. mkdir -p uploads
  41. chmod 755 uploads
  42. fi
  43. # Check if Redis is already running on the specified port
  44. REDIS_PORT=${REDIS_PORT:-6379}
  45. REDIS_RUNNING=false
  46. REDIS_HOST="localhost"
  47. if command -v netstat &> /dev/null; then
  48. if netstat -tuln 2>/dev/null | grep -q ":$REDIS_PORT "; then
  49. echo "🔴 Redis is already running on port $REDIS_PORT"
  50. REDIS_RUNNING=true
  51. REDIS_HOST=$(netstat -tuln 2>/dev/null | grep ":$REDIS_PORT " | head -1 | awk '{print $4}' | cut -d':' -f1)
  52. if [ "$REDIS_HOST" = "0.0.0.0" ] || [ "$REDIS_HOST" = "::" ]; then
  53. REDIS_HOST="localhost"
  54. fi
  55. fi
  56. elif command -v ss &> /dev/null; then
  57. if ss -tuln 2>/dev/null | grep -q ":$REDIS_PORT "; then
  58. echo "🔴 Redis is already running on port $REDIS_PORT"
  59. REDIS_RUNNING=true
  60. REDIS_HOST=$(ss -tuln 2>/dev/null | grep ":$REDIS_PORT " | head -1 | awk '{print $4}' | cut -d':' -f1)
  61. if [ "$REDIS_HOST" = "0.0.0.0" ] || [ "$REDIS_HOST" = "::" ]; then
  62. REDIS_HOST="localhost"
  63. fi
  64. fi
  65. fi
  66. # Display configuration summary
  67. echo "📋 Configuration Summary:"
  68. echo " 🌐 Frontend Port: ${FRONTEND_PORT:-80}"
  69. echo " 🗄️ Database Host: $DB_HOST"
  70. echo " 🗄️ Database Port: ${DB_PORT:-3306}"
  71. echo " 🗄️ Database Name: ${DB_NAME:-inventory_db}"
  72. echo " 🗄️ Database User: $DB_USER"
  73. echo " 🔴 Redis Port: $REDIS_PORT ($([ "$REDIS_RUNNING" = true ] && echo "EXTERNAL" || echo "DOCKER"))"
  74. echo ""
  75. # Test environment variables and database connectivity
  76. echo "🔍 Testing environment variables and database connectivity..."
  77. if ! docker-compose run --rm inventory-app php /var/www/html/test-env.php; then
  78. echo "❌ Environment variables or database connection test failed."
  79. echo "💡 Make sure:"
  80. echo " - Environment variables are properly set in .env"
  81. echo " - Database server is running and accessible"
  82. echo " - Database '$DB_NAME' exists"
  83. echo " - User '$DB_USER' has proper permissions"
  84. echo " - Network allows connection from Docker container"
  85. echo ""
  86. echo "🔍 Debug: Check container logs for detailed error information"
  87. docker-compose logs inventory-app | tail -20
  88. exit 1
  89. fi
  90. # Build and start the application
  91. echo "🔨 Building Docker containers..."
  92. if [ "$REDIS_RUNNING" = true ]; then
  93. echo "🔴 Skipping Redis build (external Redis detected)"
  94. docker-compose build inventory-app
  95. else
  96. echo "🔨 Building all containers including Redis..."
  97. docker-compose build
  98. fi
  99. echo "🚀 Starting application..."
  100. if [ "$REDIS_RUNNING" = true ]; then
  101. echo "🔴 Starting application without Redis (using external Redis at $REDIS_HOST:$REDIS_PORT)"
  102. docker-compose up -d inventory-app
  103. else
  104. echo "🚀 Starting application with Redis..."
  105. docker-compose up -d
  106. fi
  107. # Wait a moment for services to start
  108. echo "⏳ Waiting for services to start..."
  109. sleep 10
  110. # Check if containers are running
  111. echo "🔍 Checking container status..."
  112. docker-compose ps
  113. # Show logs
  114. echo "📋 Showing application logs..."
  115. docker-compose logs inventory-app
  116. # Clean up unnecessary Docker images
  117. echo "🧹 Cleaning up unnecessary Docker images..."
  118. echo "🔍 Removing dangling images..."
  119. DANGLING_IMAGES=$(docker images -f "dangling=true" -q)
  120. if [ -n "$DANGLING_IMAGES" ]; then
  121. echo "🗑️ Removing $(echo "$DANGLING_IMAGES" | wc -l) dangling images..."
  122. docker rmi $DANGLING_IMAGES 2>/dev/null || true
  123. else
  124. echo "✅ No dangling images found"
  125. fi
  126. echo "🔍 Removing unused build cache..."
  127. docker builder prune -f --keep-storage 1GB 2>/dev/null || true
  128. echo "🔍 Removing unused images (keeping last 24 hours)..."
  129. # Remove images older than 24 hours that are not being used
  130. UNUSED_IMAGES=$(docker images --format "table {{.Repository}}:{{.Tag}}\t{{.CreatedAt}}\t{{.ID}}" | grep -v "REPOSITORY" | awk '$2 < "'$(date -d '24 hours ago' '+%Y-%m-%d %H:%M:%S')'" {print $3}' | head -10)
  131. if [ -n "$UNUSED_IMAGES" ]; then
  132. echo "🗑️ Removing old unused images..."
  133. echo "$UNUSED_IMAGES" | xargs docker rmi 2>/dev/null || true
  134. else
  135. echo "✅ No old unused images found"
  136. fi
  137. # Show cleanup results
  138. echo ""
  139. echo "📊 Docker cleanup summary:"
  140. echo " 📦 Images remaining: $(docker images -q | wc -l)"
  141. echo " 💾 Space used: $(docker system df --format "table {{.Type}}\t{{.TotalCount}}\t{{.Size}}" | grep -E "Images|Local Volumes" | awk '{printf " %-15s %3s %10s\n", $1, $2, $3}')"
  142. echo ""
  143. echo "✅ Build complete!"
  144. echo ""
  145. echo "🌐 Application is available at: http://localhost:${FRONTEND_PORT:-80}"
  146. echo "🔧 API endpoints are available at: http://localhost:${FRONTEND_PORT:-80}/api"
  147. echo "🗄️ Connected to database: $DB_HOST:${DB_PORT:-3306}/$DB_NAME"
  148. if [ "$REDIS_RUNNING" = true ]; then
  149. echo "🔴 Using external Redis: $REDIS_HOST:$REDIS_PORT"
  150. else
  151. echo "🔴 Using Docker Redis: localhost:$REDIS_PORT"
  152. fi
  153. echo ""
  154. echo "📝 Management Commands:"
  155. echo " 🛑 Stop application: docker-compose down"
  156. echo " 📋 View logs: docker-compose logs -f"
  157. echo " 🔄 Rebuild: docker-compose build --no-cache"
  158. echo " 🐚 Access container: docker-compose exec inventory-app bash"
  159. echo " 🧹 Full cleanup: docker system prune -a"
  160. if [ "$REDIS_RUNNING" = true ]; then
  161. echo " 🔴 Redis management: Use your external Redis tools"
  162. else
  163. echo " 🔴 Redis access: docker-compose exec redis redis-cli"
  164. fi