-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-docker-compose.sh
executable file
·256 lines (229 loc) · 6.77 KB
/
run-docker-compose.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
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
#!/bin/bash
# 📚 =================================================================
# 🚀 QUICK GUIDE FOR SCRIPT USAGE
# =================================================================
#
# 1. 🔧 PREPARATION (first time only)
# ----------------------------------------------
# chmod +x start-container.sh
#
# 2. 🏃 START AND ACCESS THE CONTAINER
# ----------------------------------------------
# 📋 Standard start with interactive menu:
# ./start-container.sh
#
# 🖥️ Direct shell access (builds and starts container):
# ./start-container.sh -s
# ./start-container.sh --shell
#
# ⚡ Other available commands:
# ./start-container.sh --rebuild # Rebuilds the image from scratch (no cache)
# ./start-container.sh -b|--build # Rebuilds the image using cache
# ./start-container.sh -r # Restarts the container
#
# 3. 🛠️ USEFUL COMMANDS INSIDE CONTAINER
# ----------------------------------------------
# Once inside the container you can use:
# 📍 pwd # Check current directory
# 📂 ls -la # List files
# 🔄 ps aux # List processes
# 🌍 env # List environment variables
# 💾 df -h # Check disk space
# 🚪 exit # Exit shell
#
# 4. ℹ️ NOTES
# ----------------------------------------------
# 💡 With -s/--shell option, exiting the shell will keep
# the container running
# 🛑 To stop the container after shell exit, use:
# docker-compose down
# =================================================================
# 🔒 Security settings
set -euo pipefail
IFS=$'\n\t'
# 🎨 Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# ⚙️ Configuration
CONTAINER_NAME="p4pa-io-notification"
DOCKER_COMPOSE_FILE="docker-compose.yml"
MAX_WAIT_TIME=60
DIRECT_SHELL=false
# 📝 Function for logging
log() {
echo -e "${2:-$YELLOW}$(date '+%Y-%m-%d %H:%M:%S') - $1${NC}"
}
# 🔍 Function to check prerequisites
check_prerequisites() {
log "Checking prerequisites..."
if ! command -v docker &> /dev/null; then
log "❌ Docker is not installed!" "$RED"
exit 1
fi
if ! command -v docker-compose &> /dev/null; then
log "❌ Docker Compose is not installed!" "$RED"
exit 1
fi
if ! docker info &> /dev/null; then
log "❌ Docker is not running!" "$RED"
exit 1
fi
if [ ! -f "$DOCKER_COMPOSE_FILE" ]; then
log "❌ File $DOCKER_COMPOSE_FILE not found!" "$RED"
exit 1
fi
}
# 🎯 Function to check container status
check_container() {
# Check if container exists using inspect
if ! docker container inspect "$CONTAINER_NAME" &>/dev/null; then
echo "❌ ERROR: Container $CONTAINER_NAME does not exist" >&2
return 1
fi
# Get container status
status=$(docker container inspect -f '{{.State.Status}}' "$CONTAINER_NAME" 2>/dev/null)
case "$status" in
"running")
echo "✅ Container $CONTAINER_NAME is running"
return 0
;;
"exited")
echo "⏹️ Container $CONTAINER_NAME exists but is stopped" >&2
return 2
;;
"created"|"restarting"|"removing"|"paused"|"dead")
echo "⚠️ Container $CONTAINER_NAME status: $status" >&2
return 3
;;
*)
echo "❌ ERROR: Unknown container status: $status" >&2
return 4
;;
esac
}
# 🏗️ Function to build and start container
build_and_start() {
log "🔄 Building container with cache..."
docker-compose down
docker-compose build
start_container
}
# 🚀 Function to start the container
start_container() {
log "🚀 Starting container $CONTAINER_NAME..."
docker-compose up -d
local waited=0
while ! check_container && [ $waited -lt $MAX_WAIT_TIME ]; do
sleep 1
waited=$((waited + 1))
echo -n "."
done
echo ""
if [ $waited -ge $MAX_WAIT_TIME ]; then
log "⏱️ Timeout while starting container!" "$RED"
exit 1
fi
log "✨ Container started successfully" "$GREEN"
}
# 🖥️ Function to access container shell
enter_shell() {
if check_container; then
log "🔌 Accessing container shell..."
docker exec -it $CONTAINER_NAME /bin/sh
return $?
else
log "❌ Container is not running!" "$RED"
return 1
fi
}
# 📋 Main menu
show_menu() {
echo -e "\n${YELLOW}=== Container Management Menu ===${NC}"
echo "1) 🖥️ Access container shell"
echo "2) 📜 View logs"
echo "3) ℹ️ Container status"
echo "4) 🔄 Restart container"
echo "5) ⏹️ Stop container"
echo "6) 🚪 Exit"
echo -e "${YELLOW}===============================${NC}\n"
}
# 🎮 Main function
main() {
check_prerequisites
# 🎯 Parameter handling
case "${1:-}" in
-s|--shell)
DIRECT_SHELL=true
# Build e start prima di accedere alla shell
build_and_start
enter_shell
exit 0
;;
-r|--restart)
if check_container; then
docker-compose down
fi
start_container
;;
-b|--build)
build_and_start
;;
--rebuild)
log "🔄 Rebuilding container from scratch..."
docker-compose down
docker-compose build --no-cache
start_container
;;
-h|--help)
show_usage
exit 0
;;
"")
build_and_start
;;
*)
log "❌ Invalid option: $1" "$RED"
show_usage
exit 1
;;
esac
# If not direct shell, enter normal cycle
if [ "$DIRECT_SHELL" = false ]; then
enter_shell
# Post-shell menu
while true; do
show_menu
read -p "Select an option (1-6): " choice
case $choice in
1) enter_shell ;;
2) docker logs -f $CONTAINER_NAME ;;
3)
docker ps -f name=$CONTAINER_NAME
echo -e "\n${YELLOW}📊 Resource usage:${NC}"
docker stats --no-stream $CONTAINER_NAME
;;
4)
docker-compose restart
enter_shell
;;
5)
docker-compose down
exit 0
;;
6)
log "👋 Exiting. Container will continue running." "$GREEN"
exit 0
;;
*)
log "❌ Invalid option!" "$RED"
;;
esac
done
fi
}
# ⚡ Signal handling
trap 'echo -e "\n${RED}Script interrupted${NC}"; exit 1' INT TERM
# 🏃 Script execution
main "$@"