#!/bin/bash # Inventory Management System Docker Build Script # This script builds and deploys the complete inventory solution set -e echo "🚀 Building Inventory Management System..." # Load environment variables from .env file if [ -f .env ]; then echo "📝 Loading environment variables from .env..." export $(cat .env | grep -v '^#' | xargs) else echo "❌ .env file not found. Please copy .env.example to .env and configure it." exit 1 fi # Check if Docker is installed if ! command -v docker &> /dev/null; then echo "❌ Docker is not installed. Please install Docker first." exit 1 fi # Check if Docker Compose is installed if ! command -v docker-compose &> /dev/null; then echo "❌ Docker Compose is not installed. Please install Docker Compose first." exit 1 fi # Validate required environment variables if [ -z "$DB_HOST" ] || [ "$DB_HOST" = "your-external-db-host" ]; then echo "❌ DB_HOST is not configured in .env. Please set your external database host." exit 1 fi if [ -z "$DB_USER" ] || [ "$DB_USER" = "your-db-username" ]; then echo "❌ DB_USER is not configured in .env. Please set your database username." exit 1 fi if [ -z "$DB_PASS" ] || [ "$DB_PASS" = "your-db-password" ]; then echo "❌ DB_PASS is not configured in .env. Please set your database password." exit 1 fi # Create uploads directory if it doesn't exist if [ ! -d uploads ]; then echo "📁 Creating uploads directory..." mkdir -p uploads chmod 755 uploads fi # Display configuration summary echo "📋 Configuration Summary:" echo " 🌐 Frontend Port: ${FRONTEND_PORT:-80}" echo " 🗄️ Database Host: $DB_HOST" echo " 🗄️ Database Port: ${DB_PORT:-3306}" echo " 🗄️ Database Name: ${DB_NAME:-inventory_db}" echo " 🗄️ Database User: $DB_USER" echo " 🔴 Redis Port: ${REDIS_PORT:-6379}" echo "" # Test environment variables and database connectivity echo "🔍 Testing environment variables and database connectivity..." if ! docker-compose run --rm inventory-app php test-env.php; then echo "❌ Environment variables or database connection test failed." echo "💡 Make sure:" echo " - Environment variables are properly set in .env" echo " - Database server is running and accessible" echo " - Database '$DB_NAME' exists" echo " - User '$DB_USER' has proper permissions" echo " - Network allows connection from Docker container" echo "" echo "🔍 Debug: Check container logs for detailed error information" docker-compose logs inventory-app | tail -20 exit 1 fi # Build and start the application echo "🔨 Building Docker containers..." docker-compose build echo "🚀 Starting application..." docker-compose up -d # Wait a moment for services to start echo "⏳ Waiting for services to start..." sleep 10 # Check if containers are running echo "🔍 Checking container status..." docker-compose ps # Show logs echo "📋 Showing application logs..." docker-compose logs inventory-app echo "✅ Build complete!" echo "" echo "🌐 Application is available at: http://localhost:${FRONTEND_PORT:-80}" echo "🔧 API endpoints are available at: http://localhost:${FRONTEND_PORT:-80}/api" echo "🗄️ Connected to database: $DB_HOST:${DB_PORT:-3306}/$DB_NAME" echo "" echo "📝 Management Commands:" echo " � Stop application: docker-compose down" echo " � View logs: docker-compose logs -f" echo " � Rebuild: docker-compose build --no-cache" echo " 🐚 Access container: docker-compose exec inventory-app bash"