-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.ps1
More file actions
177 lines (150 loc) · 5.67 KB
/
docker.ps1
File metadata and controls
177 lines (150 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<#
.SYNOPSIS
Helper script to manage the local ecom development stack on Windows (PowerShell).
USAGE
./docker.ps1 up
./docker.ps1 down
./docker.ps1 logs [service]
./docker.ps1 clean
NOTE
Run from the `ecom-be-stack` folder. You may need to allow script execution:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
#>
param(
[string]$Command,
[string]$Service
)
function Write-Info($msg) { Write-Host "[INFO] $msg" -ForegroundColor Green }
function Write-Warn($msg) { Write-Host "[WARN] $msg" -ForegroundColor Yellow }
function Write-Err ($msg) { Write-Host "[ERROR] $msg" -ForegroundColor Red }
function Ensure-CommandExists([string]$cmd) {
try {
& $cmd --version > $null 2>&1
return $true
} catch {
return $false
}
}
function Start-Services {
Write-Info "Starting ecommerce development environment..."
# Create network if it doesn't exist (ignore errors)
try { docker network create ecom-network | Out-Null } catch {}
# Create external volumes (ignore errors)
try { docker volume create ecom_postgres_data | Out-Null } catch {}
try { docker volume create ecom_redis_data | Out-Null } catch {}
try { docker volume create ecom_rabbitmq_data | Out-Null } catch {}
try { docker volume create ecom_pgadmin_data | Out-Null } catch {}
try { docker volume create ecom_elasticsearch_data | Out-Null } catch {}
# Start services
docker compose up -d
Write-Info "Services started successfully!"
Write-Info "Access points:"
Write-Host " - PostgreSQL: localhost:5443"
Write-Host " - Redis: localhost:6390"
Write-Host " - RabbitMQ Management: http://localhost:15683"
Write-Host " - Elasticsearch: http://localhost:9201"
Write-Host " - Kibana: http://localhost:5602"
Write-Host " - PgAdmin: http://localhost:8081"
}
function Stop-Services {
Write-Info "Stopping ecommerce development environment..."
docker compose down
Write-Info "Services stopped successfully!"
}
function Restart-Services {
Write-Info "Restarting ecommerce development environment..."
Stop-Services
Start-Sleep -Seconds 2
Start-Services
}
function Show-Logs {
if ($Service) {
Write-Info "Showing logs for service: $Service"
docker compose logs -f $Service
} else {
Write-Info "Showing logs for all services..."
docker compose logs -f
}
}
function Show-Status {
Write-Info "Service status:"
docker compose ps
}
function Clean-All {
Write-Warn "This will remove ecommerce project containers, images, and volumes!"
$confirm = Read-Host "Are you sure? (y/N)"
if ($confirm -notin @('y','Y')) {
Write-Info "Clean operation cancelled."
return
}
Write-Info "Cleaning up ecommerce project Docker resources..."
# Stop and remove compose resources
docker compose down -v 2>$null
# Remove containers matching name pattern
$containers = docker ps -aq --filter "name=ecom-"
if ($containers) { docker rm -f $containers 2>$null }
# Remove images matching reference
$images = docker images --filter "reference=*ecom*" -q
if ($images) { docker rmi $images 2>$null }
$images2 = docker images --filter "reference=*ecommerce*" -q
if ($images2) { docker rmi $images2 2>$null }
# Remove known volumes
try { docker volume rm ecom_postgres_data,ecom_redis_data,ecom_rabbitmq_data,ecom_pgadmin_data,ecom_elasticsearch_data -f 2>$null } catch {}
# Remove network
try { docker network rm ecom-network 2>$null } catch {}
# Prune system (safe)
docker system prune -f
Write-Info "Ecommerce project Docker resources cleaned!"
}
function Run-Test {
Write-Info "Testing all service connections..."
if (Test-Path ./scripts/test-connections.sh) {
& bash ./scripts/test-connections.sh
} else {
Write-Warn "No test script found at ./scripts/test-connections.sh"
}
}
function Run-Seed {
Write-Info "Seeding development data..."
if (Test-Path ./scripts/seed-data.sh) {
& bash ./scripts/seed-data.sh
} else {
Write-Warn "No seed script found at ./scripts/seed-data.sh"
}
}
function Setup-ES {
Write-Info "Setting up Elasticsearch users..."
if (Test-Path ./setup-elasticsearch-users.sh) {
& bash ./setup-elasticsearch-users.sh
} else {
Write-Warn "No setup script found at ./setup-elasticsearch-users.sh"
}
}
switch ($Command) {
'up' { Start-Services; break }
'start' { Start-Services; break }
'down' { Stop-Services; break }
'stop' { Stop-Services; break }
'restart' { Restart-Services; break }
'logs' { Show-Logs; break }
'status' { Show-Status; break }
'ps' { Show-Status; break }
'clean' { Clean-All; break }
'test' { Run-Test; break }
'seed' { Run-Seed; break }
'setup-es' { Setup-ES; break }
default {
Write-Host "Usage: .\docker.ps1 {up|down|restart|logs|status|clean|test|seed|setup-es} [service]"
Write-Host ""
Write-Host "Commands:"
Write-Host " up/start - Start all services"
Write-Host " down/stop - Stop all services"
Write-Host " restart - Restart all services"
Write-Host " logs [svc] - Show logs (optionally for specific service)"
Write-Host " status/ps - Show service status"
Write-Host " clean - Clean all Docker containers, images, and volumes"
Write-Host " test - Test all service connections (calls ./scripts/test-connections.sh)"
Write-Host " seed - Seed development data (calls ./scripts/seed-data.sh)"
Write-Host " setup-es - Setup Elasticsearch users (calls ./setup-elasticsearch-users.sh)"
}
}