Phoenix21 commited on
Commit
8797c15
·
verified ·
1 Parent(s): d794d5a

🐳 Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +133 -14
Dockerfile CHANGED
@@ -1,25 +1,144 @@
1
- # Backend-only Dockerfile for Hugging Face Spaces
2
- # This deploys ONLY the Spring Boot backend on port 7860
3
- # Frontend will be added later once build issue is resolved
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  FROM eclipse-temurin:17-jre-jammy
6
 
7
  WORKDIR /app
8
 
9
- # Copy the pre-built backend JAR
10
- COPY backend/target/*.jar app.jar
 
 
 
 
 
 
 
 
 
11
 
12
- # Hugging Face Spaces requires port 7860
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  EXPOSE 7860
14
 
15
  # Set environment variables
16
- # Backend will run on port 7860 instead of 8080
17
- ENV SERVER_PORT=7860
18
  ENV SPRING_PROFILES_ACTIVE=prod
19
 
20
- # Health check endpoint for HF Spaces
21
- HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
22
- CMD curl -f http://localhost:7860/api/categories || exit 1
23
-
24
- # Run the Spring Boot application
25
- ENTRYPOINT ["java", "-jar", "/app/app.jar"]
 
1
+ # Multi-stage Dockerfile for Hugging Face Spaces deployment
2
+ # Combines frontend and backend into a single container
 
3
 
4
+ # ============================================
5
+ # Stage 1: Build Frontend
6
+ # ============================================
7
+ FROM node:20 AS frontend-builder
8
+
9
+ WORKDIR /app/frontend
10
+
11
+ # Copy frontend package files
12
+ COPY frontend/package*.json ./
13
+
14
+ # Install dependencies
15
+ RUN npm install --legacy-peer-deps
16
+
17
+ # Copy frontend source
18
+ COPY frontend/ ./
19
+
20
+ # Set build-time environment variable for API URL
21
+ # Empty string because frontend services already include /api prefix in their calls
22
+ ARG VITE_API_BASE_URL=
23
+ ENV VITE_API_BASE_URL=$VITE_API_BASE_URL
24
+
25
+ # Build frontend with error checking
26
+ RUN echo "=== Building frontend with Vite ===" && \
27
+ npm run build 2>&1 | tee /tmp/build.log && \
28
+ echo "=== Build complete ===" && \
29
+ echo "=== Verifying build output ===" && \
30
+ ls -laR dist/ && \
31
+ echo "=== Checking assets folder ===" && \
32
+ ls -la dist/assets/ && \
33
+ ASSET_COUNT=$(find dist/assets -type f | wc -l) && \
34
+ echo "Assets folder contains $ASSET_COUNT files" && \
35
+ if [ "$ASSET_COUNT" -lt 1 ]; then \
36
+ echo "ERROR: Frontend build failed - no assets generated!" && \
37
+ echo "=== Build log ===" && \
38
+ cat /tmp/build.log && \
39
+ exit 1; \
40
+ fi && \
41
+ echo "=== Frontend build verification passed ==="
42
+
43
+ # ============================================
44
+ # Stage 2: Build Backend
45
+ # ============================================
46
+ FROM maven:3.9.6-eclipse-temurin-17 AS backend-builder
47
+
48
+ WORKDIR /app/backend
49
+
50
+ # Copy pom.xml first for dependency caching
51
+ COPY backend/pom.xml ./
52
+
53
+ # Download dependencies
54
+ RUN mvn dependency:go-offline -B
55
+
56
+ # Copy backend source
57
+ COPY backend/src ./src
58
+
59
+ # Build backend (skip tests for faster build)
60
+ RUN mvn clean package -DskipTests
61
+
62
+ # ============================================
63
+ # Stage 3: Production Runtime
64
+ # ============================================
65
  FROM eclipse-temurin:17-jre-jammy
66
 
67
  WORKDIR /app
68
 
69
+ # Install nginx and diagnostic tools for serving frontend
70
+ RUN apt-get update && \
71
+ apt-get install -y nginx curl netcat-openbsd net-tools && \
72
+ rm -rf /var/lib/apt/lists/*
73
+
74
+ # Remove default nginx configuration to avoid conflicts
75
+ RUN rm -f /etc/nginx/sites-enabled/default && \
76
+ rm -f /etc/nginx/sites-available/default
77
+
78
+ # Copy built frontend from frontend-builder stage
79
+ COPY --from=frontend-builder /app/frontend/dist /usr/share/nginx/html
80
 
81
+ # Verify frontend files were copied and fix permissions
82
+ RUN echo "=== Verifying frontend build ===" && \
83
+ ls -la /usr/share/nginx/html && \
84
+ echo "=== File count: $(find /usr/share/nginx/html -type f | wc -l) ===" && \
85
+ chmod -R 755 /usr/share/nginx/html && \
86
+ chown -R root:root /usr/share/nginx/html
87
+
88
+ # Copy nginx configuration directly to sites-available and enable it
89
+ COPY frontend/nginx.conf /etc/nginx/sites-available/locallend.conf
90
+ RUN ln -s /etc/nginx/sites-available/locallend.conf /etc/nginx/sites-enabled/locallend.conf
91
+
92
+ # Copy built backend JAR
93
+ COPY --from=backend-builder /app/backend/target/*.jar /app/backend.jar
94
+
95
+ # Create a startup script using echo to avoid heredoc parsing issues
96
+ RUN echo '#!/bin/bash' > /app/start.sh && \
97
+ echo 'set -e' >> /app/start.sh && \
98
+ echo '' >> /app/start.sh && \
99
+ echo 'echo "=== Starting LocalLend Application ==="' >> /app/start.sh && \
100
+ echo 'echo "=== Diagnostic Information ==="' >> /app/start.sh && \
101
+ echo 'echo "Hostname: $(hostname)"' >> /app/start.sh && \
102
+ echo 'echo "IP Address: $(hostname -i)"' >> /app/start.sh && \
103
+ echo '' >> /app/start.sh && \
104
+ echo '# Verify frontend files' >> /app/start.sh && \
105
+ echo 'echo "=== Frontend Files Check ==="' >> /app/start.sh && \
106
+ echo 'ls -la /usr/share/nginx/html/ | head -20' >> /app/start.sh && \
107
+ echo 'echo "Total files: $(find /usr/share/nginx/html -type f | wc -l)"' >> /app/start.sh && \
108
+ echo '' >> /app/start.sh && \
109
+ echo '# Test nginx configuration' >> /app/start.sh && \
110
+ echo 'echo "=== Testing nginx configuration ==="' >> /app/start.sh && \
111
+ echo 'nginx -t' >> /app/start.sh && \
112
+ echo '' >> /app/start.sh && \
113
+ echo '# Start nginx in background' >> /app/start.sh && \
114
+ echo 'echo "=== Starting nginx on 0.0.0.0:7860 ==="' >> /app/start.sh && \
115
+ echo 'nginx -g "daemon off;" &' >> /app/start.sh && \
116
+ echo 'NGINX_PID=$!' >> /app/start.sh && \
117
+ echo 'echo "Nginx started with PID: $NGINX_PID"' >> /app/start.sh && \
118
+ echo '' >> /app/start.sh && \
119
+ echo '# Wait for nginx to fully start' >> /app/start.sh && \
120
+ echo 'sleep 3' >> /app/start.sh && \
121
+ echo '' >> /app/start.sh && \
122
+ echo '# Verify nginx is running and responding' >> /app/start.sh && \
123
+ echo 'echo "=== Verifying nginx is responding ==="' >> /app/start.sh && \
124
+ echo 'ps aux | grep nginx | grep -v grep || echo "WARNING: nginx process not found!"' >> /app/start.sh && \
125
+ echo 'netstat -tulpn | grep :7860 || echo "WARNING: Port 7860 not listening!"' >> /app/start.sh && \
126
+ echo 'curl -s -o /dev/null -w "Localhost:7860 HTTP Status: %{http_code}\n" http://localhost:7860/health || echo "WARNING: Nginx not responding on localhost:7860!"' >> /app/start.sh && \
127
+ echo 'curl -s -o /dev/null -w "0.0.0.0:7860 HTTP Status: %{http_code}\n" http://0.0.0.0:7860/health || echo "WARNING: Nginx not responding on 0.0.0.0:7860!"' >> /app/start.sh && \
128
+ echo 'echo "=== Nginx verification complete ==="' >> /app/start.sh && \
129
+ echo '' >> /app/start.sh && \
130
+ echo '# Start Spring Boot application in foreground' >> /app/start.sh && \
131
+ echo 'echo "=== Starting Spring Boot on 0.0.0.0:8080 ==="' >> /app/start.sh && \
132
+ echo 'exec java -Dserver.address=0.0.0.0 -Dserver.port=8080 -jar /app/backend.jar' >> /app/start.sh && \
133
+ chmod +x /app/start.sh
134
+
135
+ # Hugging Face Spaces expects the app to run on port 7860
136
+ # Nginx will listen on 7860 and proxy to backend on 8080
137
  EXPOSE 7860
138
 
139
  # Set environment variables
140
+ ENV SERVER_PORT=8080
 
141
  ENV SPRING_PROFILES_ACTIVE=prod
142
 
143
+ # Start both nginx and Spring Boot
144
+ CMD ["/app/start.sh"]