From e7fcaa35e1799e8e29e56fab2ccd7e24ce47eac5 Mon Sep 17 00:00:00 2001 From: theRADcozaDEV Date: Tue, 10 Mar 2026 11:56:25 +0200 Subject: [PATCH] Add port availability check to setup scripts Before starting Docker containers, setup scripts now check if required ports are available and warn users about conflicts. Features: - Checks all required ports: 8080, 8025, 1025, 6379 - Checks database port based on selection (3306 for MySQL, 5432 for PostgreSQL) - Shows clear status for each port (Available/In Use) - If conflicts found: - Displays warning message - Shows helpful commands to find and stop conflicting processes - Asks user if they want to continue anyway - Exits if user chooses not to continue Benefits: - Prevents confusing 'port already in use' errors during container startup - Provides clear diagnostic information - Gives users control over how to handle conflicts - Improves setup experience and reduces frustration Implementation: - setup.bat: Uses netstat with findstr for Windows - setup.sh: Uses lsof and netstat for Linux/Mac compatibility --- setup.bat | 101 +++++++++++++++++++++++++++++++++++++++++++++++++----- setup.sh | 69 ++++++++++++++++++++++++++++++++----- 2 files changed, 153 insertions(+), 17 deletions(-) diff --git a/setup.bat b/setup.bat index be0d4b2..41954d3 100644 --- a/setup.bat +++ b/setup.bat @@ -24,7 +24,90 @@ echo. echo Setting up with %DB%... echo. -REM Step 1: Configure environment +REM Step 1: Check port availability +echo Checking port availability... +set PORTS_OK=1 + +REM Check port 8080 (Nginx) +netstat -ano | findstr ":8080 " | findstr "LISTENING" >nul 2>&1 +if %errorlevel% equ 0 ( + echo [X] Port 8080 (Web Server) - IN USE + set PORTS_OK=0 +) else ( + echo [√] Port 8080 (Web Server) - Available +) + +REM Check port 8025 (Mailpit Dashboard) +netstat -ano | findstr ":8025 " | findstr "LISTENING" >nul 2>&1 +if %errorlevel% equ 0 ( + echo [X] Port 8025 (Mailpit Dashboard) - IN USE + set PORTS_OK=0 +) else ( + echo [√] Port 8025 (Mailpit Dashboard) - Available +) + +REM Check port 1025 (Mailpit SMTP) +netstat -ano | findstr ":1025 " | findstr "LISTENING" >nul 2>&1 +if %errorlevel% equ 0 ( + echo [X] Port 1025 (Mailpit SMTP) - IN USE + set PORTS_OK=0 +) else ( + echo [√] Port 1025 (Mailpit SMTP) - Available +) + +REM Check port 6379 (Redis) +netstat -ano | findstr ":6379 " | findstr "LISTENING" >nul 2>&1 +if %errorlevel% equ 0 ( + echo [X] Port 6379 (Redis) - IN USE + set PORTS_OK=0 +) else ( + echo [√] Port 6379 (Redis) - Available +) + +REM Check database port based on selection +if "%DB%"=="mysql" ( + netstat -ano | findstr ":3306 " | findstr "LISTENING" >nul 2>&1 + if !errorlevel! equ 0 ( + echo [X] Port 3306 (MySQL) - IN USE + set PORTS_OK=0 + ) else ( + echo [√] Port 3306 (MySQL) - Available + ) +) + +if "%DB%"=="pgsql" ( + netstat -ano | findstr ":5432 " | findstr "LISTENING" >nul 2>&1 + if !errorlevel! equ 0 ( + echo [X] Port 5432 (PostgreSQL) - IN USE + set PORTS_OK=0 + ) else ( + echo [√] Port 5432 (PostgreSQL) - Available + ) +) + +REM Handle port conflicts +if %PORTS_OK% equ 0 ( + echo. + echo WARNING: Some required ports are already in use! + echo. + echo To find what's using a port: + echo netstat -ano ^| findstr ":PORT" + echo. + echo To stop a process using a port: + echo 1. Find the PID from netstat output + echo 2. taskkill /PID [PID] /F + echo. + choice /C YN /M "Do you want to continue anyway" + if errorlevel 2 ( + echo Setup cancelled. + exit /b 1 + ) + echo. + echo Continuing with setup... +) +echo. + +REM Step 2: Configure environment echo Configuring environment... if exist "src\.env.%DB%" ( copy /y "src\.env.%DB%" "src\.env" >nul @@ -34,25 +117,25 @@ if exist "src\.env.%DB%" ( exit /b 1 ) -REM Step 2: Build containers +REM Step 3: Build containers echo Building Docker containers... docker-compose build echo Containers built echo. -REM Step 3: Start containers +REM Step 4: Start containers echo Starting Docker containers... docker-compose --profile %DB% up -d echo Containers started echo. -REM Step 4: Wait for app container to be ready +REM Step 5: Wait for app container to be ready echo Waiting for app container... timeout /t 3 /nobreak >nul echo App container ready echo. -REM Step 5: Wait for database +REM Step 6: Wait for database if "%DB%"=="mysql" ( echo Waiting for database... timeout /t 5 /nobreak >nul @@ -66,25 +149,25 @@ if "%DB%"=="pgsql" ( echo. ) -REM Step 6: Generate app key +REM Step 7: Generate app key echo Generating application key... docker-compose exec -T app php artisan key:generate --force echo App key generated echo. -REM Step 7: Run migrations +REM Step 8: Run migrations echo Running database migrations... docker-compose exec -T app php artisan migrate --force echo Migrations completed echo. -REM Step 8: Seed database +REM Step 9: Seed database echo Seeding database (roles, permissions, admin user)... docker-compose exec -T app php artisan db:seed --force echo Database seeded echo. -REM Step 9: Create storage link +REM Step 10: Create storage link echo Creating storage symlink... docker-compose exec -T app php artisan storage:link echo Storage linked diff --git a/setup.sh b/setup.sh index 5d9645d..c7cd1b8 100644 --- a/setup.sh +++ b/setup.sh @@ -32,7 +32,60 @@ fi echo -e "${YELLOW}Setting up with ${DB}...${NC}" echo "" -# Step 1: Configure environment +# Step 1: Check port availability +echo -e "${YELLOW}→ Checking port availability...${NC}" +PORTS_OK=1 + +# Function to check if port is in use +check_port() { + local port=$1 + local service=$2 + if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":$port "; then + echo -e "${RED}[✗] Port $port ($service) - IN USE${NC}" + PORTS_OK=0 + else + echo -e "${GREEN}[✓] Port $port ($service) - Available${NC}" + fi +} + +# Check required ports +check_port 8080 "Web Server" +check_port 8025 "Mailpit Dashboard" +check_port 1025 "Mailpit SMTP" +check_port 6379 "Redis" + +# Check database port based on selection +if [ "$DB" = "mysql" ]; then + check_port 3306 "MySQL" +elif [ "$DB" = "pgsql" ]; then + check_port 5432 "PostgreSQL" +fi + +# Handle port conflicts +if [ $PORTS_OK -eq 0 ]; then + echo "" + echo -e "${RED}WARNING: Some required ports are already in use!${NC}" + echo "" + echo "To find what's using a port:" + echo " lsof -i :PORT" + echo " netstat -tuln | grep :PORT" + echo "" + echo "To stop a process using a port:" + echo " 1. Find the PID: lsof -ti :PORT" + echo " 2. Kill the process: kill -9 PID" + echo "" + read -p "Do you want to continue anyway? (y/N): " -n 1 -r + echo "" + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + echo -e "${RED}Setup cancelled.${NC}" + exit 1 + fi + echo "" + echo -e "${YELLOW}Continuing with setup...${NC}" +fi +echo "" + +# Step 2: Configure environment echo -e "${YELLOW}→ Configuring environment...${NC}" if [ -f "src/.env.${DB}" ]; then cp "src/.env.${DB}" "src/.env" @@ -42,25 +95,25 @@ else exit 1 fi -# Step 2: Build containers +# Step 3: Build containers echo -e "${YELLOW}→ Building Docker containers...${NC}" docker-compose build echo -e "${GREEN}✓ Containers built${NC}" echo "" -# Step 3: Start containers +# Step 4: Start containers echo -e "${YELLOW}→ Starting Docker containers...${NC}" docker-compose --profile ${DB} up -d echo -e "${GREEN}✓ Containers started${NC}" echo "" -# Step 4: Install composer dependencies -echo -e "${YELLOW}→ Installing composer dependencies...${NC}" -docker-compose exec app composer install --no-interaction -echo -e "${GREEN}✓ Dependencies installed${NC}" +# Step 5: Wait for app container to be ready +echo -e "${YELLOW}→ Waiting for app container...${NC}" +sleep 3 +echo -e "${GREEN}✓ App container ready${NC}" echo "" -# Step 5: Wait for database +# Step 6: Wait for database if [ "$DB" = "mysql" ] || [ "$DB" = "pgsql" ]; then echo -e "${YELLOW}→ Waiting for database...${NC}" sleep 5