Skip to content

Commit 00522a0

Browse files
committed
feat: added port checker warning for local setup
1 parent 05c68e4 commit 00522a0

File tree

4 files changed

+409
-4
lines changed

4 files changed

+409
-4
lines changed

Makefile

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# =============================================================================
44

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

68+
# =============================================================================
69+
# Port Management
70+
# =============================================================================
71+
72+
# Check port availability (standalone command)
73+
check-ports-standalone:
74+
@echo "🔍 PORTS: Checking port availability for Docker services..."
75+
@bash containers/check-ports.sh
76+
@echo ""
77+
@echo "💡 TIP: Use 'make local-setup' to automatically check ports before setup"
78+
79+
# Check SonarQube port availability (standalone command)
80+
check-sonarqube-ports-standalone:
81+
@echo "🔍 SONARQUBE PORTS: Checking SonarQube port availability..."
82+
@bash containers/check-sonarqube-ports.sh
83+
@echo ""
84+
@echo "💡 TIP: Use 'make sonarqube-setup' to automatically check ports before SonarQube setup"
85+
6886
# =============================================================================
6987
# Docker Environment Management
7088
# =============================================================================
@@ -96,6 +114,12 @@ docker-verify-env:
96114
@echo "VERIFY: Docker environment setup..."
97115
bash containers/verify-env-setup.sh
98116

117+
# Check port availability before Docker setup
118+
check-ports:
119+
@echo "🔍 PORTS: Checking port availability for Docker services..."
120+
@bash containers/check-ports.sh || (echo "❌ Port check failed. Please resolve port conflicts before continuing." && exit 1)
121+
@echo "✅ SUCCESS: All required ports are available!"
122+
99123
# =============================================================================
100124
# Docker Development Environment
101125
# =============================================================================
@@ -235,7 +259,7 @@ health:
235259
# =============================================================================
236260

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

285+
# Check SonarQube port availability
286+
check-sonarqube-ports:
287+
@echo "🔍 SONARQUBE PORTS: Checking port availability..."
288+
@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)
289+
@echo "✅ SUCCESS: SonarQube ports are available!"
290+
261291
# Setup SonarQube environment and token
262292
sonarqube-setup-token:
263293
@echo "SONARQUBE: Setting up SonarQube environment and token..."

containers/README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
# Docker Setup for Laravel Blog API
22

3-
This directory contains all Docker configuration files for local development and testing of the Laravel Blog API project.
3+
This directory contains all Docker configuration files for local dev### 🚀 Main Setup Commands
4+
```bash
5+
make local-setup # Complete local development setup (MAIN COMMAND)
6+
make sonarqube-setup # Optional SonarQube setup
7+
```
8+
9+
### 🔍 Port Management
10+
```bash
11+
make check-ports-standalone # Check all required ports availability
12+
make check-sonarqube-ports-standalone # Check SonarQube ports availability
13+
```
14+
15+
### 🐳 Container Management
16+
```bash
17+
make docker-up # Start containers only (no setup)
18+
make docker-down # Stop all containers
19+
make status # Container status and access URLs
20+
make health # Check application health
21+
make logs # View all container logs
22+
make shell # Access main container shell
23+
```sting of the Laravel Blog API project.
424
525
## � Quick Setup
626
@@ -19,7 +39,8 @@ make local-setup
1939

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

52+
### Port Conflict Prevention
53+
54+
**Before starting Docker containers, the setup automatically checks port availability:**
55+
56+
```bash
57+
# Check ports for main setup (included in local-setup)
58+
make check-ports-standalone
59+
60+
# Check ports for SonarQube (included in sonarqube-setup)
61+
make check-sonarqube-ports-standalone
62+
```
63+
64+
**Required ports that must be available:**
65+
- `8081` - Laravel API (main application)
66+
- `8001` - Xdebug debugging port
67+
- `3306` - MySQL database
68+
- `6379` - Redis cache
69+
- `3307` - MySQL test database
70+
- `6380` - Redis test cache
71+
72+
**Optional ports (SonarQube):**
73+
- `9000` - SonarQube web interface
74+
- `5432` - PostgreSQL (SonarQube database)
75+
76+
**If ports are in use:** The script will suggest alternative ports and show you exactly how to modify the docker-compose files.
77+
3178
### Optional: SonarQube Code Quality Analysis
3279

3380
```bash

containers/check-ports.sh

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
#!/usr/bin/env bash
2+
3+
# =============================================================================
4+
# Port Availability Checker for Laravel Blog API Docker Setup
5+
# =============================================================================
6+
7+
# Colors for output
8+
RED='\033[0;31m'
9+
GREEN='\033[0;32m'
10+
YELLOW='\033[1;33m'
11+
BLUE='\033[0;34m'
12+
NC='\033[0m' # No Color
13+
14+
# Ports used by the application (port:description format)
15+
REQUIRED_PORTS=(
16+
"8081:Laravel API (Main App)"
17+
"8001:Xdebug Port"
18+
"3306:MySQL Database"
19+
"6379:Redis Cache"
20+
"3307:MySQL Test Database"
21+
"6380:Redis Test Cache"
22+
)
23+
24+
OPTIONAL_PORTS=(
25+
"9000:SonarQube Web Interface"
26+
"5432:PostgreSQL (SonarQube)"
27+
)
28+
29+
# Function to check if a port is available
30+
check_port() {
31+
local port=$1
32+
local description=$2
33+
local optional=${3:-false}
34+
35+
if nc -z localhost $port 2>/dev/null; then
36+
if [ "$optional" = true ]; then
37+
echo -e "${YELLOW}⚠️ OPTIONAL PORT $port ($description) is already in use${NC}"
38+
else
39+
echo -e "${RED}❌ PORT $port ($description) is already in use${NC}"
40+
return 1
41+
fi
42+
else
43+
if [ "$optional" = true ]; then
44+
echo -e "${GREEN}✅ OPTIONAL PORT $port ($description) is available${NC}"
45+
else
46+
echo -e "${GREEN}✅ PORT $port ($description) is available${NC}"
47+
fi
48+
fi
49+
return 0
50+
}
51+
52+
# Function to suggest alternative ports
53+
suggest_alternative_port() {
54+
local base_port=$1
55+
local description=$2
56+
local start_range=$((base_port + 1))
57+
local end_range=$((base_port + 100))
58+
59+
echo -e "${BLUE}💡 Searching for alternative ports for $description...${NC}"
60+
61+
for ((port=start_range; port<=end_range; port++)); do
62+
if ! nc -z localhost $port 2>/dev/null; then
63+
echo -e "${GREEN} Suggested alternative: $port${NC}"
64+
return 0
65+
fi
66+
done
67+
68+
echo -e "${YELLOW} No alternatives found in range $start_range-$end_range${NC}"
69+
return 1
70+
}
71+
72+
# Function to show port change instructions
73+
show_port_change_instructions() {
74+
local port=$1
75+
local description=$2
76+
local suggested_port=$3
77+
78+
echo -e "${BLUE}📝 To change port $port ($description) to $suggested_port:${NC}"
79+
80+
case $port in
81+
8081)
82+
echo " Update containers/docker-compose.yml: ports: \"$suggested_port:80\""
83+
;;
84+
8001)
85+
echo " Update containers/docker-compose.yml: ports: \"$suggested_port:8001\""
86+
;;
87+
3306)
88+
echo " Update containers/docker-compose.yml: ports: \"$suggested_port:3306\""
89+
;;
90+
6379)
91+
echo " Update containers/docker-compose.yml: ports: \"$suggested_port:6379\""
92+
;;
93+
3307)
94+
echo " Update containers/docker-compose.test.yml: ports: \"$suggested_port:3306\""
95+
;;
96+
6380)
97+
echo " Update containers/docker-compose.test.yml: ports: \"$suggested_port:6379\""
98+
;;
99+
9000)
100+
echo " Update containers/docker-compose.sonarqube.yml: ports: \"$suggested_port:9000\""
101+
;;
102+
5432)
103+
echo " Update containers/docker-compose.sonarqube.yml: ports: \"$suggested_port:5432\""
104+
;;
105+
esac
106+
echo ""
107+
}
108+
109+
# Main function
110+
main() {
111+
echo -e "${BLUE}🔍 Checking port availability for Laravel Blog API Docker setup...${NC}"
112+
echo ""
113+
114+
local unavailable_ports=()
115+
local failed_required=false
116+
117+
# Check required ports
118+
echo -e "${BLUE}📋 Checking required ports:${NC}"
119+
for port_info in "${REQUIRED_PORTS[@]}"; do
120+
local port="${port_info%%:*}"
121+
local description="${port_info#*:}"
122+
if ! check_port "$port" "$description"; then
123+
unavailable_ports+=("$port_info")
124+
failed_required=true
125+
fi
126+
done
127+
128+
echo ""
129+
130+
# Check optional ports (SonarQube)
131+
echo -e "${BLUE}📋 Checking optional ports (SonarQube):${NC}"
132+
for port_info in "${OPTIONAL_PORTS[@]}"; do
133+
local port="${port_info%%:*}"
134+
local description="${port_info#*:}"
135+
check_port "$port" "$description" true
136+
done
137+
138+
echo ""
139+
140+
# Summary and recommendations
141+
if [ "$failed_required" = true ]; then
142+
echo -e "${RED}❌ SETUP CANNOT CONTINUE - Some required ports are unavailable!${NC}"
143+
echo ""
144+
echo -e "${YELLOW}🔧 SOLUTIONS:${NC}"
145+
echo "1. Stop the services using these ports"
146+
echo "2. Use alternative ports (see suggestions below)"
147+
echo "3. Modify docker-compose files with new ports"
148+
echo ""
149+
150+
echo -e "${BLUE}💡 PORT ALTERNATIVES:${NC}"
151+
for port_info in "${unavailable_ports[@]}"; do
152+
local port="${port_info%%:*}"
153+
local description="${port_info#*:}"
154+
155+
# Find suggested alternative
156+
local start_range=$((port + 1))
157+
for ((alt_port=start_range; alt_port<=start_range+50; alt_port++)); do
158+
if ! nc -z localhost $alt_port 2>/dev/null; then
159+
show_port_change_instructions "$port" "$description" "$alt_port"
160+
break
161+
fi
162+
done
163+
done
164+
165+
echo -e "${YELLOW}⚠️ After making changes, run this script again to verify.${NC}"
166+
return 1
167+
else
168+
echo -e "${GREEN}✅ ALL REQUIRED PORTS ARE AVAILABLE!${NC}"
169+
echo -e "${GREEN}🚀 Docker setup can proceed safely.${NC}"
170+
return 0
171+
fi
172+
}
173+
174+
# Check if netcat is available
175+
if ! command -v nc &> /dev/null; then
176+
echo -e "${RED}❌ Error: 'nc' (netcat) is required but not installed.${NC}"
177+
echo "Please install netcat:"
178+
echo " macOS: brew install netcat"
179+
echo " Ubuntu/Debian: sudo apt-get install netcat"
180+
echo " CentOS/RHEL: sudo yum install nc"
181+
exit 1
182+
fi
183+
184+
# Run the main function
185+
main "$@"

0 commit comments

Comments
 (0)