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
This commit is contained in:
2026-03-10 11:56:25 +02:00
parent 83131d8432
commit e7fcaa35e1
2 changed files with 153 additions and 17 deletions

View File

@@ -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