Sfoglia il codice sorgente

Fixing container build

svalavuo 1 giorno fa
parent
commit
1259d12b5d
3 ha cambiato i file con 87 aggiunte e 10 eliminazioni
  1. 3 0
      Dockerfile
  2. 51 8
      build.sh
  3. 33 2
      docker/apache.conf

+ 3 - 0
Dockerfile

@@ -48,6 +48,9 @@ COPY backend/ .
 # Install PHP dependencies
 RUN composer install --no-dev --optimize-autoloader
 
+# Copy environment file for PHP application
+COPY backend/.env.local ./backend/.env.local
+
 # Copy built frontend from builder stage
 COPY --from=frontend-builder /app/frontend/dist ./frontend/dist
 

+ 51 - 8
build.sh

@@ -7,6 +7,37 @@ set -e
 
 echo "🚀 Building Inventory Management System..."
 
+# Docker Compose wrapper function to handle threading errors
+docker_compose_safe() {
+    local max_attempts=3
+    local attempt=1
+    local cmd="$@"
+    
+    while [ $attempt -le $max_attempts ]; do
+        echo "🔄 Docker Compose attempt $attempt/$max_attempts: $cmd"
+        
+        if docker-compose $cmd 2>/dev/null; then
+            echo "✅ Docker Compose command succeeded"
+            return 0
+        else
+            echo "⚠️  Docker Compose command failed (attempt $attempt/$max_attempts)"
+            if [ $attempt -eq $max_attempts ]; then
+                echo "🔧 Trying without stderr redirection..."
+                if docker-compose $cmd; then
+                    echo "✅ Docker Compose command succeeded on retry"
+                    return 0
+                else
+                    echo "❌ Docker Compose command failed after $max_attempts attempts"
+                    return 1
+                fi
+            fi
+            sleep 2
+            ((attempt++))
+        fi
+    done
+    return 1
+}
+
 # Load environment variables from .env file
 if [ -f .env ]; then
     echo "📝 Loading environment variables from .env..."
@@ -51,6 +82,16 @@ if [ ! -d uploads ]; then
     chmod 755 uploads
 fi
 
+# Copy .env to backend/.env.local for PHP application
+echo "📝 Copying environment variables to backend/.env.local..."
+if [ -f .env ]; then
+    cp .env backend/.env.local
+    echo "✅ Environment variables copied to backend/.env.local"
+else
+    echo "❌ .env file not found. Cannot create backend/.env.local"
+    exit 1
+fi
+
 # Check if Redis is already running on the specified port
 REDIS_PORT=${REDIS_PORT:-6379}
 REDIS_RUNNING=false
@@ -88,7 +129,8 @@ echo ""
 
 # Test environment variables and database connectivity
 echo "🔍 Testing environment variables and database connectivity..."
-if ! docker-compose run --rm inventory-app php /var/www/html/test-env.php; then
+echo "ℹ️  Note: If you see Docker Compose threading errors, they can be safely ignored"
+if ! timeout 30 docker-compose run --rm inventory-app php /var/www/html/test-env.php 2>/dev/null; then
     echo "❌ Environment variables or database connection test failed."
     echo "💡 Make sure:"
     echo "   - Environment variables are properly set in .env"
@@ -98,27 +140,28 @@ if ! docker-compose run --rm inventory-app php /var/www/html/test-env.php; then
     echo "   - Network allows connection from Docker container"
     echo ""
     echo "🔍 Debug: Check container logs for detailed error information"
-    docker-compose logs inventory-app | tail -20
+    docker-compose logs inventory-app | tail -20 2>/dev/null || echo "Could not fetch logs"
     exit 1
 fi
 
 # Build and start the application
 echo "🔨 Building Docker containers..."
+echo "ℹ️  Note: Docker Compose threading errors can be safely ignored during build"
 if [ "$REDIS_RUNNING" = true ]; then
     echo "🔴 Skipping Redis build (external Redis detected)"
-    docker-compose build inventory-app
+    docker-compose build inventory-app 2>/dev/null || docker-compose build inventory-app
 else
     echo "🔨 Building all containers including Redis..."
-    docker-compose build
+    docker-compose build 2>/dev/null || docker-compose build
 fi
 
 echo "🚀 Starting application..."
 if [ "$REDIS_RUNNING" = true ]; then
     echo "🔴 Starting application without Redis (using external Redis at $REDIS_HOST:$REDIS_PORT)"
-    docker-compose up -d inventory-app
+    docker-compose up -d inventory-app 2>/dev/null || docker-compose up -d inventory-app
 else
     echo "🚀 Starting application with Redis..."
-    docker-compose up -d
+    docker-compose up -d 2>/dev/null || docker-compose up -d
 fi
 
 # Wait a moment for services to start
@@ -127,11 +170,11 @@ sleep 10
 
 # Check if containers are running
 echo "🔍 Checking container status..."
-docker-compose ps
+docker-compose ps 2>/dev/null || echo "⚠️  Could not get container status (Docker Compose threading error)"
 
 # Show logs
 echo "📋 Showing application logs..."
-docker-compose logs inventory-app
+docker-compose logs inventory-app 2>/dev/null || echo "⚠️  Could not fetch logs (Docker Compose threading error)"
 
 # Clean up unnecessary Docker images
 echo "🧹 Cleaning up unnecessary Docker images..."

+ 33 - 2
docker/apache.conf

@@ -23,9 +23,18 @@
 
     # Enable CORS for API endpoints
     <Directory "/var/www/html/api">
-        Header set Access-Control-Allow-Origin "*"
+        # Set CORS headers based on origin
+        SetEnvIfNoCase Origin "^https://inventory\.valavuo\.net$" CORS_ALLOW_ORIGIN
+        SetEnvIfNoCase Origin "^http://localhost(:[0-9]+)?$" CORS_ALLOW_ORIGIN
+        SetEnvIfNoCase Origin "^http://127\.0\.0\.1(:[0-9]+)?$" CORS_ALLOW_ORIGIN
+        
+        Header set Access-Control-Allow-Origin "%{CORS_ALLOW_ORIGIN}e" env=CORS_ALLOW_ORIGIN
         Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
         Header set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With"
+        Header set Access-Control-Allow-Credentials "true"
+        
+        # Fallback for development
+        Header set Access-Control-Allow-Origin "*" env=!CORS_ALLOW_ORIGIN
     </Directory>
 
     # Handle OPTIONS requests for CORS
@@ -34,9 +43,18 @@
     </FilesMatch>
 
     <IfModule mod_headers.c>
-        Header always set Access-Control-Allow-Origin "*" env=CORs
+        # Dynamic CORS based on origin
+        SetEnvIfNoCase Origin "^https://inventory\.valavuo\.net$" CORS_ALLOW_ORIGIN
+        SetEnvIfNoCase Origin "^http://localhost(:[0-9]+)?$" CORS_ALLOW_ORIGIN
+        SetEnvIfNoCase Origin "^http://127\.0\.0\.1(:[0-9]+)?$" CORS_ALLOW_ORIGIN
+        
+        Header always set Access-Control-Allow-Origin "%{CORS_ALLOW_ORIGIN}e" env=CORS_ALLOW_ORIGIN env=CORs
         Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" env=CORs
         Header always set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With" env=CORs
+        Header always set Access-Control-Allow-Credentials "true" env=CORs
+        
+        # Fallback for development
+        Header always set Access-Control-Allow-Origin "*" env=!CORS_ALLOW_ORIGIN env=CORs
     </IfModule>
 
     # API endpoint routing
@@ -48,6 +66,19 @@
         AllowOverride All
         Require all granted
         Options -Indexes
+        
+        # Set CORS headers based on origin
+        SetEnvIfNoCase Origin "^https://inventory\.valavuo\.net$" CORS_ALLOW_ORIGIN
+        SetEnvIfNoCase Origin "^http://localhost(:[0-9]+)?$" CORS_ALLOW_ORIGIN
+        SetEnvIfNoCase Origin "^http://127\.0\.0\.1(:[0-9]+)?$" CORS_ALLOW_ORIGIN
+        
+        Header set Access-Control-Allow-Origin "%{CORS_ALLOW_ORIGIN}e" env=CORS_ALLOW_ORIGIN
+        Header set Access-Control-Allow-Methods "GET, HEAD, OPTIONS"
+        Header set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With"
+        Header set Access-Control-Allow-Credentials "true"
+        
+        # Fallback for development
+        Header set Access-Control-Allow-Origin "*" env=!CORS_ALLOW_ORIGIN
     </Directory>
 
     ErrorLog ${APACHE_LOG_DIR}/error.log