Skip to content

Commit b641994

Browse files
authored
Merge pull request #29 from PeterVinter/feature/enhance-startup-logging
feat: enhance startup logging with detailed container information
2 parents 80b69d0 + bec03a9 commit b641994

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

startup_docker_container.sh

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ SHUTDOWN_LIST="shutdowned.txt"
66

77
log_message() {
88
local message="$1"
9+
local level="${2:-INFO}" # Default level is INFO
910
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
10-
echo "[$timestamp] $message" | tee -a "$LOG_FILE"
11+
echo "[$timestamp] [$level] $message" | tee -a "$LOG_FILE"
1112
}
1213

1314
# Function to check container status
@@ -25,6 +26,19 @@ check_container() {
2526
fi
2627
}
2728

29+
# Function to get container details
30+
get_container_details() {
31+
local name=$1
32+
local details=$(docker inspect "$name" 2>/dev/null)
33+
if [ $? -eq 0 ]; then
34+
local ip=$(echo "$details" | grep -m 1 '"IPAddress":' | cut -d '"' -f 4)
35+
local created=$(echo "$details" | grep -m 1 '"Created":' | cut -d '"' -f 4)
36+
echo "IP: $ip, Created: $created"
37+
else
38+
echo "No details available"
39+
fi
40+
}
41+
2842
# Function to reverse file content (compatible with both macOS and Linux)
2943
reverse_file() {
3044
if command -v tac >/dev/null 2>&1; then
@@ -36,66 +50,57 @@ reverse_file() {
3650

3751
# Check if shutdown list exists
3852
if [ ! -f "$SHUTDOWN_LIST" ]; then
39-
log_message "No shutdown list found at $SHUTDOWN_LIST. Nothing to start."
53+
log_message "No shutdown list found at $SHUTDOWN_LIST. Nothing to start." "WARNING"
4054
exit 1
4155
fi
4256

43-
log_message "Starting Docker containers from shutdown list..."
57+
log_message "Starting Docker containers from shutdown list..." "INFO"
4458

4559
# Read the shutdown list in reverse order (to respect dependencies)
4660
while IFS='|' read -r name image cmd ports networks; do
47-
log_message "Processing container: $name"
61+
log_message "Processing container: $name" "INFO"
4862

4963
# Check container status
5064
container_status=$(check_container "$name")
5165

52-
case $container_status in
66+
case "$container_status" in
5367
"running")
54-
log_message "Container $name is already running, skipping..."
55-
continue
68+
log_message "Container $name is already running" "INFO"
69+
details=$(get_container_details "$name")
70+
log_message "Container details - $details" "INFO"
5671
;;
5772
"stopped")
58-
log_message "Container $name exists but is stopped, starting it..."
73+
log_message "Starting stopped container: $name" "INFO"
5974
if docker start "$name"; then
60-
log_message "Successfully started existing container: $name"
75+
details=$(get_container_details "$name")
76+
log_message "Successfully started container $name" "SUCCESS"
77+
log_message "Container details - $details" "INFO"
6178
else
62-
log_message "Failed to start existing container: $name"
79+
log_message "Failed to start container $name" "ERROR"
6380
fi
64-
continue
6581
;;
6682
"none")
67-
# Process port mappings
68-
port_args=""
69-
if [ ! -z "$ports" ]; then
70-
# Convert semicolon-separated port mappings into multiple -p arguments
71-
for port_mapping in ${ports//;/ }; do
72-
if [ ! -z "$port_mapping" ]; then
73-
port_args="$port_args -p $port_mapping"
74-
fi
75-
done
76-
fi
77-
78-
# Process network
79-
network_arg=""
80-
if [ ! -z "$networks" ]; then
81-
# Get first network (before semicolon)
82-
network=$(echo "$networks" | cut -d';' -f1)
83-
if [ ! -z "$network" ]; then
84-
network_arg="--network $network"
85-
fi
86-
fi
87-
83+
log_message "Creating new container: $name" "INFO"
8884
# Create and start the container
89-
docker_cmd="docker run -d --name ${name} ${port_args} ${network_arg} ${image} ${cmd}"
90-
log_message "Creating new container: $docker_cmd"
91-
92-
if eval "$docker_cmd"; then
93-
log_message "Successfully created and started container: $name"
85+
if [ -z "$networks" ]; then
86+
if docker run -d --name "$name" $ports "$image" $cmd; then
87+
details=$(get_container_details "$name")
88+
log_message "Successfully created and started container $name" "SUCCESS"
89+
log_message "Container details - $details" "INFO"
90+
else
91+
log_message "Failed to create container $name" "ERROR"
92+
fi
9493
else
95-
log_message "Failed to create and start container: $name"
94+
if docker run -d --name "$name" $ports --network "$networks" "$image" $cmd; then
95+
details=$(get_container_details "$name")
96+
log_message "Successfully created and started container $name with network $networks" "SUCCESS"
97+
log_message "Container details - $details" "INFO"
98+
else
99+
log_message "Failed to create container $name with network $networks" "ERROR"
100+
fi
96101
fi
97102
;;
98103
esac
99104
done < <(reverse_file "$SHUTDOWN_LIST")
100105

101-
log_message "Finished starting all containers from shutdown list."
106+
log_message "Container startup process completed" "INFO"

0 commit comments

Comments
 (0)