The previous 5-second fixed wait was insufficient for MySQL 8.0 to fully initialize, causing migrations and seeders to fail with 'Connection refused'. Changes: - Replace 'sleep 5' with active connection polling - Try to connect every second for up to 30 seconds - Use PHP PDO to test actual database connectivity - Only proceed when database accepts connections - Show clear status when database is ready This ensures migrations and seeders run only after database is fully ready, preventing 'Connection refused' errors on fresh setup. Fixes the issue where Laravel loads but shows MySQL connection error.
246 lines
6.6 KiB
Batchfile
246 lines
6.6 KiB
Batchfile
@echo off
|
|
REM Laravel Docker Development Template - Quick Setup (Windows)
|
|
REM Everything is pre-installed! This just starts Docker and sets up the database.
|
|
REM Usage: setup.bat [mysql|pgsql|sqlite]
|
|
|
|
setlocal enabledelayedexpansion
|
|
|
|
set DB=%1
|
|
if "%DB%"=="" set DB=mysql
|
|
|
|
REM Validate database choice
|
|
if not "%DB%"=="mysql" if not "%DB%"=="pgsql" if not "%DB%"=="sqlite" (
|
|
echo Error: Invalid database '%DB%'
|
|
echo Usage: setup.bat [mysql^|pgsql^|sqlite]
|
|
echo Example: setup.bat mysql
|
|
exit /b 1
|
|
)
|
|
|
|
echo ========================================================
|
|
echo Laravel Docker Development Template
|
|
echo Quick Setup - Everything Pre-Installed!
|
|
echo ========================================================
|
|
echo.
|
|
echo Setting up with %DB%...
|
|
echo.
|
|
|
|
REM Step 1: Check port availability and auto-assign alternatives
|
|
echo Checking port availability...
|
|
|
|
REM Function to find next available port
|
|
REM Usage: call :find_port <start_port> <result_var>
|
|
goto :skip_functions
|
|
|
|
:find_port
|
|
set /a port=%~1
|
|
:find_port_loop
|
|
netstat -ano | findstr ":%port% " >nul 2>&1
|
|
if %errorlevel% equ 0 (
|
|
set /a port+=1
|
|
goto :find_port_loop
|
|
)
|
|
set %~2=%port%
|
|
exit /b 0
|
|
|
|
:skip_functions
|
|
|
|
REM Check and assign APP_PORT (Web Server)
|
|
netstat -ano | findstr ":8080" >nul 2>&1
|
|
if %errorlevel% equ 0 (
|
|
call :find_port 8081 APP_PORT
|
|
echo [!] Port 8080 in use - using !APP_PORT! for Web Server
|
|
) else (
|
|
set APP_PORT=8080
|
|
echo [OK] Port 8080 ^(Web Server^) - Available
|
|
)
|
|
|
|
REM Check and assign MAIL_DASHBOARD_PORT (Mailpit Dashboard)
|
|
netstat -ano | findstr ":8025" >nul 2>&1
|
|
if %errorlevel% equ 0 (
|
|
call :find_port 8026 MAIL_DASHBOARD_PORT
|
|
echo [!] Port 8025 in use - using !MAIL_DASHBOARD_PORT! for Mailpit Dashboard
|
|
) else (
|
|
set MAIL_DASHBOARD_PORT=8025
|
|
echo [OK] Port 8025 ^(Mailpit Dashboard^) - Available
|
|
)
|
|
|
|
REM Check and assign MAIL_PORT (Mailpit SMTP)
|
|
netstat -ano | findstr ":1025" >nul 2>&1
|
|
if %errorlevel% equ 0 (
|
|
call :find_port 1026 MAIL_PORT
|
|
echo [!] Port 1025 in use - using !MAIL_PORT! for Mailpit SMTP
|
|
) else (
|
|
set MAIL_PORT=1025
|
|
echo [OK] Port 1025 ^(Mailpit SMTP^) - Available
|
|
)
|
|
|
|
REM Check and assign REDIS_PORT
|
|
netstat -ano | findstr ":6379" >nul 2>&1
|
|
if %errorlevel% equ 0 (
|
|
call :find_port 6380 REDIS_PORT
|
|
echo [!] Port 6379 in use - using !REDIS_PORT! for Redis
|
|
) else (
|
|
set REDIS_PORT=6379
|
|
echo [OK] Port 6379 ^(Redis^) - Available
|
|
)
|
|
|
|
REM Check and assign DB_PORT based on database selection
|
|
if "%DB%"=="mysql" (
|
|
netstat -ano | findstr ":3306" >nul 2>&1
|
|
if !errorlevel! equ 0 (
|
|
call :find_port 3307 DB_PORT
|
|
echo [!] Port 3306 in use - using !DB_PORT! for MySQL
|
|
) else (
|
|
set DB_PORT=3306
|
|
echo [OK] Port 3306 ^(MySQL^) - Available
|
|
)
|
|
)
|
|
|
|
if "%DB%"=="pgsql" (
|
|
netstat -ano | findstr ":5432" >nul 2>&1
|
|
if !errorlevel! equ 0 (
|
|
call :find_port 5433 DB_PORT
|
|
echo [!] Port 5432 in use - using !DB_PORT! for PostgreSQL
|
|
) else (
|
|
set DB_PORT=5432
|
|
echo [OK] Port 5432 ^(PostgreSQL^) - Available
|
|
)
|
|
)
|
|
|
|
echo.
|
|
|
|
REM Step 2: Configure environment
|
|
echo Configuring environment...
|
|
if exist "src\.env.%DB%" (
|
|
copy /y "src\.env.%DB%" "src\.env" >nul
|
|
|
|
REM Append port configurations to .env
|
|
echo APP_PORT=%APP_PORT% >> src\.env
|
|
echo MAIL_DASHBOARD_PORT=%MAIL_DASHBOARD_PORT% >> src\.env
|
|
echo MAIL_PORT=%MAIL_PORT% >> src\.env
|
|
echo REDIS_PORT=%REDIS_PORT% >> src\.env
|
|
if defined DB_PORT echo DB_PORT=%DB_PORT% >> src\.env
|
|
|
|
echo Environment configured for %DB%
|
|
) else (
|
|
echo ERROR: Template file src\.env.%DB% not found
|
|
exit /b 1
|
|
)
|
|
|
|
REM Step 3: Build containers
|
|
echo Building Docker containers...
|
|
docker-compose build
|
|
echo Containers built
|
|
echo.
|
|
|
|
REM Step 4: Start containers
|
|
echo Starting Docker containers...
|
|
docker-compose --profile %DB% up -d
|
|
echo Containers started
|
|
echo.
|
|
|
|
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 6: Wait for database
|
|
if "%DB%"=="mysql" (
|
|
echo Waiting for MySQL to be ready...
|
|
set DB_READY=0
|
|
for /L %%i in (1,1,30) do (
|
|
docker-compose exec -T app php -r "new PDO('mysql:host=mysql;dbname=laravel', 'laravel', 'secret');" >nul 2>&1
|
|
if !errorlevel! equ 0 (
|
|
set DB_READY=1
|
|
goto :mysql_ready
|
|
)
|
|
timeout /t 1 /nobreak >nul
|
|
)
|
|
:mysql_ready
|
|
if !DB_READY! equ 1 (
|
|
echo MySQL ready
|
|
) else (
|
|
echo WARNING: MySQL may not be fully ready yet
|
|
)
|
|
echo.
|
|
)
|
|
if "%DB%"=="pgsql" (
|
|
echo Waiting for PostgreSQL to be ready...
|
|
set DB_READY=0
|
|
for /L %%i in (1,1,30) do (
|
|
docker-compose exec -T app php -r "new PDO('pgsql:host=pgsql;dbname=laravel', 'laravel', 'secret');" >nul 2>&1
|
|
if !errorlevel! equ 0 (
|
|
set DB_READY=1
|
|
goto :pgsql_ready
|
|
)
|
|
timeout /t 1 /nobreak >nul
|
|
)
|
|
:pgsql_ready
|
|
if !DB_READY! equ 1 (
|
|
echo PostgreSQL ready
|
|
) else (
|
|
echo WARNING: PostgreSQL may not be fully ready yet
|
|
)
|
|
echo.
|
|
)
|
|
|
|
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 8: Run migrations
|
|
echo Running database migrations...
|
|
docker-compose exec -T app php artisan migrate --force
|
|
echo Migrations completed
|
|
echo.
|
|
|
|
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 10: Create storage link
|
|
echo Creating storage symlink...
|
|
docker-compose exec -T app php artisan storage:link
|
|
echo Storage linked
|
|
echo.
|
|
|
|
REM Done
|
|
echo.
|
|
echo ========================================================
|
|
echo Setup Complete!
|
|
echo ========================================================
|
|
echo.
|
|
echo Your Laravel application is ready!
|
|
echo.
|
|
echo Laravel App: http://localhost:%APP_PORT%
|
|
echo Admin Panel: http://localhost:%APP_PORT%/admin
|
|
echo Mailpit: http://localhost:%MAIL_DASHBOARD_PORT%
|
|
echo.
|
|
echo Admin Login:
|
|
echo Email: admin@example.com
|
|
echo Password: password
|
|
echo.
|
|
echo What's Included:
|
|
echo - Laravel 11 with Breeze authentication
|
|
echo - Filament admin panel with user management
|
|
echo - Pest testing framework
|
|
echo - Laravel Pint code style
|
|
echo - Queue workers ^& scheduler (optional profiles)
|
|
echo.
|
|
echo Common Commands:
|
|
echo docker-compose exec app php artisan [command]
|
|
echo docker-compose exec app composer [command]
|
|
echo docker-compose exec app ./vendor/bin/pest
|
|
echo docker-compose logs -f app
|
|
echo.
|
|
echo Stop containers:
|
|
echo docker-compose down
|
|
echo.
|
|
|
|
endlocal
|