Phoenix21 commited on
Commit
37f588a
·
verified ·
1 Parent(s): e27cd9e

🐳 Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +42 -115
Dockerfile CHANGED
@@ -1,142 +1,69 @@
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
- # Build frontend with error checking
21
- # Note: VITE_API_BASE_URL is intentionally NOT set here
22
- # The frontend will use its default fallback: import.meta.env.VITE_API_BASE_URL || 'http://localhost:8080'
23
- # Since we're in production, the frontend will make API calls to /api/* which nginx proxies to localhost:8080
24
- RUN echo "=== Building frontend with Vite ===" && \
25
- npm run build 2>&1 | tee /tmp/build.log && \
26
- echo "=== Build complete ===" && \
27
- echo "=== Verifying build output ===" && \
28
- ls -laR dist/ && \
29
- echo "=== Checking assets folder ===" && \
30
- ls -la dist/assets/ && \
31
- ASSET_COUNT=$(find dist/assets -type f | wc -l) && \
32
- echo "Assets folder contains $ASSET_COUNT files" && \
33
- if [ "$ASSET_COUNT" -lt 1 ]; then \
34
- echo "ERROR: Frontend build failed - no assets generated!" && \
35
- echo "=== Build log ===" && \
36
- cat /tmp/build.log && \
37
- exit 1; \
38
- fi && \
39
- echo "=== Frontend build verification passed ==="
40
-
41
- # ============================================
42
- # Stage 2: Build Backend
43
- # ============================================
44
- FROM maven:3.9.6-eclipse-temurin-17 AS backend-builder
45
-
46
- WORKDIR /app/backend
47
 
48
- # Copy pom.xml first for dependency caching
49
  COPY backend/pom.xml ./
50
 
51
- # Download dependencies
52
- RUN mvn dependency:go-offline -B
53
 
54
- # Copy backend source
55
  COPY backend/src ./src
56
 
57
- # Build backend (skip tests for faster build)
58
  RUN mvn clean package -DskipTests
59
 
60
  # ============================================
61
- # Stage 3: Production Runtime
62
  # ============================================
63
  FROM eclipse-temurin:17-jre-jammy
64
 
65
  WORKDIR /app
66
 
67
- # Install nginx and diagnostic tools for serving frontend
68
  RUN apt-get update && \
69
- apt-get install -y nginx curl netcat-openbsd net-tools && \
70
  rm -rf /var/lib/apt/lists/*
71
 
72
- # Remove default nginx configuration to avoid conflicts
73
- RUN rm -f /etc/nginx/sites-enabled/default && \
74
- rm -f /etc/nginx/sites-available/default
75
-
76
- # Copy built frontend from frontend-builder stage
77
- COPY --from=frontend-builder /app/frontend/dist /usr/share/nginx/html
78
-
79
- # Verify frontend files were copied and fix permissions
80
- RUN echo "=== Verifying frontend build ===" && \
81
- ls -la /usr/share/nginx/html && \
82
- echo "=== File count: $(find /usr/share/nginx/html -type f | wc -l) ===" && \
83
- chmod -R 755 /usr/share/nginx/html && \
84
- chown -R root:root /usr/share/nginx/html
85
-
86
- # Copy nginx configuration directly to sites-available and enable it
87
- COPY frontend/nginx.conf /etc/nginx/sites-available/locallend.conf
88
- RUN ln -s /etc/nginx/sites-available/locallend.conf /etc/nginx/sites-enabled/locallend.conf
89
 
90
- # Copy built backend JAR
91
- COPY --from=backend-builder /app/backend/target/*.jar /app/backend.jar
92
-
93
- # Create a startup script using echo to avoid heredoc parsing issues
94
- RUN echo '#!/bin/bash' > /app/start.sh && \
95
- echo 'set -e' >> /app/start.sh && \
96
- echo '' >> /app/start.sh && \
97
- echo 'echo "=== Starting LocalLend Application ==="' >> /app/start.sh && \
98
- echo 'echo "=== Diagnostic Information ==="' >> /app/start.sh && \
99
- echo 'echo "Hostname: $(hostname)"' >> /app/start.sh && \
100
- echo 'echo "IP Address: $(hostname -i)"' >> /app/start.sh && \
101
- echo '' >> /app/start.sh && \
102
- echo '# Verify frontend files' >> /app/start.sh && \
103
- echo 'echo "=== Frontend Files Check ==="' >> /app/start.sh && \
104
- echo 'ls -la /usr/share/nginx/html/ | head -20' >> /app/start.sh && \
105
- echo 'echo "Total files: $(find /usr/share/nginx/html -type f | wc -l)"' >> /app/start.sh && \
106
- echo '' >> /app/start.sh && \
107
- echo '# Test nginx configuration' >> /app/start.sh && \
108
- echo 'echo "=== Testing nginx configuration ==="' >> /app/start.sh && \
109
- echo 'nginx -t' >> /app/start.sh && \
110
- echo '' >> /app/start.sh && \
111
- echo '# Start nginx in background' >> /app/start.sh && \
112
- echo 'echo "=== Starting nginx on 0.0.0.0:7860 ==="' >> /app/start.sh && \
113
- echo 'nginx -g "daemon off;" &' >> /app/start.sh && \
114
- echo 'NGINX_PID=$!' >> /app/start.sh && \
115
- echo 'echo "Nginx started with PID: $NGINX_PID"' >> /app/start.sh && \
116
- echo '' >> /app/start.sh && \
117
- echo '# Wait for nginx to fully start' >> /app/start.sh && \
118
- echo 'sleep 3' >> /app/start.sh && \
119
- echo '' >> /app/start.sh && \
120
- echo '# Verify nginx is running and responding' >> /app/start.sh && \
121
- echo 'echo "=== Verifying nginx is responding ==="' >> /app/start.sh && \
122
- echo 'ps aux | grep nginx | grep -v grep || echo "WARNING: nginx process not found!"' >> /app/start.sh && \
123
- echo 'netstat -tulpn | grep :7860 || echo "WARNING: Port 7860 not listening!"' >> /app/start.sh && \
124
- 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 && \
125
- 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 && \
126
- echo 'echo "=== Nginx verification complete ==="' >> /app/start.sh && \
127
- echo '' >> /app/start.sh && \
128
- echo '# Start Spring Boot application in foreground' >> /app/start.sh && \
129
- echo 'echo "=== Starting Spring Boot on 0.0.0.0:8080 ==="' >> /app/start.sh && \
130
- echo 'exec java -Dserver.address=0.0.0.0 -Dserver.port=8080 -jar /app/backend.jar' >> /app/start.sh && \
131
- chmod +x /app/start.sh
132
-
133
- # Hugging Face Spaces expects the app to run on port 7860
134
- # Nginx will listen on 7860 and proxy to backend on 8080
135
  EXPOSE 7860
136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  # Set environment variables
138
- ENV SERVER_PORT=8080
139
  ENV SPRING_PROFILES_ACTIVE=prod
140
 
141
- # Start both nginx and Spring Boot
142
- CMD ["/app/start.sh"]
 
1
+ # Dockerfile for LocalLend - Spring Boot with Static HTML/CSS/JS Frontend
2
+ # This builds a single JAR with embedded static frontend resources
3
 
4
  # ============================================
5
+ # Stage 1: Build Spring Boot Application
6
  # ============================================
7
+ FROM maven:3.9-eclipse-temurin-17 AS backend-builder
8
 
9
+ WORKDIR /app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ # Copy backend pom.xml
12
  COPY backend/pom.xml ./
13
 
14
+ # Download dependencies for caching
15
+ RUN mvn dependency:go-offline -B || true
16
 
17
+ # Copy backend source (which includes static resources in src/main/resources/static/)
18
  COPY backend/src ./src
19
 
20
+ # Build backend with static resources included
21
  RUN mvn clean package -DskipTests
22
 
23
  # ============================================
24
+ # Stage 2: Production Runtime
25
  # ============================================
26
  FROM eclipse-temurin:17-jre-jammy
27
 
28
  WORKDIR /app
29
 
30
+ # Install utilities for debugging
31
  RUN apt-get update && \
32
+ apt-get install -y curl netcat-openbsd net-tools && \
33
  rm -rf /var/lib/apt/lists/*
34
 
35
+ # Copy the backend JAR (contains static resources)
36
+ COPY --from=backend-builder /app/target/*.jar /app/backend.jar
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ # Expose port 7860 (required by Hugging Face Spaces)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  EXPOSE 7860
40
 
41
+ # Create startup script
42
+ RUN echo '#!/bin/bash' > /app/startup.sh && \
43
+ echo 'echo "===== Application Startup at $(date) ====="' >> /app/startup.sh && \
44
+ echo 'echo ""' >> /app/startup.sh && \
45
+ echo 'echo "=== Starting LocalLend Application ==="' >> /app/startup.sh && \
46
+ echo 'echo "=== Diagnostic Information ==="' >> /app/startup.sh && \
47
+ echo 'echo "Hostname: $(hostname)"' >> /app/startup.sh && \
48
+ echo 'echo "IP Address: $(hostname -I)"' >> /app/startup.sh && \
49
+ echo 'echo ""' >> /app/startup.sh && \
50
+ echo 'echo "=== Static Resources Check ==="' >> /app/startup.sh && \
51
+ echo 'if [ -f /app/backend.jar ]; then' >> /app/startup.sh && \
52
+ echo ' echo "JAR file exists"' >> /app/startup.sh && \
53
+ echo ' unzip -l /app/backend.jar | grep "BOOT-INF/classes/static/" | head -10 || echo "No static resources found in JAR"' >> /app/startup.sh && \
54
+ echo 'fi' >> /app/startup.sh && \
55
+ echo 'echo ""' >> /app/startup.sh && \
56
+ echo 'echo "=== Starting Spring Boot on 0.0.0.0:7860 ==="' >> /app/startup.sh && \
57
+ echo 'echo ""' >> /app/startup.sh && \
58
+ echo 'exec java -Dserver.address=0.0.0.0 \\' >> /app/startup.sh && \
59
+ echo ' -Dserver.port=7860 \\' >> /app/startup.sh && \
60
+ echo ' -Dspring.profiles.active=prod \\' >> /app/startup.sh && \
61
+ echo ' -jar /app/backend.jar' >> /app/startup.sh && \
62
+ chmod +x /app/startup.sh
63
+
64
  # Set environment variables
65
+ ENV SERVER_PORT=7860
66
  ENV SPRING_PROFILES_ACTIVE=prod
67
 
68
+ # Start Spring Boot with embedded static resources
69
+ CMD ["/app/startup.sh"]