forked from ariskou/Dynamic-Web-Server-Assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-monitor-and-adjust.sh
executable file
·48 lines (40 loc) · 1.7 KB
/
run-monitor-and-adjust.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
#!/bin/bash
# Function to check if a container is running
is_container_running() {
docker ps --filter "name=$1" --format '{{.Names}}' | grep -w "$1" > /dev/null 2>&1
}
# Function to check if an image exists
is_image_present() {
docker images --filter "reference=$1" --format '{{.Repository}}' | grep -w "$1" > /dev/null 2>&1
}
# Ensure the network exists
if ! docker network ls --filter "name=pricing-network" --format '{{.Name}}' | grep -w "pricing-network" > /dev/null 2>&1; then
echo "Creating network pricing-network..."
docker network create pricing-network
else
echo "Network pricing-network already exists."
fi
# Ensure the pricing-service image exists
if ! is_image_present pricing-service; then
echo "Error: pricing-service image not found. Please build the image first."
exit 1
fi
# Ensure the monitor-and-adjust image exists
if ! is_image_present monitor-and-adjust; then
echo "Error: monitor-and-adjust image not found. Please build the image first."
exit 1
fi
# Ensure the pricing-service is running
if ! is_container_running pricing-service; then
echo "Starting pricing-service..."
docker run --rm --network pricing-network --name pricing-service -p 20100:80 -d pricing-service
else
echo "pricing-service is already running."
fi
# Run or restart monitor-and-adjust container
if is_container_running monitor-and-adjust; then
echo "Restarting monitor-and-adjust..."
docker stop monitor-and-adjust
fi
# docker run --rm --name monitor-and-adjust --network pricing-network -d monitor-and-adjust # This fails to connect with Docker Daemon
docker run --name monitor-and-adjust --network pricing-network -v /var/run/docker.sock:/var/run/docker.sock -d monitor-and-adjust