浏览代码

Fixing build -script

svalavuo 1 天之前
父节点
当前提交
497ff6dfd9
共有 1 个文件被更改,包括 85 次插入6 次删除
  1. 85 6
      build.sh

+ 85 - 6
build.sh

@@ -51,6 +51,31 @@ if [ ! -d uploads ]; then
     chmod 755 uploads
 fi
 
+# Check if Redis is already running on the specified port
+REDIS_PORT=${REDIS_PORT:-6379}
+REDIS_RUNNING=false
+REDIS_HOST="localhost"
+
+if command -v netstat &> /dev/null; then
+    if netstat -tuln 2>/dev/null | grep -q ":$REDIS_PORT "; then
+        echo "🔴 Redis is already running on port $REDIS_PORT"
+        REDIS_RUNNING=true
+        REDIS_HOST=$(netstat -tuln 2>/dev/null | grep ":$REDIS_PORT " | head -1 | awk '{print $4}' | cut -d':' -f1)
+        if [ "$REDIS_HOST" = "0.0.0.0" ] || [ "$REDIS_HOST" = "::" ]; then
+            REDIS_HOST="localhost"
+        fi
+    fi
+elif command -v ss &> /dev/null; then
+    if ss -tuln 2>/dev/null | grep -q ":$REDIS_PORT "; then
+        echo "🔴 Redis is already running on port $REDIS_PORT"
+        REDIS_RUNNING=true
+        REDIS_HOST=$(ss -tuln 2>/dev/null | grep ":$REDIS_PORT " | head -1 | awk '{print $4}' | cut -d':' -f1)
+        if [ "$REDIS_HOST" = "0.0.0.0" ] || [ "$REDIS_HOST" = "::" ]; then
+            REDIS_HOST="localhost"
+        fi
+    fi
+fi
+
 # Display configuration summary
 echo "📋 Configuration Summary:"
 echo "   🌐 Frontend Port: ${FRONTEND_PORT:-80}"
@@ -58,7 +83,7 @@ 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 "   🔴 Redis Port: $REDIS_PORT ($([ "$REDIS_RUNNING" = true ] && echo "EXTERNAL" || echo "DOCKER"))"
 echo ""
 
 # Test environment variables and database connectivity
@@ -79,10 +104,22 @@ fi
 
 # Build and start the application
 echo "🔨 Building Docker containers..."
-docker-compose build
+if [ "$REDIS_RUNNING" = true ]; then
+    echo "🔴 Skipping Redis build (external Redis detected)"
+    docker-compose build inventory-app
+else
+    echo "🔨 Building all containers including Redis..."
+    docker-compose build
+fi
 
 echo "🚀 Starting application..."
-docker-compose up -d
+if [ "$REDIS_RUNNING" = true ]; then
+    echo "🔴 Starting application without Redis (using external Redis at $REDIS_HOST:$REDIS_PORT)"
+    docker-compose up -d inventory-app
+else
+    echo "🚀 Starting application with Redis..."
+    docker-compose up -d
+fi
 
 # Wait a moment for services to start
 echo "⏳ Waiting for services to start..."
@@ -96,14 +133,56 @@ docker-compose ps
 echo "📋 Showing application logs..."
 docker-compose logs inventory-app
 
+# Clean up unnecessary Docker images
+echo "🧹 Cleaning up unnecessary Docker images..."
+echo "🔍 Removing dangling images..."
+DANGLING_IMAGES=$(docker images -f "dangling=true" -q)
+if [ -n "$DANGLING_IMAGES" ]; then
+    echo "🗑️  Removing $(echo "$DANGLING_IMAGES" | wc -l) dangling images..."
+    docker rmi $DANGLING_IMAGES 2>/dev/null || true
+else
+    echo "✅ No dangling images found"
+fi
+
+echo "🔍 Removing unused build cache..."
+docker builder prune -f --keep-storage 1GB 2>/dev/null || true
+
+echo "🔍 Removing unused images (keeping last 24 hours)..."
+# Remove images older than 24 hours that are not being used
+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)
+if [ -n "$UNUSED_IMAGES" ]; then
+    echo "🗑️  Removing old unused images..."
+    echo "$UNUSED_IMAGES" | xargs docker rmi 2>/dev/null || true
+else
+    echo "✅ No old unused images found"
+fi
+
+# Show cleanup results
+echo ""
+echo "📊 Docker cleanup summary:"
+echo "   📦 Images remaining: $(docker images -q | wc -l)"
+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}')"
+
+echo ""
 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"
+if [ "$REDIS_RUNNING" = true ]; then
+    echo "🔴 Using external Redis: $REDIS_HOST:$REDIS_PORT"
+else
+    echo "🔴 Using Docker Redis: localhost:$REDIS_PORT"
+fi
 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 "   🛑 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"
+echo "   🧹 Full cleanup: docker system prune -a"
+if [ "$REDIS_RUNNING" = true ]; then
+    echo "   🔴 Redis management: Use your external Redis tools"
+else
+    echo "   🔴 Redis access: docker-compose exec redis redis-cli"
+fi