Skip to content

A modern and elegant dashboard for network traffic visualization and analysis.

feat: Implement Docker health checks and standardize port environment variables across Dockerfile, docker-compose, and Next.js configuration.

luolei66962a590c31
diff --git a/Dockerfile b/Dockerfile
index 12bea59..b5c0509 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -32,8 +32,18 @@ RUN pnpm --filter @clashmaster/web build
# Production stage
FROM node:22-alpine AS production
+# Install wget for health checks
+RUN apk add --no-cache wget
+
WORKDIR /app
+# Default environment variables
+ENV NODE_ENV=production \
+ WEB_PORT=3000 \
+ API_PORT=3001 \
+ COLLECTOR_WS_PORT=3002 \
+ DB_PATH=/app/data/stats.db
+
# Ensure data directory exists
RUN mkdir -p /app/data
@@ -60,6 +70,10 @@ EXPOSE 3000 3001 3002
# Data volume
VOLUME ["/app/data"]
+# Health check - verify API is responding
+HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
+ CMD wget -q --spider http://127.0.0.1:3001/health || exit 1
+
# Start script
COPY docker-start.sh ./
RUN chmod +x docker-start.sh
diff --git a/apps/web/next.config.ts b/apps/web/next.config.ts
index bab8681..76cbd96 100644
--- a/apps/web/next.config.ts
+++ b/apps/web/next.config.ts
@@ -10,6 +10,8 @@ const rootPkg = JSON.parse(
readFileSync(join(__dirname, "../../package.json"), "utf-8"),
);
+const API_URL = process.env.API_URL || "http://localhost:3001";
+
const nextConfig: NextConfig = {
reactStrictMode: true,
output: 'standalone',
@@ -20,7 +22,7 @@ const nextConfig: NextConfig = {
return [
{
source: "/api/:path*",
- destination: "http://localhost:3001/api/:path*",
+ destination: `${API_URL}/api/:path*`,
},
];
},
diff --git a/assets/clash-master-overview.png b/assets/clash-master-overview.png
index 43914fc..1208c8f 100644
Binary files a/assets/clash-master-overview.png and b/assets/clash-master-overview.png differ
diff --git a/docker-compose.yml b/docker-compose.yml
index 89a7da6..232e77c 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -15,12 +15,7 @@ services:
- ./data:/app/data
environment:
- NODE_ENV=production
+ - WEB_PORT=3000
- API_PORT=3001
- - WS_PORT=3002
+ - COLLECTOR_WS_PORT=3002
- DB_PATH=/app/data/stats.db
- healthcheck:
- test: ["CMD", "wget", "-q", "--spider", "http://localhost:3001/health"]
- interval: 30s
- timeout: 10s
- retries: 3
- start_period: 40s
diff --git a/docker-start.sh b/docker-start.sh
index 263aa8f..3e37d47 100755
--- a/docker-start.sh
+++ b/docker-start.sh
@@ -2,53 +2,107 @@
set -e
+# ─── Default environment variables ───────────────────────────────────
+# All services bind to 0.0.0.0 so Docker port mapping works correctly
+WEB_PORT="${WEB_PORT:-3000}"
+API_PORT="${API_PORT:-3001}"
+COLLECTOR_WS_PORT="${COLLECTOR_WS_PORT:-3002}"
+DB_PATH="${DB_PATH:-/app/data/stats.db}"
+
+export API_PORT COLLECTOR_WS_PORT DB_PATH
+
echo "╔════════════════════════════════════════════════════════╗"
echo "║ Clash Master - Starting... ║"
echo "╚════════════════════════════════════════════════════════╝"
echo
+echo "📊 Web UI: http://0.0.0.0:${WEB_PORT}"
+echo "🔌 API: http://0.0.0.0:${API_PORT}"
+echo "📡 WebSocket: ws://0.0.0.0:${COLLECTOR_WS_PORT}"
+echo "💾 Database: ${DB_PATH}"
+echo
# Ensure data directory exists
-mkdir -p /app/data
+mkdir -p "$(dirname "$DB_PATH")"
-echo "📊 Web UI: http://localhost:3000"
-echo "🔌 API: http://localhost:3001"
-echo "📡 WebSocket: ws://localhost:3002"
-echo
-
-# Start collector in background
+# ─── Start collector ─────────────────────────────────────────────────
echo "🚀 Starting data collector..."
cd /app/apps/collector && node dist/index.js &
COLLECTOR_PID=$!
-# Wait for collector to be ready
-echo "⏳ Waiting for collector to be ready..."
-sleep 3
+# Wait for API to be ready (up to 30 seconds)
+echo "⏳ Waiting for API to be ready..."
+RETRIES=0
+MAX_RETRIES=30
+while [ $RETRIES -lt $MAX_RETRIES ]; do
+ if wget -q --spider "http://127.0.0.1:${API_PORT}/health" 2>/dev/null; then
+ echo "✅ API is ready!"
+ break
+ fi
+ RETRIES=$((RETRIES + 1))
+ sleep 1
+done
+
+if [ $RETRIES -eq $MAX_RETRIES ]; then
+ echo "⚠️ API did not become ready in ${MAX_RETRIES}s, starting web anyway..."
+fi
-# Start web frontend (standalone mode)
+# ─── Start web frontend ─────────────────────────────────────────────
echo "🌐 Starting web frontend..."
-cd /app/apps/web/.next/standalone/apps/web && NODE_ENV=production PORT=3000 node server.js &
+cd /app/apps/web/.next/standalone/apps/web && \
+ HOSTNAME=0.0.0.0 \
+ NODE_ENV=production \
+ PORT="${WEB_PORT}" \
+ node server.js &
WEB_PID=$!
-# Wait for web to be ready
+# Wait for web to be ready (up to 30 seconds)
echo "⏳ Waiting for web frontend to be ready..."
-sleep 3
+RETRIES=0
+while [ $RETRIES -lt $MAX_RETRIES ]; do
+ if wget -q --spider "http://127.0.0.1:${WEB_PORT}" 2>/dev/null; then
+ echo "✅ Web frontend is ready!"
+ break
+ fi
+ RETRIES=$((RETRIES + 1))
+ sleep 1
+done
+
+if [ $RETRIES -eq $MAX_RETRIES ]; then
+ echo "⚠️ Web frontend did not become ready in ${MAX_RETRIES}s"
+fi
echo
-echo "✅ All services started successfully!"
+echo "════════════════════════════════════════════════════════"
+echo " ✅ All services started successfully!"
echo
-echo "📝 Access the dashboard at: http://localhost:3000"
-echo "🔧 Configure your OpenClash backend in the web UI"
+echo " 📝 Access the dashboard at: http://<your-host>:${WEB_PORT}"
+echo " 🔧 Configure your OpenClash backend in the web UI"
+echo "════════════════════════════════════════════════════════"
echo
-# Handle shutdown
+# ─── Process monitoring & graceful shutdown ──────────────────────────
cleanup() {
echo
echo "🛑 Shutting down services..."
- kill $WEB_PID $COLLECTOR_PID 2>/dev/null || true
+ kill $WEB_PID 2>/dev/null || true
+ kill $COLLECTOR_PID 2>/dev/null || true
+ wait $WEB_PID 2>/dev/null || true
+ wait $COLLECTOR_PID 2>/dev/null || true
+ echo "👋 All services stopped."
exit 0
}
trap cleanup SIGTERM SIGINT
-# Keep container running
-wait
+# Monitor child processes - exit if either one dies
+while true; do
+ if ! kill -0 $COLLECTOR_PID 2>/dev/null; then
+ echo "❌ Collector process (PID $COLLECTOR_PID) has died. Shutting down..."
+ cleanup
+ fi
+ if ! kill -0 $WEB_PID 2>/dev/null; then
+ echo "❌ Web frontend process (PID $WEB_PID) has died. Shutting down..."
+ cleanup
+ fi
+ sleep 5
+done