File size: 8,411 Bytes
3aea7c6
 
 
 
c764bfc
 
 
 
 
 
 
3aea7c6
 
c764bfc
3aea7c6
 
c764bfc
 
 
 
 
3aea7c6
c764bfc
 
3aea7c6
 
c764bfc
3aea7c6
 
c764bfc
 
3aea7c6
c764bfc
 
3aea7c6
 
c764bfc
 
 
 
 
 
 
 
 
 
3aea7c6
c764bfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3aea7c6
c764bfc
3aea7c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# 🐳 LeRobot Arena Docker Setup

This Docker setup provides a containerized environment that runs both the Python backend and Svelte frontend for LeRobot Arena using modern tools like Bun, uv, and box-packager.

## πŸ—οΈ Architecture

The application consists of:
- **Svelte Frontend**: Built as static files and served by FastAPI
- **FastAPI Backend**: WebSocket server with HTTP API for robot control
- **Single Port**: Both frontend and backend served on port 7860

## πŸš€ Quick Start

### Build and Run with Docker

```bash
# Build the image
docker build -t lerobot-arena .

# Run the container
docker run -p 7860:7860 lerobot-arena

# Access the application
open http://localhost:7860
```

### Using Docker Compose

```bash
# Start the application
docker-compose up

# Access the application
open http://localhost:7860
```

## 🌐 Deployment

### Hugging Face Spaces

This project is configured for deployment on Hugging Face Spaces:

1. Push to a Hugging Face Space repository
2. The Dockerfile will automatically build and serve the application
3. Both frontend and backend run on port 7860 (HF Spaces default)
4. Environment detection automatically configures URLs for HF Spaces

### Local Development vs Production

- **Local Development**: Frontend dev server on 5173, backend on 7860
- **Docker/Production**: Both frontend and backend served by FastAPI on 7860
- **HF Spaces**: Single FastAPI server on 7860 with automatic HTTPS and domain detection

## πŸ”§ Environment Variables

- `SPACE_HOST`: Set automatically by Hugging Face Spaces for proper URL configuration
- `PORT`: Override the default port (7860)

## πŸ“‘ API Endpoints

- **Frontend**: `http://localhost:7860/` (Served by FastAPI)
- **API**: `http://localhost:7860/api/*`
- **WebSocket**: `ws://localhost:7860/ws/*`
- **Status**: `http://localhost:7860/status`

## πŸ› οΈ Development

For local development without Docker:

```bash
# Start backend
cd src-python
uv sync
uv run python start_server.py

# Start frontend (in another terminal)
bun install
bun run dev
```

The frontend dev server (port 5173) will automatically proxy API requests to the backend (port 7860).

## πŸ“‹ What's Included

The Docker container includes:

1. **Frontend**: 
   - Svelte application built as static files using **Bun**
   - Served on port 7860 using Python's built-in HTTP server
   - Production-ready build with all optimizations

2. **Backend**:
   - FastAPI Python server with dependencies managed by **uv**
   - **Standalone executable** created with **box-packager** for faster startup
   - WebSocket support for real-time robot communication
   - Runs on port 8080
   - Auto-configured for container environment

## πŸ› οΈ Development vs Production

### Production (Default)
The Dockerfile builds the Svelte app as static files using Bun and packages the Python backend as a standalone executable using box-packager.

### Development
For development with hot-reload, you can use local tools:

```bash
# Terminal 1: Frontend development
bun run dev

# Terminal 2: Backend development
cd src-python && python start_server.py
```

## πŸ“ Container Structure

```
/home/user/app/
β”œβ”€β”€ src-python/          # Python backend source
β”‚   └── target/release/  # Box-packager standalone executable
β”œβ”€β”€ static-frontend/     # Built Svelte app (static files)
└── start_services.sh    # Startup script for both services
```

## πŸ”§ Build Process

The Docker build process includes these key steps:

1. **Frontend Build Stage (Bun)**:
   - Uses `oven/bun:1-alpine` for fast package management
   - Builds Svelte app with `bun run build`
   - Produces optimized static files

2. **Backend Build Stage (Python + Rust)**:
   - Installs Rust/Cargo for box-packager
   - Uses `uv` for fast Python dependency management  
   - Packages backend with `box package` into standalone executable
   - Configures proper user permissions for HF Spaces

3. **Runtime**:
   - Runs standalone executable (faster startup)
   - Serves frontend static files
   - Both services managed by startup script

## πŸš€ Performance Benefits

### Box-packager Advantages:
- **Faster startup**: No Python interpreter overhead
- **Self-contained**: All dependencies bundled
- **Smaller runtime**: No need for full Python environment
- **Cross-platform**: Single executable works anywhere

### Build Optimization:
- **Bun**: Faster JavaScript package manager and bundler
- **uv**: Ultra-fast Python package manager
- **Multi-stage build**: Minimal final image size
- **Alpine base**: Lightweight frontend build stage

## πŸ”§ Customization

### Environment Variables

You can customize the container behavior using environment variables:

```bash
docker run -p 8080:8080 -p 7860:7860 \
  -e PYTHONUNBUFFERED=1 \
  -e NODE_ENV=production \
  lerobot-arena
```

### Port Configuration

To use different ports:

```bash
# Map to different host ports
docker run -p 9000:8080 -p 3000:7860 lerobot-arena
```

Then access:
- Frontend: http://localhost:3000
- Backend: http://localhost:9000

### Volume Mounts for Persistence

```bash
# Mount data directory for persistence
docker run -p 8080:8080 -p 7860:7860 \
  -v $(pwd)/data:/home/user/app/data \
  lerobot-arena
```

## πŸ› Troubleshooting

### Container won't start
```bash
# Check logs
docker-compose logs lerobot-arena

# Or for direct docker run
docker logs <container-id>
```

### Port conflicts
```bash
# Check what's using the ports
lsof -i :8080
lsof -i :7860

# Kill processes or use different ports
docker run -p 8081:8080 -p 7861:7860 lerobot-arena
```

### Frontend not loading
```bash
# Verify the frontend was built correctly
docker exec -it <container-id> ls -la /home/user/app/static-frontend

# Check if frontend server is running
docker exec -it <container-id> ps aux | grep python
```

### Backend API errors
```bash
# Check if standalone executable is running
docker exec -it <container-id> ps aux | grep lerobot-arena-server

# Test backend directly
curl http://localhost:8080/
```

### Box-packager Build Issues
```bash
# Force rebuild without cache to fix cargo/box issues
docker-compose build --no-cache
docker-compose up

# Check if Rust/Cargo is properly installed
docker exec -it <container-id> cargo --version
```

## πŸ”„ Updates and Rebuilding

```bash
# Pull latest code and rebuild
git pull
docker-compose down
docker-compose up --build

# Force rebuild without cache
docker-compose build --no-cache
docker-compose up
```

## πŸš€ Hugging Face Spaces Deployment

This Docker setup is optimized for Hugging Face Spaces:

### Key Features for HF Spaces:
- **Port 7860**: Uses the default HF Spaces port
- **User permissions**: Runs as user ID 1000 as required
- **Proper ownership**: All files owned by the user
- **Git support**: Includes git for dependency resolution
- **Standalone executable**: Faster cold starts on HF infrastructure

### Deployment Steps:
1. Push your code to a GitHub repository
2. Create a new Space on Hugging Face Spaces
3. Connect your GitHub repository
4. The YAML frontmatter in README.md will auto-configure the Space
5. HF will build and deploy your Docker container automatically

### Accessing on HF Spaces:
- Your Space URL will serve the frontend directly
- Backend API will be available at your-space-url/api/ (if using a reverse proxy)

## πŸ”§ Advanced Configuration

### Using with nginx (Production)
```nginx
server {
    listen 80;
    
    # Serve frontend
    location / {
        proxy_pass http://localhost:7860;
    }
    
    # Proxy API calls
    location /api/ {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    
    # WebSocket support
    location /ws/ {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
```

## πŸ“Š Container Stats

```bash
# Monitor resource usage
docker stats lerobot-arena

# View container info
docker inspect lerobot-arena
```

## 🧹 Cleanup

```bash
# Stop and remove containers
docker-compose down

# Remove images
docker rmi lerobot-arena

# Clean up unused images and containers
docker system prune -a
```

---

**Happy containerized robotics! πŸ€–πŸ³**