File size: 2,673 Bytes
ece880f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68c1d18
 
 
ece880f
68c1d18
ece880f
 
68c1d18
 
 
 
 
 
 
 
ece880f
 
68c1d18
 
ece880f
68c1d18
ece880f
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Multi-stage Dockerfile for WagerKit (Hugging Face Spaces Compatible)

# Stage 1: Build Backend
FROM node:20-alpine AS backend-build

WORKDIR /app/backend

# Copy backend package files
COPY backend/package*.json ./
RUN npm ci --only=production

# Copy backend source and build
COPY backend ./
RUN npm run build

# Stage 2: Build Frontend
FROM node:20-alpine AS frontend-build

WORKDIR /app/frontend

# Copy frontend package files
COPY frontend/package*.json ./
RUN npm ci

# Copy frontend source
COPY frontend ./

# Build Next.js frontend (standalone mode)
ENV NEXT_PUBLIC_API_URL=/api
RUN npm run build

# Stage 3: Production Runtime
FROM node:20-alpine

WORKDIR /app

# Install dependencies for Redis and process management
RUN apk add --no-cache redis bash

# Copy backend production files
COPY --from=backend-build /app/backend/dist ./backend/dist
COPY --from=backend-build /app/backend/node_modules ./backend/node_modules
COPY --from=backend-build /app/backend/package.json ./backend/

# Copy backend .env (if exists)
COPY backend/.env ./backend/.env 2>/dev/null || true

# Copy frontend production files (standalone build)
COPY --from=frontend-build /app/frontend/.next/standalone ./frontend/
COPY --from=frontend-build /app/frontend/.next/static ./frontend/.next/static
COPY --from=frontend-build /app/frontend/public ./frontend/public
COPY --from=frontend-build /app/frontend/package.json ./frontend/

# Environment variables for Hugging Face Spaces
ENV NODE_ENV=production
ENV PORT=7860
ENV REDIS_HOST=127.0.0.1
ENV REDIS_PORT=6379
ENV NEXT_PUBLIC_API_URL=/api

# Expose Hugging Face Spaces default port
EXPOSE 7860

# Create comprehensive startup script
RUN cat > /app/start.sh << 'EOF'
#!/bin/bash
set -e

echo "πŸš€ Starting WagerKit on Hugging Face Spaces..."

# Start Redis in background
echo "πŸ“¦ Starting Redis..."
redis-server --daemonize yes --bind 127.0.0.1 --port 6379 --loglevel warning
sleep 2

# Verify Redis is running
redis-cli ping > /dev/null
echo "βœ… Redis is ready"

# Start Backend in background on port 3001
# Use BACKEND_PORT to avoid conflict with the frontend PORT env var
echo "πŸ”§ Starting NestJS backend on port 3001..."
cd /app/backend
BACKEND_PORT=3001 node dist/main.js &
BACKEND_PID=$!
sleep 3

# Verify backend is running
if kill -0 $BACKEND_PID 2>/dev/null; then
  echo "βœ… Backend started (PID: $BACKEND_PID)"
else
  echo "❌ Backend failed to start!"
  exit 1
fi

# Start Frontend (foreground - keeps container alive)
# PORT=7860 is already set via Docker ENV
echo "🌐 Starting Next.js frontend on port ${PORT}..."
cd /app/frontend
HOSTNAME=0.0.0.0 node server.js

EOF

RUN chmod +x /app/start.sh

CMD ["/app/start.sh"]