-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-run.sh
More file actions
executable file
·366 lines (316 loc) · 9.58 KB
/
Copy pathdocker-run.sh
File metadata and controls
executable file
·366 lines (316 loc) · 9.58 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/bin/bash
# TCP Security System Docker Runner
# Provides easy commands to build, run, and manage the TCP Docker container
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
# Configuration
CONTAINER_NAME="tcp-security-system"
IMAGE_NAME="tcp-security"
NETWORK_NAME="tcp-security-network"
# Helper functions
log_info() {
echo -e "${BLUE}ℹ️ $1${NC}"
}
log_success() {
echo -e "${GREEN}✅ $1${NC}"
}
log_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
log_error() {
echo -e "${RED}❌ $1${NC}"
}
show_header() {
echo -e "${BLUE}"
echo "🐳 TCP Security System Docker Manager"
echo "====================================="
echo -e "${NC}"
}
show_usage() {
echo "Usage: $0 [COMMAND]"
echo ""
echo "Commands:"
echo " build Build the TCP Security Docker image"
echo " run Run the TCP Security container"
echo " start Start existing container"
echo " stop Stop the container"
echo " restart Restart the container"
echo " shell Open shell in running container"
echo " logs Show container logs"
echo " status Show container status"
echo " demo Run the local Ollama demo"
echo " security Run the security demo"
echo " clean Remove container and image"
echo " health Check container health"
echo " compose Use docker-compose (build/up/down)"
echo ""
echo "Examples:"
echo " $0 build # Build the image"
echo " $0 run # Run container interactively"
echo " $0 demo # Run Ollama demo inside container"
echo " $0 shell # Get shell access"
echo " $0 compose up # Use docker-compose to start"
}
check_docker() {
if ! command -v docker &> /dev/null; then
log_error "Docker is not installed or not in PATH"
exit 1
fi
if ! docker info &> /dev/null; then
log_error "Docker daemon is not running"
exit 1
fi
}
build_image() {
log_info "Building TCP Security Docker image..."
docker build -t "$IMAGE_NAME" . || {
log_error "Failed to build Docker image"
exit 1
}
log_success "Docker image built successfully: $IMAGE_NAME"
}
run_container() {
log_info "Running TCP Security container..."
# Stop existing container if running
if docker ps -q -f name="$CONTAINER_NAME" | grep -q .; then
log_warning "Stopping existing container..."
docker stop "$CONTAINER_NAME" &> /dev/null || true
fi
# Remove existing container if exists
if docker ps -aq -f name="$CONTAINER_NAME" | grep -q .; then
log_warning "Removing existing container..."
docker rm "$CONTAINER_NAME" &> /dev/null || true
fi
# Create network if it doesn't exist
docker network create "$NETWORK_NAME" &> /dev/null || true
# Run container
docker run -it \
--name "$CONTAINER_NAME" \
--hostname tcp-ubuntu \
--network "$NETWORK_NAME" \
-p 11434:11434 \
-p 8080:8080 \
-v tcp-security-data:/tcp-security/data \
-v tcp-security-cache:/tcp-security/cache \
-v tcp-security-logs:/tcp-security/logs \
-v tcp-ollama-models:/root/.ollama \
--security-opt no-new-privileges:true \
--memory="4g" \
--cpus="2.0" \
"$IMAGE_NAME"
}
start_container() {
if ! docker ps -aq -f name="$CONTAINER_NAME" | grep -q .; then
log_error "Container $CONTAINER_NAME does not exist. Run '$0 run' first."
exit 1
fi
log_info "Starting container..."
docker start "$CONTAINER_NAME"
log_success "Container started"
}
stop_container() {
if docker ps -q -f name="$CONTAINER_NAME" | grep -q .; then
log_info "Stopping container..."
docker stop "$CONTAINER_NAME"
log_success "Container stopped"
else
log_warning "Container is not running"
fi
}
restart_container() {
log_info "Restarting container..."
docker restart "$CONTAINER_NAME" &> /dev/null || {
log_error "Failed to restart container. Container may not exist."
exit 1
}
log_success "Container restarted"
}
open_shell() {
if ! docker ps -q -f name="$CONTAINER_NAME" | grep -q .; then
log_error "Container is not running. Start it with '$0 start' or '$0 run'"
exit 1
fi
log_info "Opening shell in container..."
docker exec -it "$CONTAINER_NAME" /bin/bash
}
show_logs() {
if ! docker ps -aq -f name="$CONTAINER_NAME" | grep -q .; then
log_error "Container does not exist"
exit 1
fi
log_info "Showing container logs..."
docker logs -f "$CONTAINER_NAME"
}
show_status() {
log_info "Container status:"
if docker ps -q -f name="$CONTAINER_NAME" | grep -q .; then
echo " Status: ✅ Running"
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" -f name="$CONTAINER_NAME"
echo ""
# Check health
health_status=$(docker inspect --format='{{.State.Health.Status}}' "$CONTAINER_NAME" 2>/dev/null || echo "no-health-check")
echo " Health: $health_status"
# Show resource usage
docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}" "$CONTAINER_NAME"
elif docker ps -aq -f name="$CONTAINER_NAME" | grep -q .; then
echo " Status: ⏹️ Stopped"
docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" -f name="$CONTAINER_NAME"
else
echo " Status: ❌ Does not exist"
fi
}
run_demo() {
if ! docker ps -q -f name="$CONTAINER_NAME" | grep -q .; then
log_error "Container is not running. Start it with '$0 start' or '$0 run'"
exit 1
fi
log_info "Running local Ollama demo inside container..."
docker exec -it "$CONTAINER_NAME" bash -c "
source /tcp-security/venv/bin/activate
cd /tcp-security
python tcp/local_ollama_demo.py
"
}
run_security_demo() {
if ! docker ps -q -f name="$CONTAINER_NAME" | grep -q .; then
log_error "Container is not running. Start it with '$0 start' or '$0 run'"
exit 1
fi
log_info "Running complete security demo inside container..."
docker exec -it "$CONTAINER_NAME" bash -c "
source /tcp-security/venv/bin/activate
cd /tcp-security
python tcp/demo_complete_security_system.py
"
}
clean_all() {
log_warning "This will remove the container and image. Continue? (y/N)"
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
# Stop and remove container
if docker ps -q -f name="$CONTAINER_NAME" | grep -q .; then
log_info "Stopping container..."
docker stop "$CONTAINER_NAME"
fi
if docker ps -aq -f name="$CONTAINER_NAME" | grep -q .; then
log_info "Removing container..."
docker rm "$CONTAINER_NAME"
fi
# Remove image
if docker images -q "$IMAGE_NAME" | grep -q .; then
log_info "Removing image..."
docker rmi "$IMAGE_NAME"
fi
# Remove network
docker network rm "$NETWORK_NAME" &> /dev/null || true
log_success "Cleanup completed"
else
log_info "Cleanup cancelled"
fi
}
check_health() {
if ! docker ps -q -f name="$CONTAINER_NAME" | grep -q .; then
log_error "Container is not running"
exit 1
fi
log_info "Checking container health..."
health_status=$(docker inspect --format='{{.State.Health.Status}}' "$CONTAINER_NAME" 2>/dev/null || echo "no-health-check")
echo "Health Status: $health_status"
# Test Ollama directly
docker exec "$CONTAINER_NAME" curl -s http://localhost:11434/api/tags > /dev/null
if [ $? -eq 0 ]; then
log_success "Ollama is responsive"
else
log_error "Ollama is not responding"
fi
# Show health check logs
docker inspect --format='{{range .State.Health.Log}}{{.Output}}{{end}}' "$CONTAINER_NAME" 2>/dev/null || true
}
use_compose() {
if [ -z "$2" ]; then
log_error "Please specify compose command: build, up, down, logs, etc."
exit 1
fi
log_info "Running docker-compose $2..."
docker-compose "$2" "${@:3}"
}
# Main command handling
case "$1" in
"build")
show_header
check_docker
build_image
;;
"run")
show_header
check_docker
run_container
;;
"start")
show_header
check_docker
start_container
;;
"stop")
show_header
check_docker
stop_container
;;
"restart")
show_header
check_docker
restart_container
;;
"shell")
check_docker
open_shell
;;
"logs")
check_docker
show_logs
;;
"status")
show_header
check_docker
show_status
;;
"demo")
check_docker
run_demo
;;
"security")
check_docker
run_security_demo
;;
"clean")
show_header
check_docker
clean_all
;;
"health")
check_docker
check_health
;;
"compose")
check_docker
use_compose "$@"
;;
"help"|"-h"|"--help")
show_header
show_usage
;;
"")
show_header
show_usage
;;
*)
log_error "Unknown command: $1"
show_usage
exit 1
;;
esac