-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocker.sh
More file actions
executable file
·264 lines (237 loc) · 7.16 KB
/
docker.sh
File metadata and controls
executable file
·264 lines (237 loc) · 7.16 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/bin/bash
# Sorting Office Docker Management Script
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to show usage
show_usage() {
echo "🚀 Sorting Office Docker Management Script"
echo "=========================================="
echo ""
echo "Usage: $0 [COMMAND]"
echo ""
echo "Commands:"
echo " build Build the Docker images"
echo " up Start all services"
echo " down Stop all services"
echo " restart Restart all services"
echo " logs Show logs from all services"
echo " logs-app Show logs from application only"
echo " logs-db Show logs from database only"
echo " shell Open shell in application container"
echo " db-shell Open MySQL shell"
echo " clean Remove all containers and volumes"
echo " test-clean Remove all test containers"
echo " dev Start development environment"
echo " dev-down Stop development environment"
echo " status Show status of all services"
echo " help Show this help message"
echo ""
echo "Examples:"
echo " $0 build # Build images"
echo " $0 up # Start production environment"
echo " $0 dev # Start development environment"
echo " $0 logs # View all logs"
}
# Function to check if Docker is running
check_docker() {
if ! docker info > /dev/null 2>&1; then
print_error "Docker is not running. Please start Docker and try again."
exit 1
fi
}
# Function to check if Docker Compose is available
check_docker_compose() {
if ! command -v docker compose > /dev/null 2>&1; then
print_error "Docker Compose is not installed. Please install Docker Compose and try again."
exit 1
fi
}
# Function to build images
build_images() {
print_status "Building Docker images..."
docker compose build
print_success "Images built successfully!"
}
# Function to start services
start_services() {
print_status "Starting Sorting Office services..."
docker compose up -d
print_success "Services started successfully!"
print_status "Application will be available at: http://localhost:3000"
print_status "phpMyAdmin will be available at: http://localhost:8080"
print_status "Default login: admin/admin"
}
# Function to stop services
stop_services() {
print_status "Stopping Sorting Office services..."
docker compose down
print_success "Services stopped successfully!"
}
# Function to restart services
restart_services() {
print_status "Restarting Sorting Office services..."
docker compose restart
print_success "Services restarted successfully!"
}
# Function to show logs
show_logs() {
print_status "Showing logs from all services..."
docker compose logs -f
}
# Function to show app logs
show_app_logs() {
print_status "Showing application logs..."
docker compose logs -f app
}
# Function to show database logs
show_db_logs() {
print_status "Showing database logs..."
docker compose logs -f db
}
# Function to open shell in app container
open_shell() {
print_status "Opening shell in application container..."
docker compose exec app /bin/bash
}
# Function to open database shell
open_db_shell() {
print_status "Opening MySQL shell..."
docker compose exec db mysql -u sortingoffice -psortingoffice sortingoffice
}
# Function to clean up
clean_up() {
print_warning "This will remove all containers and volumes. Are you sure? (y/N)"
read -r response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
print_status "Cleaning up Docker resources..."
docker compose down -v --remove-orphans
# docker system prune -f
print_success "Cleanup completed!"
else
print_status "Cleanup cancelled."
fi
}
test-clean_up() {
print_warning "This will remove all orphaned test containers and volumes."
# read -r response
# if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
# print_status "Cleaning up Docker Test resources..."
# Remove orphaned MySQL test containers
mysql_containers=$(docker ps -a --format '{{.ID}} {{.Image}} {{.Names}}' | grep -v sortingoffice | grep ' mysql' | awk '{print $1}')
if [ -n "$mysql_containers" ]; then
echo "$mysql_containers" | xargs docker rm -f
else
print_status "No orphaned MySQL test containers found."
fi
# Remove orphaned Selenium test containers
selenium_containers=$(docker ps -a --format '{{.ID}} {{.Image}} {{.Names}}' | grep -v sortingoffice | grep ' selenium' | awk '{print $1}')
if [ -n "$selenium_containers" ]; then
echo "$selenium_containers" | xargs docker rm -f
else
print_status "No orphaned Selenium test containers found."
fi
# docker system prune -f
print_success "Cleanup completed!"
# else
# print_status "Cleanup cancelled."
# fi
}
# Function to start development environment
start_dev() {
print_status "Starting development environment..."
docker compose -f docker-compose.yml -f docker-compose.dev.yml up -d
print_success "Development environment started!"
print_status "Application will be available at: http://localhost:3000"
print_status "Database will be available at: localhost:3306"
}
# Function to stop development environment
stop_dev() {
print_status "Stopping development environment..."
docker compose -f docker-compose.yml -f docker-compose.dev.yml down
print_success "Development environment stopped!"
}
# Function to show status
show_status() {
print_status "Service Status:"
docker compose ps
}
# Main script logic
main() {
# Check prerequisites
check_docker
check_docker_compose
# Parse command
case "${1:-help}" in
build)
build_images
;;
up)
start_services
;;
down)
stop_services
;;
restart)
restart_services
;;
logs)
show_logs
;;
logs-app)
show_app_logs
;;
logs-db)
show_db_logs
;;
shell)
open_shell
;;
db-shell)
open_db_shell
;;
clean)
clean_up
;;
test-clean)
test-clean_up
;;
dev)
start_dev
;;
dev-down)
stop_dev
;;
status)
show_status
;;
help|--help|-h)
show_usage
;;
*)
print_error "Unknown command: $1"
echo ""
show_usage
exit 1
;;
esac
}
# Run main function with all arguments
main "$@"