anuragshas commited on
Commit
6c18509
·
1 Parent(s): 62d7668

add: huggingface docker

Browse files
Files changed (5) hide show
  1. Dockerfile.hf +44 -0
  2. README-HF.md +178 -0
  3. deploy-hf.sh +37 -0
  4. docker-compose.hf.yml +44 -0
  5. nginx.hf.conf +64 -0
Dockerfile.hf ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Stage 1: Build stage
2
+ FROM node:18-alpine AS builder
3
+
4
+ # Set working directory
5
+ WORKDIR /app
6
+
7
+ # Copy package files
8
+ COPY package*.json ./
9
+
10
+ # Install dependencies
11
+ RUN npm ci --only=production=false
12
+
13
+ # Copy source code
14
+ COPY . .
15
+
16
+ # Build the application
17
+ RUN npm run build
18
+
19
+ # Stage 2: Production stage for Hugging Face Spaces
20
+ FROM nginx:alpine AS production
21
+
22
+ # Install wget for healthcheck
23
+ RUN apk add --no-cache wget
24
+
25
+ # Copy built application from builder stage
26
+ COPY --from=builder /app/dist /usr/share/nginx/html
27
+
28
+ # Copy custom nginx configuration for HF Spaces
29
+ COPY nginx.hf.conf /etc/nginx/nginx.conf
30
+
31
+ # Create nginx directories with proper permissions
32
+ RUN mkdir -p /var/cache/nginx/client_temp \
33
+ /var/cache/nginx/proxy_temp \
34
+ /var/cache/nginx/fastcgi_temp \
35
+ /var/cache/nginx/uwsgi_temp \
36
+ /var/cache/nginx/scgi_temp && \
37
+ chown -R nginx:nginx /var/cache/nginx && \
38
+ chmod -R 755 /var/cache/nginx
39
+
40
+ # Expose port 7860 (required by Hugging Face Spaces)
41
+ EXPOSE 7860
42
+
43
+ # Start nginx on port 7860
44
+ CMD ["nginx", "-g", "daemon off;"]
README-HF.md ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hugging Face Spaces Deployment Guide
2
+
3
+ This guide explains how to deploy your Invoice Generator application to Hugging Face Spaces.
4
+
5
+ ## Prerequisites
6
+
7
+ - Hugging Face account
8
+ - Git installed locally
9
+ - Application built and tested locally
10
+
11
+ ## Quick Start
12
+
13
+ ### 1. Test Locally with HF Configuration
14
+
15
+ First, test your application with the Hugging Face Spaces configuration:
16
+
17
+ ```bash
18
+ # Make the deployment script executable
19
+ chmod +x deploy-hf.sh
20
+
21
+ # Deploy locally using HF configuration
22
+ ./deploy-hf.sh
23
+ ```
24
+
25
+ Your app will be available at `http://localhost:7860`
26
+
27
+ ### 2. Create a New Space on Hugging Face
28
+
29
+ 1. Go to https://huggingface.co/new-space
30
+ 2. Choose a name for your space
31
+ 3. Set the **SDK** to `Docker`
32
+ 4. Set visibility (Public/Private)
33
+ 5. Click "Create Space"
34
+
35
+ ### 3. Prepare Files for Upload
36
+
37
+ You need to upload these files to your Hugging Face Space:
38
+
39
+ **Required Files:**
40
+ - `Dockerfile` (rename `Dockerfile.hf` to `Dockerfile`)
41
+ - `nginx.hf.conf`
42
+ - `package.json`
43
+ - `package-lock.json`
44
+ - `index.html`
45
+ - `script.js`
46
+ - `style.css`
47
+ - `.dockerignore`
48
+
49
+ **Optional Files:**
50
+ - `README.md` (Space description)
51
+ - Any other assets your app needs
52
+
53
+ ### 4. Upload to Hugging Face Spaces
54
+
55
+ #### Option A: Web Interface
56
+ 1. Go to your newly created Space
57
+ 2. Click "Files" tab
58
+ 3. Upload all required files
59
+ 4. Rename `Dockerfile.hf` to `Dockerfile`
60
+
61
+ #### Option B: Git (Recommended)
62
+ ```bash
63
+ # Clone your space repository
64
+ git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
65
+ cd YOUR_SPACE_NAME
66
+
67
+ # Copy required files
68
+ cp ../path/to/your/project/Dockerfile.hf ./Dockerfile
69
+ cp ../path/to/your/project/nginx.hf.conf ./
70
+ cp ../path/to/your/project/package*.json ./
71
+ cp ../path/to/your/project/index.html ./
72
+ cp ../path/to/your/project/script.js ./
73
+ cp ../path/to/your/project/style.css ./
74
+ cp ../path/to/your/project/.dockerignore ./
75
+
76
+ # Commit and push
77
+ git add .
78
+ git commit -m "Initial deployment of Invoice Generator"
79
+ git push
80
+ ```
81
+
82
+ ### 5. Configure Space Settings
83
+
84
+ In your Space settings, ensure:
85
+ - **SDK**: Docker
86
+ - **Port**: 7860 (automatically detected)
87
+ - **Hardware**: CPU Basic (or upgrade if needed)
88
+
89
+ ## File Structure for Hugging Face Spaces
90
+
91
+ ```
92
+ your-space/
93
+ ├── Dockerfile # Renamed from Dockerfile.hf
94
+ ├── nginx.hf.conf # Nginx config for port 7860
95
+ ├── package.json # Node.js dependencies
96
+ ├── package-lock.json # Locked versions
97
+ ├── index.html # Main HTML file
98
+ ├── script.js # JavaScript code
99
+ ├── style.css # Styles
100
+ ├── .dockerignore # Docker ignore rules
101
+ └── README.md # Space description (optional)
102
+ ```
103
+
104
+ ## Key Differences from Standard Deployment
105
+
106
+ ### Port Configuration
107
+ - **Standard**: Port 80/8080
108
+ - **Hugging Face Spaces**: Port 7860 (required)
109
+
110
+ ### Dockerfile Changes
111
+ - Uses `Dockerfile.hf` which configures nginx for port 7860
112
+ - Includes `wget` for health checks
113
+ - Exposes port 7860
114
+
115
+ ### Nginx Configuration
116
+ - `nginx.hf.conf` listens on port 7860
117
+ - Includes health check endpoint at `/health`
118
+ - Optimized for Hugging Face Spaces environment
119
+
120
+ ## Troubleshooting
121
+
122
+ ### Build Failures
123
+ 1. Check that all required files are uploaded
124
+ 2. Verify `package.json` has all dependencies
125
+ 3. Check Dockerfile syntax
126
+
127
+ ### Runtime Issues
128
+ 1. Check Space logs in the Hugging Face interface
129
+ 2. Ensure port 7860 is properly configured
130
+ 3. Verify nginx configuration
131
+
132
+ ### Common Issues
133
+
134
+ **Issue**: Space shows "Building" for too long
135
+ **Solution**: Check for missing dependencies in package.json
136
+
137
+ **Issue**: Application not accessible
138
+ **Solution**: Verify nginx is listening on port 7860, not 80
139
+
140
+ **Issue**: Static files not loading
141
+ **Solution**: Check file paths and nginx static file configuration
142
+
143
+ ## Health Check
144
+
145
+ Your deployed Space will have a health check endpoint at:
146
+ ```
147
+ https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/health
148
+ ```
149
+
150
+ ## Production Considerations
151
+
152
+ ### Performance
153
+ - Consider upgrading to CPU/GPU hardware if needed
154
+ - Monitor Space usage and logs
155
+ - Optimize bundle size for faster builds
156
+
157
+ ### Security
158
+ - Review Content Security Policy headers
159
+ - Ensure no sensitive data in client-side code
160
+ - Use HTTPS (automatically provided by HF Spaces)
161
+
162
+ ### Monitoring
163
+ - Check Space analytics in Hugging Face dashboard
164
+ - Monitor build times and resource usage
165
+ - Set up alerts for Space downtime
166
+
167
+ ## Support
168
+
169
+ - Hugging Face Spaces Documentation: https://huggingface.co/docs/hub/spaces
170
+ - Community Forum: https://discuss.huggingface.co/
171
+ - Discord: https://discord.gg/hugging-face
172
+
173
+ ## Next Steps
174
+
175
+ 1. Test your Space thoroughly after deployment
176
+ 2. Update README.md with Space-specific information
177
+ 3. Share your Space with the community
178
+ 4. Consider adding a demo or usage instructions
deploy-hf.sh ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Build and deploy script for invoice generator on Hugging Face Spaces
4
+
5
+ set -e
6
+
7
+ echo "🤗 Starting Hugging Face Spaces deployment process..."
8
+
9
+ # Stop existing services
10
+ echo "🛑 Stopping existing services..."
11
+ docker-compose -f docker-compose.hf.yml down 2>/dev/null || true
12
+
13
+ # Build and start the Hugging Face Spaces service
14
+ echo "📦 Building and starting services for HF Spaces..."
15
+ docker-compose -f docker-compose.hf.yml up --build -d
16
+
17
+ # Show status
18
+ echo "📊 Service status:"
19
+ docker-compose -f docker-compose.hf.yml ps
20
+
21
+ echo "✅ Hugging Face Spaces deployment completed successfully!"
22
+ echo "🌐 Application is available at: http://localhost:7860"
23
+ echo "🤗 Ready to deploy to Hugging Face Spaces!"
24
+
25
+ # Optional: Show logs
26
+ echo "📋 Recent logs:"
27
+ docker-compose -f docker-compose.hf.yml logs --tail=20 invoice-generator
28
+
29
+ echo ""
30
+ echo "📝 Next steps for Hugging Face Spaces:"
31
+ echo "1. Create a new Space on Hugging Face"
32
+ echo "2. Upload these files to your Space:"
33
+ echo " - Dockerfile.hf (rename to Dockerfile)"
34
+ echo " - nginx.hf.conf"
35
+ echo " - All your source files (package.json, src/, etc.)"
36
+ echo "3. Set Space SDK to 'docker'"
37
+ echo "4. Your app will be available on port 7860"
docker-compose.hf.yml ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ services:
2
+ invoice-generator:
3
+ build:
4
+ context: .
5
+ dockerfile: Dockerfile.hf
6
+ target: production
7
+ ports:
8
+ - "7860:7860"
9
+ restart: unless-stopped
10
+ container_name: invoice-generator-hf
11
+ environment:
12
+ - NODE_ENV=production
13
+ - PORT=7860
14
+ labels:
15
+ - "com.docker.compose.service=invoice-generator"
16
+ - "com.docker.compose.environment=huggingface"
17
+ logging:
18
+ driver: "json-file"
19
+ options:
20
+ max-size: "10m"
21
+ max-file: "3"
22
+ healthcheck:
23
+ test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:7860/"]
24
+ interval: 30s
25
+ timeout: 10s
26
+ retries: 3
27
+ start_period: 40s
28
+ deploy:
29
+ resources:
30
+ limits:
31
+ memory: 512M
32
+ cpus: "0.5"
33
+ reservations:
34
+ memory: 256M
35
+ cpus: "0.25"
36
+ security_opt:
37
+ - no-new-privileges:true
38
+ read_only: true
39
+ tmpfs:
40
+ - /tmp
41
+ - /var/cache/nginx
42
+ - /var/run
43
+ - /run
44
+ - /var/log/nginx
nginx.hf.conf ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ events {
2
+ worker_connections 1024;
3
+ }
4
+
5
+ http {
6
+ include /etc/nginx/mime.types;
7
+ default_type application/octet-stream;
8
+
9
+ # Logging
10
+ access_log /var/log/nginx/access.log;
11
+ error_log /var/log/nginx/error.log;
12
+
13
+ # Gzip compression
14
+ gzip on;
15
+ gzip_vary on;
16
+ gzip_min_length 1024;
17
+ gzip_proxied any;
18
+ gzip_comp_level 6;
19
+ gzip_types
20
+ text/plain
21
+ text/css
22
+ text/xml
23
+ text/javascript
24
+ application/javascript
25
+ application/xml+rss
26
+ application/json;
27
+
28
+ # Security headers
29
+ add_header X-Frame-Options "SAMEORIGIN" always;
30
+ add_header X-XSS-Protection "1; mode=block" always;
31
+ add_header X-Content-Type-Options "nosniff" always;
32
+ add_header Referrer-Policy "no-referrer-when-downgrade" always;
33
+ add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
34
+
35
+ server {
36
+ listen 7860;
37
+ server_name _;
38
+ root /usr/share/nginx/html;
39
+ index index.html;
40
+
41
+ # Cache static assets
42
+ location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
43
+ expires 1y;
44
+ add_header Cache-Control "public, immutable";
45
+ }
46
+
47
+ # Handle SPA routing
48
+ location / {
49
+ try_files $uri $uri/ /index.html;
50
+ }
51
+
52
+ # Health check endpoint for HF Spaces
53
+ location /health {
54
+ access_log off;
55
+ return 200 "healthy\n";
56
+ add_header Content-Type text/plain;
57
+ }
58
+
59
+ # Security
60
+ location ~ /\. {
61
+ deny all;
62
+ }
63
+ }
64
+ }