-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
executable file
·76 lines (63 loc) · 2.32 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
executable file
·76 lines (63 loc) · 2.32 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
#!/bin/bash
# examples/docker-entrypoint.sh
# Production-ready Docker entrypoint script
set -e
# Configuration
MEMORY_CALCULATOR="/usr/local/bin/memory-calculator"
DEFAULT_JVM_OPTS="-Xmx512m -XX:MaxMetaspaceSize=128m -Xss1m -XX:ReservedCodeCacheSize=240m"
echo "🐳 Docker container starting..."
# Function to calculate JVM options
calculate_jvm_options() {
if [ -x "$MEMORY_CALCULATOR" ]; then
echo "🧮 Calculating JVM options based on container resources..."
# Try to calculate optimal settings
local calculated_opts
if calculated_opts="$($MEMORY_CALCULATOR --quiet 2>/dev/null)" && [ -n "$calculated_opts" ]; then
echo "✅ Using calculated JVM options"
echo "$calculated_opts"
return 0
else
echo "⚠️ Memory calculation failed, checking container limits..."
# Try with explicit memory limit detection
local mem_limit
if mem_limit=$(cat /sys/fs/cgroup/memory.max 2>/dev/null) && [ "$mem_limit" != "max" ]; then
local mem_gb=$((mem_limit / 1024 / 1024 / 1024))
echo "📊 Detected ${mem_gb}GB container limit"
calculated_opts="$($MEMORY_CALCULATOR --total-memory=${mem_gb}G --quiet 2>/dev/null)" || true
if [ -n "$calculated_opts" ]; then
echo "✅ Using calculated options for ${mem_gb}GB"
echo "$calculated_opts"
return 0
fi
fi
fi
fi
echo "⚠️ Using fallback JVM options"
echo "$DEFAULT_JVM_OPTS"
}
# Calculate and set JVM options
JVM_OPTS="$(calculate_jvm_options)"
export JAVA_TOOL_OPTIONS="$JVM_OPTS"
echo "🎯 Final JVM Options: $JAVA_TOOL_OPTIONS"
echo ""
# Health check function
health_check() {
echo "🏥 Container health check:"
echo " Memory limit: $(cat /sys/fs/cgroup/memory.max 2>/dev/null || echo 'not detected')"
echo " JVM options: $JAVA_TOOL_OPTIONS"
echo " Java version: $(java -version 2>&1 | head -1)"
}
# Handle special commands
case "$1" in
"health")
health_check
exit 0
;;
"java-opts")
echo "$JAVA_TOOL_OPTIONS"
exit 0
;;
esac
# Start the main application
echo "🚀 Starting application: $@"
exec "$@"