Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# =============================================================================

# Complete local development setup (MAIN COMMAND)
local-setup: docker-cleanup docker-setup-env install-commit-tools setup-git-hooks
local-setup: docker-cleanup docker-setup-env check-ports install-commit-tools setup-git-hooks
@echo "🚀 SETUP: Complete local development environment..."
@echo ""
@echo "📦 Setting up Docker containers..."
Expand Down Expand Up @@ -65,6 +65,24 @@ release:
@echo "RELEASE: Creating release with release-please..."
npm run release

# =============================================================================
# Port Management
# =============================================================================

# Check port availability (standalone command)
check-ports-standalone:
@echo "🔍 PORTS: Checking port availability for Docker services..."
@bash containers/check-ports.sh
@echo ""
@echo "💡 TIP: Use 'make local-setup' to automatically check ports before setup"

# Check SonarQube port availability (standalone command)
check-sonarqube-ports-standalone:
@echo "🔍 SONARQUBE PORTS: Checking SonarQube port availability..."
@bash containers/check-sonarqube-ports.sh
@echo ""
@echo "💡 TIP: Use 'make sonarqube-setup' to automatically check ports before SonarQube setup"

# =============================================================================
# Docker Environment Management
# =============================================================================
Expand Down Expand Up @@ -96,6 +114,12 @@ docker-verify-env:
@echo "VERIFY: Docker environment setup..."
bash containers/verify-env-setup.sh

# Check port availability before Docker setup
check-ports:
@echo "🔍 PORTS: Checking port availability for Docker services..."
@bash containers/check-ports.sh || (echo "❌ Port check failed. Please resolve port conflicts before continuing." && exit 1)
@echo "✅ SUCCESS: All required ports are available!"

# =============================================================================
# Docker Development Environment
# =============================================================================
Expand Down Expand Up @@ -235,7 +259,7 @@ health:
# =============================================================================

# Complete SonarQube setup and analysis
sonarqube-setup: sonarqube-setup-env sonarqube-start
sonarqube-setup: sonarqube-setup-env check-sonarqube-ports sonarqube-start
@echo "🔍 SONARQUBE: Complete setup and analysis..."
@echo "⏳ SonarQube is starting up... This may take a few minutes."
@echo "📊 SonarQube will be available at: http://localhost:9000"
Expand All @@ -258,6 +282,12 @@ sonarqube-stop:
cd containers && docker-compose -f docker-compose.sonarqube.yml down
@echo "SUCCESS: SonarQube server stopped!"

# Check SonarQube port availability
check-sonarqube-ports:
@echo "🔍 SONARQUBE PORTS: Checking port availability..."
@bash containers/check-sonarqube-ports.sh || (echo "⚠️ SonarQube port check failed. You can continue without SonarQube or resolve port conflicts." && read -p "Continue anyway? (y/N): " confirm && [ "$$confirm" = "y" ] || [ "$$confirm" = "Y" ] || exit 1)
@echo "✅ SUCCESS: SonarQube ports are available!"

# Setup SonarQube environment and token
sonarqube-setup-token:
@echo "SONARQUBE: Setting up SonarQube environment and token..."
Expand Down
51 changes: 49 additions & 2 deletions containers/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
# Docker Setup for Laravel Blog API

This directory contains all Docker configuration files for local development and testing of the Laravel Blog API project.
This directory contains all Docker configuration files for local dev### 🚀 Main Setup Commands
```bash
make local-setup # Complete local development setup (MAIN COMMAND)
make sonarqube-setup # Optional SonarQube setup
```

### 🔍 Port Management
```bash
make check-ports-standalone # Check all required ports availability
make check-sonarqube-ports-standalone # Check SonarQube ports availability
```

### 🐳 Container Management
```bash
make docker-up # Start containers only (no setup)
make docker-down # Stop all containers
make status # Container status and access URLs
make health # Check application health
make logs # View all container logs
make shell # Access main container shell
```sting of the Laravel Blog API project.

## � Quick Setup

Expand All @@ -19,7 +39,8 @@ make local-setup

This automated setup will:
- 🧹 Clean up any existing containers and images
- 🔑 **Auto-generate unique APP_KEY** for all environments
- � **Check port availability** and warn of conflicts
- �🔑 **Auto-generate unique APP_KEY** for all environments
- 📝 **Copy and configure** all environment files automatically
- 🐳 Start both **main AND testing** environments
- 📦 Install Composer dependencies
Expand All @@ -28,6 +49,32 @@ This automated setup will:
- 🛠️ Install Git hooks and semantic commit tools
- ✅ Provide complete development and testing setup

### Port Conflict Prevention

**Before starting Docker containers, the setup automatically checks port availability:**

```bash
# Check ports for main setup (included in local-setup)
make check-ports-standalone

# Check ports for SonarQube (included in sonarqube-setup)
make check-sonarqube-ports-standalone
```

**Required ports that must be available:**
- `8081` - Laravel API (main application)
- `8001` - Xdebug debugging port
- `3306` - MySQL database
- `6379` - Redis cache
- `3307` - MySQL test database
- `6380` - Redis test cache

**Optional ports (SonarQube):**
- `9000` - SonarQube web interface
- `5432` - PostgreSQL (SonarQube database)

**If ports are in use:** The script will suggest alternative ports and show you exactly how to modify the docker-compose files.

### Optional: SonarQube Code Quality Analysis

```bash
Expand Down
185 changes: 185 additions & 0 deletions containers/check-ports.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#!/usr/bin/env bash

# =============================================================================
# Port Availability Checker for Laravel Blog API Docker Setup
# =============================================================================

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Ports used by the application (port:description format)
REQUIRED_PORTS=(
"8081:Laravel API (Main App)"
"8001:Xdebug Port"
"3306:MySQL Database"
"6379:Redis Cache"
"3307:MySQL Test Database"
"6380:Redis Test Cache"
)

OPTIONAL_PORTS=(
"9000:SonarQube Web Interface"
"5432:PostgreSQL (SonarQube)"
)

# Function to check if a port is available
check_port() {
local port=$1
local description=$2
local optional=${3:-false}

if nc -z localhost $port 2>/dev/null; then
if [ "$optional" = true ]; then
echo -e "${YELLOW}⚠️ OPTIONAL PORT $port ($description) is already in use${NC}"
else
echo -e "${RED}❌ PORT $port ($description) is already in use${NC}"
return 1
fi
else
if [ "$optional" = true ]; then
echo -e "${GREEN}✅ OPTIONAL PORT $port ($description) is available${NC}"
else
echo -e "${GREEN}✅ PORT $port ($description) is available${NC}"
fi
fi
return 0
}

# Function to suggest alternative ports
suggest_alternative_port() {
local base_port=$1
local description=$2
local start_range=$((base_port + 1))
local end_range=$((base_port + 100))

echo -e "${BLUE}💡 Searching for alternative ports for $description...${NC}"

for ((port=start_range; port<=end_range; port++)); do
if ! nc -z localhost $port 2>/dev/null; then
echo -e "${GREEN} Suggested alternative: $port${NC}"
return 0
fi
done

echo -e "${YELLOW} No alternatives found in range $start_range-$end_range${NC}"
return 1
}

# Function to show port change instructions
show_port_change_instructions() {
local port=$1
local description=$2
local suggested_port=$3

echo -e "${BLUE}📝 To change port $port ($description) to $suggested_port:${NC}"

case $port in
8081)
echo " Update containers/docker-compose.yml: ports: \"$suggested_port:80\""
;;
8001)
echo " Update containers/docker-compose.yml: ports: \"$suggested_port:8001\""
;;
3306)
echo " Update containers/docker-compose.yml: ports: \"$suggested_port:3306\""
;;
6379)
echo " Update containers/docker-compose.yml: ports: \"$suggested_port:6379\""
;;
3307)
echo " Update containers/docker-compose.test.yml: ports: \"$suggested_port:3306\""
;;
6380)
echo " Update containers/docker-compose.test.yml: ports: \"$suggested_port:6379\""
;;
9000)
echo " Update containers/docker-compose.sonarqube.yml: ports: \"$suggested_port:9000\""
;;
5432)
echo " Update containers/docker-compose.sonarqube.yml: ports: \"$suggested_port:5432\""
;;
esac
echo ""
}

# Main function
main() {
echo -e "${BLUE}🔍 Checking port availability for Laravel Blog API Docker setup...${NC}"
echo ""

local unavailable_ports=()
local failed_required=false

# Check required ports
echo -e "${BLUE}📋 Checking required ports:${NC}"
for port_info in "${REQUIRED_PORTS[@]}"; do
local port="${port_info%%:*}"
local description="${port_info#*:}"
if ! check_port "$port" "$description"; then
unavailable_ports+=("$port_info")
failed_required=true
fi
done

echo ""

# Check optional ports (SonarQube)
echo -e "${BLUE}📋 Checking optional ports (SonarQube):${NC}"
for port_info in "${OPTIONAL_PORTS[@]}"; do
local port="${port_info%%:*}"
local description="${port_info#*:}"
check_port "$port" "$description" true
done

echo ""

# Summary and recommendations
if [ "$failed_required" = true ]; then
echo -e "${RED}❌ SETUP CANNOT CONTINUE - Some required ports are unavailable!${NC}"
echo ""
echo -e "${YELLOW}🔧 SOLUTIONS:${NC}"
echo "1. Stop the services using these ports"
echo "2. Use alternative ports (see suggestions below)"
echo "3. Modify docker-compose files with new ports"
echo ""

echo -e "${BLUE}💡 PORT ALTERNATIVES:${NC}"
for port_info in "${unavailable_ports[@]}"; do
local port="${port_info%%:*}"
local description="${port_info#*:}"

# Find suggested alternative
local start_range=$((port + 1))
for ((alt_port=start_range; alt_port<=start_range+50; alt_port++)); do
if ! nc -z localhost $alt_port 2>/dev/null; then
show_port_change_instructions "$port" "$description" "$alt_port"
break
fi
done
done

echo -e "${YELLOW}⚠️ After making changes, run this script again to verify.${NC}"
return 1
else
echo -e "${GREEN}✅ ALL REQUIRED PORTS ARE AVAILABLE!${NC}"
echo -e "${GREEN}🚀 Docker setup can proceed safely.${NC}"
return 0
fi
}

# Check if netcat is available
if ! command -v nc &> /dev/null; then
echo -e "${RED}❌ Error: 'nc' (netcat) is required but not installed.${NC}"
echo "Please install netcat:"
echo " macOS: brew install netcat"
echo " Ubuntu/Debian: sudo apt-get install netcat"
echo " CentOS/RHEL: sudo yum install nc"
exit 1
fi

# Run the main function
main "$@"
Loading