Summary
Implement a non-root user for the backend Python container to follow Docker security best practices and comply with industry standards for container security. Currently, both Dockerfile.prod and the production deployment run containers as root user, which poses security risks.
Progress Update (2025-10-14)
✅ Implementation Complete - Ready for Testing
All code changes have been implemented and documented. The non-root user implementation is complete and ready for testing.
Completed Tasks:
- ✅ Updated
backend/Dockerfile.prod with multi-stage build and non-root user
- ✅ Updated
docker-compose.yml volume mappings for development
- ✅ Updated
docker-compose.prod.yml volume mappings for production
- ✅ Created
scripts/fix-model-permissions.sh migration script
- ✅ Updated
CLAUDE.md with security documentation
- ✅ Updated
scripts/README.md with migration guide
- ✅ All changes pushed to
fix/backend-non-root branch
Next Steps:
- ⏳ Test in development environment
- ⏳ Verify GPU access and model caching
- ⏳ Test migration script on existing installation
- ⏳ Build and push new Docker images to DockerHub
Background
Current State:
backend/Dockerfile.prod runs as root user (no USER directive)
backend/Dockerfile.prod.optimized already has non-root implementation (user appuser with UID 1000)
- Volume mappings in docker-compose files use
/root/.cache/* paths
- Model cache directories are mounted to root-owned paths
Security Concerns:
- Running as root violates principle of least privilege
- Container escape vulnerabilities could lead to host root compromise
- File permission issues when volumes are accessed from host
- Non-compliance with security scanning tools (Trivy, Snyk, etc.)
Objectives
Implement non-root user configuration that:
- ✅ Follows Python official Docker image best practices
- ✅ Maintains compatibility with GPU access (NVIDIA runtime)
- ✅ Preserves model caching functionality (HuggingFace, PyTorch)
- ✅ Ensures proper permissions for temp directories and file uploads
- ✅ Works with both development and production environments
- ✅ Compatible with Celery worker processes
- ✅ No breaking changes to existing deployments
Implementation Summary
1. Updated Dockerfile.prod ✅
Converted to multi-stage build with non-root user:
- Added
builder stage for package installation
- Created
appuser (UID 1000, GID 1000) in video group
- Updated cache directories to
/home/appuser/.cache/*
- Set environment variables for HuggingFace and PyTorch
- Added health check for container orchestration
- All files copied with proper ownership
2. Updated Docker Compose Files ✅
Modified Files:
docker-compose.yml (development)
docker-compose.prod.yml (production)
Services Updated:
backend
celery-worker
flower
Volume Mappings Changed:
# Old (root user)
- ${MODEL_CACHE_DIR}/huggingface:/root/.cache/huggingface
- ${MODEL_CACHE_DIR}/torch:/root/.cache/torch
# New (non-root user)
- ${MODEL_CACHE_DIR}/huggingface:/home/appuser/.cache/huggingface
- ${MODEL_CACHE_DIR}/torch:/home/appuser/.cache/torch
3. Created Migration Script ✅
File: scripts/fix-model-permissions.sh
Automated permission fixer for existing deployments:
- Reads
MODEL_CACHE_DIR from .env file
- Fixes ownership to UID:GID 1000:1000
- Supports Docker and sudo methods
- Sets correct permissions (755 for dirs, 644 for files)
Usage:
./scripts/fix-model-permissions.sh
4. Updated Documentation ✅
CLAUDE.md:
- Added "Security Features" section
- Documented non-root container user
- Included migration instructions
- Updated volume mapping examples
scripts/README.md:
- Added "Model Cache Permission Fixer" section
- Documented script usage and verification
- Linked to security documentation
Testing Checklist
Development Environment Tests
Production Environment Tests
Security Verification
GPU and AI Model Tests
Migration Path for Existing Deployments
For Users with Existing Deployments:
-
Run the migration script:
./scripts/fix-model-permissions.sh
-
Pull latest changes:
-
Rebuild and restart containers:
docker compose down
docker compose build
docker compose up -d
-
Verify migration:
# Check container user
docker compose exec backend whoami
# Should output: appuser (not root)
# Verify model cache accessibility
docker compose exec backend ls -la /home/appuser/.cache/huggingface
Documentation Updates
Acceptance Criteria
References
Industry Standards
Best Practices
- Use numeric UID for better compatibility across systems
- Avoid UID 0 (root) and UIDs below 1000 (system users)
- Use named volumes for better permission management
- Add user to necessary groups (video for GPU access)
- Set proper file permissions (755 for executables, 644 for files)
Related Issues
Priority
High - Security best practice, required for production deployments
Labels
security, docker, backend, enhancement, production
Summary
Implement a non-root user for the backend Python container to follow Docker security best practices and comply with industry standards for container security. Currently, both
Dockerfile.prodand the production deployment run containers as root user, which poses security risks.Progress Update (2025-10-14)
✅ Implementation Complete - Ready for Testing
All code changes have been implemented and documented. The non-root user implementation is complete and ready for testing.
Completed Tasks:
backend/Dockerfile.prodwith multi-stage build and non-root userdocker-compose.ymlvolume mappings for developmentdocker-compose.prod.ymlvolume mappings for productionscripts/fix-model-permissions.shmigration scriptCLAUDE.mdwith security documentationscripts/README.mdwith migration guidefix/backend-non-rootbranchNext Steps:
Background
Current State:
backend/Dockerfile.prodruns as root user (no USER directive)backend/Dockerfile.prod.optimizedalready has non-root implementation (userappuserwith UID 1000)/root/.cache/*pathsSecurity Concerns:
Objectives
Implement non-root user configuration that:
Implementation Summary
1. Updated Dockerfile.prod ✅
Converted to multi-stage build with non-root user:
builderstage for package installationappuser(UID 1000, GID 1000) invideogroup/home/appuser/.cache/*2. Updated Docker Compose Files ✅
Modified Files:
docker-compose.yml(development)docker-compose.prod.yml(production)Services Updated:
backendcelery-workerflowerVolume Mappings Changed:
3. Created Migration Script ✅
File:
scripts/fix-model-permissions.shAutomated permission fixer for existing deployments:
MODEL_CACHE_DIRfrom.envfileUsage:
4. Updated Documentation ✅
CLAUDE.md:
scripts/README.md:
Testing Checklist
Development Environment Tests
./opentr.sh start devstarts without errorsdocker exec backend whoami)Production Environment Tests
Security Verification
docker exec -it <container> whoami)GPU and AI Model Tests
Migration Path for Existing Deployments
For Users with Existing Deployments:
Run the migration script:
Pull latest changes:
Rebuild and restart containers:
Verify migration:
Documentation Updates
CLAUDE.mdwith new volume paths ✅scripts/README.mdwith migration script documentation ✅README.mdwith security best practices sectionsetup-opentranscribe.shto set correct permissionsAcceptance Criteria
Dockerfile.prod.optimizedalready implements non-root pattern (use as reference) ✅Dockerfile.produpdated to match optimized version ✅/home/appuser/.cache/*paths ✅References
Industry Standards
Best Practices
Related Issues
Priority
High - Security best practice, required for production deployments
Labels
security,docker,backend,enhancement,production