E5K7 commited on
Commit
5b612d4
·
1 Parent(s): 865193f

Optimize Docker: Switch to multi-stage build for faster and leaner deployment

Browse files
Files changed (1) hide show
  1. Dockerfile +36 -18
Dockerfile CHANGED
@@ -1,44 +1,62 @@
1
- FROM node:20-bookworm
 
2
 
3
- # Install Python, Java, and Redis
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  RUN apt-get update && apt-get install -y \
5
  python3 \
6
  openjdk-17-jdk \
7
  redis-server \
8
  && rm -rf /var/lib/apt/lists/*
9
 
10
- # Set working directory
11
  WORKDIR /app
12
 
13
- # Copy root package files
14
  COPY package.json package-lock.json ./
15
-
16
- # Copy workspace package files
17
  COPY packages/shared/package.json ./packages/shared/
18
  COPY packages/server/package.json ./packages/server/
19
  COPY packages/client/package.json ./packages/client/
20
 
21
- # Install dependencies
22
- RUN npm ci
23
 
24
- # Copy full source
25
- COPY . .
 
 
26
 
27
- # Build packages
28
- RUN npm run build
 
29
 
30
- # Set environment variables for Hugging Face Spaces
31
  ENV NODE_ENV=production
32
  ENV PORT=7860
33
  ENV CLIENT_URL=*
34
  ENV REDIS_URL=redis://localhost:6379
35
 
36
- # Copy startup script
37
- COPY scripts/start-hf.sh ./scripts/start-hf.sh
38
- RUN chmod +x ./scripts/start-hf.sh
39
-
40
  # Expose the HF port
41
  EXPOSE 7860
42
 
43
- # Start Redis and the server via script
44
  CMD ["./scripts/start-hf.sh"]
 
1
+ # Stage 1: Build the application
2
+ FROM node:20-bookworm AS builder
3
 
4
+ WORKDIR /app
5
+
6
+ # Copy root configurations
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
+ # Build all packages (shared -> server -> client)
19
+ RUN npm run build
20
+
21
+
22
+ # Stage 2: Runtime environment
23
+ FROM node:20-bookworm-slim
24
+
25
+ # Install runtime dependencies: Redis, Java (for execution), and Python
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 production manifests
35
  COPY package.json package-lock.json ./
 
 
36
  COPY packages/shared/package.json ./packages/shared/
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
+ # Copy startup script
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
  ENV PORT=7860
55
  ENV CLIENT_URL=*
56
  ENV REDIS_URL=redis://localhost:6379
57
 
 
 
 
 
58
  # Expose the HF port
59
  EXPOSE 7860
60
 
61
+ # Start Redis and the server
62
  CMD ["./scripts/start-hf.sh"]