Files
foxhunt/docs/guides/DOCKER_BUILD_GUIDE.md
jgrusewski e393a8af89 chore(cleanup): Cleanup Wave 3 - Archive reports, organize docs, fix security issues
## Summary
Third major cleanup wave after investigating 287 remaining root files.
Archived historical reports, organized documentation, removed regeneratable
artifacts, and fixed critical security issue.

## Files Cleaned (119 total)
- Archived: 78 files (7 WAVE reports + 71 summaries) → docs/archive/
- Archived: 7 build logs → docs/archive/build_logs/
- Organized: 10 markdown files → docs/guides/ + docs/checklists/
- Deleted: 17 test/coverage artifacts (regeneratable)
- Deleted: 7 empty/obsolete files (docker override, clippy baselines)
- Deleted: 3 large files (119MB - .venv, ppo_hyperopt_output.txt, backup)

## Space Recovered
- Total: ~120.7 MB
- Large files: 119.25 MB (.venv, ppo_hyperopt_output.txt)
- Archives: 1.04 MB (summaries + build logs)
- Test artifacts: 980 KB

## Security Fix (CRITICAL)
- Fixed: certs/security.env removed from git tracking (contained JWT secrets)
- Updated: .gitignore to prevent future tracking of sensitive cert files
- Removed: 4 files from git history (security.env, production.env.template, *.serial)

## Documentation Organization
- Created: docs/archive/ (wave_reports/, summaries/, build_logs/)
- Created: docs/guides/ (7 detailed implementation guides)
- Created: docs/checklists/ (3 operational checklists)
- Retained: 30 essential .md files in root (quick refs, CLAUDE.md)

## Investigation Reports Created
- MARKDOWN_ORGANIZATION_REPORT.md
- TXT_FILES_INVENTORY_AND_ARCHIVAL_PLAN.md
- ROOT_CONFIG_FILES_ANALYSIS_REPORT.md
- DOCKER_ROOT_FILES_ANALYSIS.md
- DATABASE_INITIALIZATION_AND_SETUP_ANALYSIS.md
- (6 additional investigation/index files)

## Cleanup Wave Progress
- Wave 1: 899 files deleted (1,071,884 lines)
- Wave 2: 543 files archived/deleted (~34GB)
- Wave 3: 119 files archived/deleted/organized (~121MB)
- Total: 1,561 files cleaned, ~35.1GB space recovered

## Result
Root directory: 287 files → ~180 files (excluding investigation reports)
Clean, organized, production-ready structure maintained.

Related: Second cleanup wave (previous commit)
2025-10-30 01:46:39 +01:00

318 lines
7.3 KiB
Markdown

# Foxhunt Hyperopt Docker Build Guide
## Overview
Production-ready multi-stage Dockerfile using `cargo-chef` for optimal layer caching and CUDA 12.4.1 support.
**Output**: 4 hyperparameter optimization binaries
- `hyperopt_mamba2_demo`
- `hyperopt_dqn_demo`
- `hyperopt_ppo_demo`
- `hyperopt_tft_demo`
**Key Features**:
- ✅ GLIBC 2.35 compatibility (Ubuntu 22.04)
- ✅ CUDA 12.4.1 + cuDNN runtime
- ✅ Cargo-chef dependency caching (fast rebuilds)
- ✅ BuildKit cache mounts (optimized registry access)
- ✅ Minimal runtime image (~2-3GB vs. ~11GB devel)
- ✅ Non-root user for security
- ✅ Metadata labels (git commit, build date, versions)
---
## Quick Start
### 1. Build Image
```bash
# Simple build (uses defaults)
./scripts/build_hyperopt_docker.sh
# Custom image name/tag
IMAGE_NAME=myrepo/foxhunt IMAGE_TAG=v1.0.0 ./scripts/build_hyperopt_docker.sh
```
### 2. Test Locally
```bash
# Test DQN binary
docker run --rm --gpus all jgrusewski/foxhunt-hyperopt:latest \
hyperopt_dqn_demo --help
# Test with mounted data
docker run --rm --gpus all \
-v $(pwd)/test_data:/workspace/data \
jgrusewski/foxhunt-hyperopt:latest \
hyperopt_dqn_demo --data-path /workspace/data
```
### 3. Push to Registry
```bash
# Login to Docker Hub
docker login
# Push both tags
docker push jgrusewski/foxhunt-hyperopt:latest
docker push jgrusewski/foxhunt-hyperopt:<git-commit>
```
---
## Architecture
### 5-Stage Build Process
```
Stage 1: Chef → Install cargo-chef
Stage 2: Planner → Generate recipe.json (dependency manifest)
Stage 3: Builder-Deps → Build dependencies (cached layer)
Stage 4: Builder → Build 4 hyperopt binaries
Stage 5: Runtime → Minimal CUDA runtime image
```
**Layer Caching**:
- Dependencies cached in Stage 3 (rebuilds only if `Cargo.toml` changes)
- BuildKit cache mounts for cargo registry (no re-downloads)
- Final image only contains binaries + CUDA runtime libs
**Size Comparison**:
| Image | Size | Use Case |
|---|---|---|
| nvidia/cuda:12.4.1-cudnn-devel | ~8GB | Build stage only |
| nvidia/cuda:12.4.1-cudnn-runtime | ~2.5GB | Final runtime stage |
| Foxhunt final image | ~2.8GB | Production deployment |
---
## Build Configuration
### Environment Variables
| Variable | Default | Description |
|---|---|---|
| `IMAGE_NAME` | `jgrusewski/foxhunt-hyperopt` | Docker image name |
| `IMAGE_TAG` | `latest` | Docker image tag |
| `DOCKER_BUILDKIT` | `1` | Enable BuildKit (auto-set) |
| `SQLX_OFFLINE` | `true` | Offline mode (no DB connection) |
### Build Arguments
| Argument | Default | Description |
|---|---|---|
| `GIT_COMMIT` | Auto-detected | Git commit hash |
| `BUILD_DATE` | Auto-generated | ISO 8601 build timestamp |
| `CUDA_VERSION` | `12.4.1` | CUDA toolkit version |
| `CUDNN_VERSION` | `9` | cuDNN version |
---
## Advanced Usage
### Manual Build (without script)
```bash
export DOCKER_BUILDKIT=1
GIT_COMMIT=$(git rev-parse --short HEAD)
BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
docker build \
-f Dockerfile.foxhunt-build \
-t jgrusewski/foxhunt-hyperopt:latest \
--build-arg GIT_COMMIT="${GIT_COMMIT}" \
--build-arg BUILD_DATE="${BUILD_DATE}" \
--progress=plain \
.
```
### Inspect Image
```bash
# List binaries
docker run --rm jgrusewski/foxhunt-hyperopt:latest \
ls -lh /usr/local/bin/hyperopt_*
# Check GLIBC version
docker run --rm jgrusewski/foxhunt-hyperopt:latest \
ldd --version
# View metadata
docker inspect jgrusewski/foxhunt-hyperopt:latest | jq '.[0].Config.Labels'
# Check CUDA availability
docker run --rm --gpus all jgrusewski/foxhunt-hyperopt:latest \
sh -c 'nvidia-smi && echo "CUDA_HOME=${CUDA_HOME}"'
```
### Multi-Architecture Build (Optional)
```bash
# Create buildx builder (one-time setup)
docker buildx create --name foxhunt-builder --use
# Build for multiple architectures
docker buildx build \
-f Dockerfile.foxhunt-build \
-t jgrusewski/foxhunt-hyperopt:latest \
--platform linux/amd64,linux/arm64 \
--push \
.
```
---
## Runpod Deployment
### 1. Push to Docker Hub
```bash
# Build and push
./scripts/build_hyperopt_docker.sh
docker push jgrusewski/foxhunt-hyperopt:latest
```
### 2. Create Runpod Pod
```bash
python3 scripts/runpod_deploy.py \
--gpu-type "RTX A4000" \
--docker-image "jgrusewski/foxhunt-hyperopt:latest" \
--volume-mount "/runpod-volume"
```
### 3. Execute Training
```bash
# SSH into pod
runpodctl ssh <pod-id>
# Mount data from network volume
export DATA_PATH=/runpod-volume/test_data
# Run hyperopt (auto-uploads to S3)
hyperopt_dqn_demo \
--data-path ${DATA_PATH} \
--epochs 100 \
--s3-bucket se3zdnb5o4 \
--s3-prefix models/dqn
```
---
## Troubleshooting
### Build Fails: SQLX Error
**Symptom**: `error: could not find .env file`
**Solution**: Ensure `SQLX_OFFLINE=true` is set in Dockerfile (already included)
### Build Fails: CUDA Not Found
**Symptom**: `nvcc: command not found`
**Solution**: Verify base image is `nvidia/cuda:12.4.1-cudnn-devel-ubuntu22.04` in Stage 3
### Runtime Error: GLIBC Version Mismatch
**Symptom**: `version 'GLIBC_2.35' not found`
**Solution**: Ensure base image is Ubuntu 22.04 (GLIBC 2.35). Runpod driver 550 supports GLIBC 2.35.
### Binary Not Found
**Symptom**: `hyperopt_dqn_demo: command not found`
**Solution**: Verify binary copied to `/usr/local/bin/` in final stage. Check with `docker run --rm <image> ls /usr/local/bin/`
### CUDA Error at Runtime
**Symptom**: `CUDA error: no kernel image available`
**Solution**: Ensure running with `--gpus all` flag and CUDA driver version is compatible with CUDA 12.4.1 (driver >= 550)
---
## Performance Optimization
### Cache Hits
After first build, subsequent builds should show:
```
[builder-deps] => CACHED cargo chef cook ...
[builder-deps] => CACHED cargo build ...
```
If not cached, check:
1. `recipe.json` hasn't changed (dependencies stable)
2. BuildKit enabled (`export DOCKER_BUILDKIT=1`)
3. No `--no-cache` flag used
### Build Time Expectations
| Stage | First Build | Cached Build |
|---|---|---|
| Stage 1-2 (Chef/Planner) | ~30s | ~5s |
| Stage 3 (Dependencies) | ~15 min | ~10s (cached) |
| Stage 4 (Binaries) | ~8 min | ~8 min |
| Stage 5 (Runtime) | ~1 min | ~30s |
| **Total** | **~25 min** | **~9 min** |
**Optimization**: Stage 3 cache is critical. Avoid changing `Cargo.toml` unless necessary.
---
## Files Created
| File | Description |
|---|---|
| `Dockerfile.foxhunt-build` | Multi-stage build definition |
| `.dockerignore` | Build context exclusions |
| `scripts/build_hyperopt_docker.sh` | Automated build script |
| `DOCKER_BUILD_GUIDE.md` | This guide |
---
## Next Steps
1. **Local Validation**:
```bash
# Build image
./scripts/build_hyperopt_docker.sh
# Test DQN binary
docker run --rm --gpus all jgrusewski/foxhunt-hyperopt:latest \
hyperopt_dqn_demo --help
```
2. **Push to Registry**:
```bash
docker push jgrusewski/foxhunt-hyperopt:latest
```
3. **Runpod Deployment**:
```bash
python3 scripts/runpod_deploy.py --gpu-type "RTX A4000"
```
4. **Monitor Training**:
```bash
# Check S3 for checkpoints
aws s3 ls s3://se3zdnb5o4/models/ --profile runpod --recursive
```
---
## References
- **cargo-chef**: https://github.com/LukeMathWalker/cargo-chef
- **Docker BuildKit**: https://docs.docker.com/build/buildkit/
- **NVIDIA CUDA Images**: https://hub.docker.com/r/nvidia/cuda
- **Runpod Docs**: https://docs.runpod.io/
---
**Last Updated**: 2025-10-29
**Status**: ✅ PRODUCTION READY