-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun-container.sh
executable file
·70 lines (60 loc) · 1.53 KB
/
run-container.sh
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
#!/bin/bash
# Get the directory where the script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Function to check prerequisites
check_prerequisites() {
command -v podman >/dev/null 2>&1 || { echo "Error: podman is not installed"; exit 1; }
command -v curl >/dev/null 2>&1 || { echo "Error: curl is not installed"; exit 1; }
}
# Function to wait for the application to be ready
wait_for_app() {
local max_attempts=5
local attempt=1
while [ $attempt -le $max_attempts ]; do
if curl -s -L http://localhost:18080/todo >/dev/null; then
return 0
fi
attempt=$((attempt + 1))
sleep 5
done
return 1
}
# Function to clean up containers
cleanup_containers() {
cd "$SCRIPT_DIR"
podman-compose -f docker-compose.yml down
podman rm -f $(podman ps -aq) 2>/dev/null || true
}
# Function to start containers
start_containers() {
cd "$SCRIPT_DIR"
cleanup_containers
podman-compose -f docker-compose.yml up -d --build
wait_for_app || { echo "Application failed to start"; exit 1; }
}
# Function to stop containers
stop_containers() {
cleanup_containers
}
# Function to restart containers
restart_containers() {
cleanup_containers
start_containers
}
# Main script
check_prerequisites
case "$1" in
"start")
start_containers
;;
"stop")
stop_containers
;;
"restart")
restart_containers
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac