-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-build.sh
More file actions
executable file
·246 lines (209 loc) · 6 KB
/
docker-build.sh
File metadata and controls
executable file
·246 lines (209 loc) · 6 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
#!/bin/bash
# ============================================================
# Docker Build Script for City Service Application
# Usage: ./docker-build.sh [environment] [action]
# ============================================================
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
# Default values
ENVIRONMENT=${1:-dev}
ACTION=${2:-build}
PROJECT_NAME="city-service"
# 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 check prerequisites
check_prerequisites() {
print_status "Checking prerequisites..."
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed"
exit 1
fi
if ! command -v docker-compose &> /dev/null; then
print_error "Docker Compose is not installed"
exit 1
fi
if [ ! -f ".env" ]; then
print_warning ".env file not found, copying from env.example"
cp env.example .env
print_warning "Please update .env file with your configuration"
fi
print_success "Prerequisites check passed"
}
# Function to build images
build_images() {
print_status "Building Docker images for $ENVIRONMENT environment..."
case $ENVIRONMENT in
"dev"|"development")
docker-compose build --parallel
;;
"prod"|"production")
docker-compose -f docker-compose.yml -f docker-compose.prod.yml build --parallel
;;
*)
print_error "Unknown environment: $ENVIRONMENT"
print_status "Available environments: dev, prod"
exit 1
;;
esac
print_success "Images built successfully"
}
# Function to start services
start_services() {
print_status "Starting services for $ENVIRONMENT environment..."
case $ENVIRONMENT in
"dev"|"development")
docker-compose up -d
;;
"prod"|"production")
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
;;
esac
print_success "Services started successfully"
print_status "Waiting for services to be ready..."
sleep 30
# Check service health
check_health
}
# Function to stop services
stop_services() {
print_status "Stopping services..."
case $ENVIRONMENT in
"dev"|"development")
docker-compose down
;;
"prod"|"production")
docker-compose -f docker-compose.yml -f docker-compose.prod.yml down
;;
esac
print_success "Services stopped successfully"
}
# Function to check service health
check_health() {
print_status "Checking service health..."
# Check backend health
if curl -f http://localhost:8080/api/health > /dev/null 2>&1; then
print_success "Backend is healthy"
else
print_warning "Backend health check failed"
fi
# Check frontend health
if curl -f http://localhost:3000 > /dev/null 2>&1; then
print_success "Frontend is healthy"
else
print_warning "Frontend health check failed"
fi
# Show running containers
print_status "Running containers:"
docker-compose ps
}
# Function to show logs
show_logs() {
print_status "Showing logs for $ENVIRONMENT environment..."
case $ENVIRONMENT in
"dev"|"development")
docker-compose logs -f
;;
"prod"|"production")
docker-compose -f docker-compose.yml -f docker-compose.prod.yml logs -f
;;
esac
}
# Function to clean up
cleanup() {
print_status "Cleaning up Docker resources..."
# Stop and remove containers
docker-compose down --remove-orphans
# Remove unused images
docker image prune -f
# Remove unused volumes (be careful with this in production)
if [ "$ENVIRONMENT" = "dev" ]; then
docker volume prune -f
fi
print_success "Cleanup completed"
}
# Function to show usage
show_usage() {
echo "Usage: $0 [environment] [action]"
echo ""
echo "Environments:"
echo " dev, development - Development environment"
echo " prod, production - Production environment"
echo ""
echo "Actions:"
echo " build - Build Docker images"
echo " start - Start services"
echo " stop - Stop services"
echo " restart - Restart services"
echo " logs - Show logs"
echo " health - Check service health"
echo " cleanup - Clean up Docker resources"
echo " help - Show this help message"
echo ""
echo "Examples:"
echo " $0 dev build # Build images for development"
echo " $0 prod start # Start production services"
echo " $0 dev logs # Show development logs"
}
# Main script logic
main() {
print_status "City Service Docker Build Script"
print_status "Environment: $ENVIRONMENT"
print_status "Action: $ACTION"
echo ""
case $ACTION in
"build")
check_prerequisites
build_images
;;
"start")
check_prerequisites
build_images
start_services
;;
"stop")
stop_services
;;
"restart")
stop_services
sleep 5
check_prerequisites
build_images
start_services
;;
"logs")
show_logs
;;
"health")
check_health
;;
"cleanup")
cleanup
;;
"help"|"--help"|"-h")
show_usage
;;
*)
print_error "Unknown action: $ACTION"
show_usage
exit 1
;;
esac
}
# Run main function
main "$@"