Final Fix: Robust Docker Multi-Stage and Dynamic Port for Hugging Face
Browse files- Dockerfile +15 -32
- packages/server/src/config/env.ts +1 -1
- scripts/start-hf.sh +18 -6
Dockerfile
CHANGED
|
@@ -1,62 +1,45 @@
|
|
| 1 |
-
# Stage 1: Build the application
|
| 2 |
FROM node:20-bookworm AS builder
|
| 3 |
|
|
|
|
| 4 |
WORKDIR /app
|
| 5 |
|
| 6 |
-
# Copy
|
| 7 |
-
COPY package.json package-lock.json ./
|
| 8 |
-
COPY packages/shared/package.json ./packages/shared/
|
| 9 |
-
COPY packages/server/package.json ./packages/server/
|
| 10 |
-
COPY packages/client/package.json ./packages/client/
|
| 11 |
-
|
| 12 |
-
# Install all dependencies for building
|
| 13 |
-
RUN npm ci
|
| 14 |
-
|
| 15 |
-
# Copy source code
|
| 16 |
COPY . .
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
RUN npm run build
|
| 20 |
|
| 21 |
|
| 22 |
-
# Stage 2: Runtime
|
| 23 |
FROM node:20-bookworm-slim
|
| 24 |
|
| 25 |
-
# Install runtime dependencies: Redis, Java
|
| 26 |
RUN apt-get update && apt-get install -y \
|
| 27 |
python3 \
|
| 28 |
openjdk-17-jdk \
|
| 29 |
redis-server \
|
| 30 |
&& rm -rf /var/lib/apt/lists/*
|
| 31 |
|
|
|
|
| 32 |
WORKDIR /app
|
| 33 |
|
| 34 |
-
# Copy
|
| 35 |
-
|
| 36 |
-
COPY
|
| 37 |
-
COPY packages/server/package.json ./packages/server/
|
| 38 |
-
COPY packages/client/package.json ./packages/client/
|
| 39 |
-
|
| 40 |
-
# Install only production dependencies
|
| 41 |
-
RUN npm ci --omit=dev
|
| 42 |
-
|
| 43 |
-
# Copy compiled results from the builder
|
| 44 |
-
COPY --from=builder /app/packages/shared/dist ./packages/shared/dist
|
| 45 |
-
COPY --from=builder /app/packages/server/dist ./packages/server/dist
|
| 46 |
-
COPY --from=builder /app/packages/client/dist ./packages/client/dist
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
COPY scripts/start-hf.sh ./scripts/start-hf.sh
|
| 50 |
RUN chmod +x ./scripts/start-hf.sh
|
| 51 |
|
| 52 |
# Environment variables for Hugging Face
|
| 53 |
ENV NODE_ENV=production
|
| 54 |
-
|
|
|
|
| 55 |
ENV CLIENT_URL=*
|
| 56 |
ENV REDIS_URL=redis://localhost:6379
|
| 57 |
|
| 58 |
# Expose the HF port
|
| 59 |
EXPOSE 7860
|
| 60 |
|
| 61 |
-
#
|
| 62 |
CMD ["./scripts/start-hf.sh"]
|
|
|
|
| 1 |
+
# Stage 1: Build the full application
|
| 2 |
FROM node:20-bookworm AS builder
|
| 3 |
|
| 4 |
+
# Set build-time directory
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
+
# Copy all source files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
COPY . .
|
| 9 |
|
| 10 |
+
# Install all dependencies and build
|
| 11 |
+
RUN npm ci && npm run build
|
| 12 |
|
| 13 |
|
| 14 |
+
# Stage 2: Final Runtime Image
|
| 15 |
FROM node:20-bookworm-slim
|
| 16 |
|
| 17 |
+
# Install runtime dependencies: Redis, Java, and Python
|
| 18 |
RUN apt-get update && apt-get install -y \
|
| 19 |
python3 \
|
| 20 |
openjdk-17-jdk \
|
| 21 |
redis-server \
|
| 22 |
&& rm -rf /var/lib/apt/lists/*
|
| 23 |
|
| 24 |
+
# Set runtime working directory
|
| 25 |
WORKDIR /app
|
| 26 |
|
| 27 |
+
# Copy EVERYTHING from builder (including dist and node_modules)
|
| 28 |
+
# to ensure workspace symlinks and dependencies are preserved exactly.
|
| 29 |
+
COPY --from=builder /app .
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Ensure the startup script is executable
|
|
|
|
| 32 |
RUN chmod +x ./scripts/start-hf.sh
|
| 33 |
|
| 34 |
# Environment variables for Hugging Face
|
| 35 |
ENV NODE_ENV=production
|
| 36 |
+
# Hugging Face usually provides PORT, but we default to 7860
|
| 37 |
+
ENV PORT=7860
|
| 38 |
ENV CLIENT_URL=*
|
| 39 |
ENV REDIS_URL=redis://localhost:6379
|
| 40 |
|
| 41 |
# Expose the HF port
|
| 42 |
EXPOSE 7860
|
| 43 |
|
| 44 |
+
# Use the robust startup script
|
| 45 |
CMD ["./scripts/start-hf.sh"]
|
packages/server/src/config/env.ts
CHANGED
|
@@ -34,7 +34,7 @@ function required(key: string): string {
|
|
| 34 |
|
| 35 |
export const config: EnvConfig = {
|
| 36 |
NODE_ENV: (process.env.NODE_ENV || 'development').trim(),
|
| 37 |
-
PORT:
|
| 38 |
CLIENT_URL: (process.env.NODE_ENV === 'production' ? '*' : (process.env.CLIENT_URL || 'http://localhost:5173')).trim(),
|
| 39 |
GLM_API_KEY: required('GLM_API_KEY'),
|
| 40 |
GLM_BASE_URL: (process.env.GLM_BASE_URL || 'https://open.bigmodel.cn/api/paas/v4/').trim(),
|
|
|
|
| 34 |
|
| 35 |
export const config: EnvConfig = {
|
| 36 |
NODE_ENV: (process.env.NODE_ENV || 'development').trim(),
|
| 37 |
+
PORT: parseInt(process.env.PORT || '7860', 10),
|
| 38 |
CLIENT_URL: (process.env.NODE_ENV === 'production' ? '*' : (process.env.CLIENT_URL || 'http://localhost:5173')).trim(),
|
| 39 |
GLM_API_KEY: required('GLM_API_KEY'),
|
| 40 |
GLM_BASE_URL: (process.env.GLM_BASE_URL || 'https://open.bigmodel.cn/api/paas/v4/').trim(),
|
scripts/start-hf.sh
CHANGED
|
@@ -1,13 +1,25 @@
|
|
| 1 |
#!/bin/bash
|
| 2 |
-
# Start Redis in the background
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
-
# Wait for Redis to be ready
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
sleep 1
|
|
|
|
| 9 |
done
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Start the Node.js application
|
| 13 |
npm run start -w packages/server
|
|
|
|
| 1 |
#!/bin/bash
|
| 2 |
+
# Start Redis in the background with essential flags for containerized environments
|
| 3 |
+
echo "Starting Redis server..."
|
| 4 |
+
redis-server --daemonize yes --protected-mode no
|
| 5 |
|
| 6 |
+
# Wait for Redis to be ready (up to 30 seconds)
|
| 7 |
+
COUNTER=0
|
| 8 |
+
until redis-cli ping > /dev/null 2>&1 || [ $COUNTER -eq 30 ]; do
|
| 9 |
+
echo "Waiting for Redis ($COUNTER)..."
|
| 10 |
sleep 1
|
| 11 |
+
((COUNTER++))
|
| 12 |
done
|
| 13 |
+
|
| 14 |
+
if [ $COUNTER -eq 30 ]; then
|
| 15 |
+
echo "⚠️ Redis failed to start or is unreachable, but continuing for app boot..."
|
| 16 |
+
else
|
| 17 |
+
echo "✅ Redis is ready!"
|
| 18 |
+
fi
|
| 19 |
+
|
| 20 |
+
# Use the PORT environment variable if provided by HF, otherwise default to 7860
|
| 21 |
+
export PORT="${PORT:-7860}"
|
| 22 |
+
echo "🚀 Starting GLMPilot on port $PORT..."
|
| 23 |
|
| 24 |
# Start the Node.js application
|
| 25 |
npm run start -w packages/server
|