Files
Laravel-Docker-Dev-Template/setup.sh

229 lines
8.4 KiB
Bash

#!/bin/bash
# Laravel Docker Development Template - Quick Setup
# Everything is pre-installed! This just starts Docker and sets up the database.
# Usage: ./setup.sh [mysql|pgsql|sqlite]
set -e
DB=${1:-mysql}
VALID_DBS=("mysql" "pgsql" "sqlite")
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
echo -e "${GREEN}╔════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ Laravel Docker Development Template ║${NC}"
echo -e "${GREEN}║ Quick Setup - Everything Pre-Installed! ║${NC}"
echo -e "${GREEN}╚════════════════════════════════════════════════════════╝${NC}"
echo ""
# Validate database choice
if [[ ! " ${VALID_DBS[@]} " =~ " ${DB} " ]]; then
echo -e "${RED}Error: Invalid database '${DB}'${NC}"
echo "Usage: ./setup.sh [mysql|pgsql|sqlite]"
echo "Example: ./setup.sh mysql"
exit 1
fi
echo -e "${YELLOW}Setting up with ${DB}...${NC}"
echo ""
# Step 1: Check port availability and auto-assign alternatives
echo -e "${YELLOW}→ Checking port availability...${NC}"
# Function to find next available port
find_port() {
local port=$1
while lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":$port "; do
port=$((port + 1))
done
echo $port
}
# Check and assign APP_PORT (Web Server)
if lsof -Pi :8080 -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":8080 "; then
APP_PORT=$(find_port 8081)
echo -e "${YELLOW}[!] Port 8080 in use - using $APP_PORT for Web Server${NC}"
else
APP_PORT=8080
echo -e "${GREEN}[✓] Port 8080 (Web Server) - Available${NC}"
fi
# Check and assign MAIL_DASHBOARD_PORT (Mailpit Dashboard)
if lsof -Pi :8025 -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":8025 "; then
MAIL_DASHBOARD_PORT=$(find_port 8026)
echo -e "${YELLOW}[!] Port 8025 in use - using $MAIL_DASHBOARD_PORT for Mailpit Dashboard${NC}"
else
MAIL_DASHBOARD_PORT=8025
echo -e "${GREEN}[✓] Port 8025 (Mailpit Dashboard) - Available${NC}"
fi
# Check and assign MAIL_PORT (Mailpit SMTP)
if lsof -Pi :1025 -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":1025 "; then
MAIL_PORT=$(find_port 1026)
echo -e "${YELLOW}[!] Port 1025 in use - using $MAIL_PORT for Mailpit SMTP${NC}"
else
MAIL_PORT=1025
echo -e "${GREEN}[✓] Port 1025 (Mailpit SMTP) - Available${NC}"
fi
# Check and assign REDIS_PORT
if lsof -Pi :6379 -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":6379 "; then
REDIS_PORT=$(find_port 6380)
echo -e "${YELLOW}[!] Port 6379 in use - using $REDIS_PORT for Redis${NC}"
else
REDIS_PORT=6379
echo -e "${GREEN}[✓] Port 6379 (Redis) - Available${NC}"
fi
# Check and assign DB_PORT based on database selection
if [ "$DB" = "mysql" ]; then
if lsof -Pi :3306 -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":3306 "; then
DB_PORT=$(find_port 3307)
echo -e "${YELLOW}[!] Port 3306 in use - using $DB_PORT for MySQL${NC}"
else
DB_PORT=3306
echo -e "${GREEN}[✓] Port 3306 (MySQL) - Available${NC}"
fi
elif [ "$DB" = "pgsql" ]; then
if lsof -Pi :5432 -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":5432 "; then
DB_PORT=$(find_port 5433)
echo -e "${YELLOW}[!] Port 5432 in use - using $DB_PORT for PostgreSQL${NC}"
else
DB_PORT=5432
echo -e "${GREEN}[✓] Port 5432 (PostgreSQL) - Available${NC}"
fi
fi
echo ""
# Step 2: Configure environment
echo -e "${YELLOW}→ Configuring environment...${NC}"
if [ -f "src/.env.${DB}" ]; then
cp "src/.env.${DB}" "src/.env"
# Append port configurations to .env (for reference only, not used by Laravel)
# Note: DB_PORT is NOT appended - Laravel uses internal Docker port (3306/5432)
# These are for docker compose external port mapping only
echo "" >> src/.env
echo "# Port Configuration (auto-assigned by setup)" >> src/.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
echo -e "${GREEN}✓ Environment configured for ${DB}${NC}"
else
echo -e "${RED}✗ Template file src/.env.${DB} not found${NC}"
exit 1
fi
# Step 3: Build containers
echo -e "${YELLOW}→ Building Docker containers...${NC}"
docker compose build
echo -e "${GREEN}✓ Containers built${NC}"
echo ""
# 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 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 6: Wait for database
if [ "$DB" = "mysql" ]; then
echo -e "${YELLOW}→ Waiting for MySQL to be ready...${NC}"
for i in {1..30}; do
if docker compose exec -T app php -r "new PDO('mysql:host=mysql;dbname=laravel', 'laravel', 'secret');" >/dev/null 2>&1; then
echo -e "${GREEN}✓ MySQL ready${NC}"
break
fi
sleep 1
done
echo ""
elif [ "$DB" = "pgsql" ]; then
echo -e "${YELLOW}→ Waiting for PostgreSQL to be ready...${NC}"
for i in {1..30}; do
if docker compose exec -T app php -r "new PDO('pgsql:host=pgsql;dbname=laravel', 'laravel', 'secret');" >/dev/null 2>&1; then
echo -e "${GREEN}✓ PostgreSQL ready${NC}"
break
fi
sleep 1
done
echo ""
fi
# Step 7: Generate app key
echo -e "${YELLOW}→ Generating application key...${NC}"
docker compose exec app php artisan key:generate --force
echo -e "${GREEN}✓ App key generated${NC}"
echo ""
# Step 8: Run migrations
echo -e "${YELLOW}→ Running database migrations...${NC}"
docker compose exec app php artisan migrate --force
echo -e "${GREEN}✓ Migrations completed${NC}"
echo ""
# Step 9: Seed database (roles, permissions, admin user)
echo -e "${YELLOW}→ Seeding database (roles, permissions, admin user)...${NC}"
docker compose exec app php artisan db:seed --force
echo -e "${GREEN}✓ Database seeded${NC}"
echo ""
# Step 10: Create storage link
echo -e "${YELLOW}→ Creating storage symlink...${NC}"
docker compose exec app php artisan storage:link
echo -e "${GREEN}✓ Storage linked${NC}"
echo ""
# Step 11: Build frontend assets
echo -e "${YELLOW}→ Building frontend assets...${NC}"
docker compose run --rm node npm install >/dev/null 2>&1
docker compose run --rm node npm run build >/dev/null 2>&1
echo -e "${GREEN}✓ Frontend assets built${NC}"
echo ""
# Done!
echo ""
echo -e "${GREEN}╔════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ 🎉 Setup Complete! 🎉 ║${NC}"
echo -e "${GREEN}╚════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e "${GREEN}Your Laravel application is ready!${NC}"
echo ""
echo -e " 📱 Laravel App: ${GREEN}http://localhost:$APP_PORT${NC}"
echo -e " 🔐 Admin Panel: ${GREEN}http://localhost:$APP_PORT/admin${NC}"
echo -e " 📧 Mailpit: ${GREEN}http://localhost:$MAIL_DASHBOARD_PORT${NC}"
echo ""
echo -e "${YELLOW}Admin Login:${NC}"
echo -e " Email: admin@example.com"
echo -e " Password: password"
echo ""
echo -e "${YELLOW}What's Included:${NC}"
echo -e " ✓ Laravel 11 with Breeze authentication"
echo -e " ✓ Filament admin panel with user management"
echo -e " ✓ Pest testing framework"
echo -e " ✓ Laravel Pint code style"
echo -e " ✓ Queue workers & scheduler (optional profiles)"
echo ""
echo -e "${YELLOW}Common Commands:${NC}"
echo -e " docker compose exec app php artisan <command>"
echo -e " docker compose exec app composer <command>"
echo -e " docker compose exec app ./vendor/bin/pest"
echo -e " docker compose logs -f app"
echo ""
echo -e "${YELLOW}Stop containers:${NC}"
echo -e " docker compose down"
echo ""