build.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. # Display configuration summary
  44. echo "📋 Configuration Summary:"
  45. echo " 🌐 Frontend Port: ${FRONTEND_PORT:-80}"
  46. echo " 🗄️ Database Host: $DB_HOST"
  47. echo " 🗄️ Database Port: ${DB_PORT:-3306}"
  48. echo " 🗄️ Database Name: ${DB_NAME:-inventory_db}"
  49. echo " 🗄️ Database User: $DB_USER"
  50. echo " 🔴 Redis Port: ${REDIS_PORT:-6379}"
  51. echo ""
  52. # Test environment variables and database connectivity
  53. echo "🔍 Testing environment variables and database connectivity..."
  54. if ! docker-compose run --rm inventory-app php test-env.php; then
  55. echo "❌ Environment variables or database connection test failed."
  56. echo "💡 Make sure:"
  57. echo " - Environment variables are properly set in .env"
  58. echo " - Database server is running and accessible"
  59. echo " - Database '$DB_NAME' exists"
  60. echo " - User '$DB_USER' has proper permissions"
  61. echo " - Network allows connection from Docker container"
  62. echo ""
  63. echo "🔍 Debug: Check container logs for detailed error information"
  64. docker-compose logs inventory-app | tail -20
  65. exit 1
  66. fi
  67. # Build and start the application
  68. echo "🔨 Building Docker containers..."
  69. docker-compose build
  70. echo "🚀 Starting application..."
  71. docker-compose up -d
  72. # Wait a moment for services to start
  73. echo "⏳ Waiting for services to start..."
  74. sleep 10
  75. # Check if containers are running
  76. echo "🔍 Checking container status..."
  77. docker-compose ps
  78. # Show logs
  79. echo "📋 Showing application logs..."
  80. docker-compose logs inventory-app
  81. echo "✅ Build complete!"
  82. echo ""
  83. echo "🌐 Application is available at: http://localhost:${FRONTEND_PORT:-80}"
  84. echo "🔧 API endpoints are available at: http://localhost:${FRONTEND_PORT:-80}/api"
  85. echo "🗄️ Connected to database: $DB_HOST:${DB_PORT:-3306}/$DB_NAME"
  86. echo ""
  87. echo "📝 Management Commands:"
  88. echo " � Stop application: docker-compose down"
  89. echo " � View logs: docker-compose logs -f"
  90. echo " � Rebuild: docker-compose build --no-cache"
  91. echo " 🐚 Access container: docker-compose exec inventory-app bash"