Overview
The deployment architecture consists of two containerized services:- API Service: FastAPI backend for model inference
- Client Service: Web interface for interactive predictions
docker-compose for simplified management.
Prerequisites
Install Docker
Install Docker Compose
Verify docker-compose is available:Docker Compose is included with Docker Desktop on macOS and Windows.
Train a Model
Ensure you have trained model files:
model/model_weights_001.pthconfig/models-configs/model_config_001.yamlprocessing/preprocessor.joblib
Docker Architecture
Service Configuration
Thedocker-compose.yml defines the complete service stack:
docker-compose.yml:1-41
API Service Container
Dockerfile Configuration
The API container is built using a multi-stage, security-hardened Dockerfile:Dockerfile.api:1-43
Security Features
Non-root User
Container runs as
appuser (UID 1000) instead of root, limiting potential security risks.Minimal Base Image
Uses
python:3.11-slim to reduce attack surface and image size.No Cache Files
--no-cache-dir flag prevents storing unnecessary pip cache, reducing image size.Unbuffered Output
PYTHONUNBUFFERED=1 ensures logs are immediately visible for monitoring.Deployment Methods
Quick Start: Full Stack Deployment
Deploy both services with a single command:What happens during deployment?
What happens during deployment?
-
Build Phase:
- Builds API container from
Dockerfile.api - Builds Client container from
Dockerfile.client - Installs all dependencies
- Copies application code
- Builds API container from
-
Network Setup:
- Creates
credit-score-networkbridge network - Enables inter-container communication
- Creates
-
Service Launch:
- Starts API service on port 8000
- Starts Client service on port 3000
- Mounts volumes for hot-reloading
-
Ready State:
- API available at
http://localhost:8000 - Client available at
http://localhost:3000 - API docs at
http://localhost:8000/docs
- API available at
Development Mode (Hot Reload)
For development with automatic code reloading:Volume mounts in
docker-compose.yml enable hot-reloading. Changes to local files are immediately reflected in containers.Production Mode (Detached)
Run services in the background:Service-Specific Deployment
Deploy Only API Service
- Base URL:
http://localhost:8000 - Documentation:
http://localhost:8000/docs - Health Check:
http://localhost:8000(redirects to docs)
server/api.py:50-55
Deploy Only Client Service
Volume Mounts for Development
The docker-compose configuration mounts local directories for hot-reloading:docker-compose.yml:9-15
Benefits:
- Instant code updates without rebuilding
- Local file editing with IDE
- Simplified debugging
- Faster development iteration
Networking
Services communicate through a dedicated bridge network:docker-compose.yml:39-41
Inter-Service Communication
Containers can reference each other by service name:External Access
Port mappings expose services to the host machine:- API:
localhost:8000→api-service:8000 - Client:
localhost:3000→client-service:3000
Environment Configuration
The client service uses environment variables for configuration:docker-compose.yml:31-33
Customize for different environments:
- Development
- Staging
- Production
Production Deployment Best Practices
Scaling Deployments
Horizontal Scaling
Run multiple API instances behind a load balancer:Example with NGINX Load Balancer
Monitoring and Observability
View Service Logs
Container Metrics
Monitor resource usage:- CPU usage percentage
- Memory usage and limit
- Network I/O
- Block I/O
Access Running Container
For debugging, access a running container:Troubleshooting
Port already in use
Port already in use
Error:
Bind for 0.0.0.0:8000 failed: port is already allocatedSolution:Container exits immediately
Container exits immediately
Debugging steps:Common causes:
- Missing dependencies
- File permission issues
- Configuration errors
Cannot connect to API from client
Cannot connect to API from client
Solution:
-
Verify both services are running:
-
Check network connectivity:
-
Verify API_URL environment variable:
Volume mount permissions error
Volume mount permissions error
Error:
Permission denied when accessing mounted filesSolution:Out of memory errors
Out of memory errors
Solution:Increase Docker memory allocation:
- Docker Desktop: Settings → Resources → Memory
- Linux: Modify
/etc/docker/daemon.json
Cleanup
Stop and Remove Containers
Remove Unused Docker Resources
Advanced Deployment Options
Docker Swarm
For orchestrated, multi-node deployments:Kubernetes
For production-grade orchestration, convert to Kubernetes manifests using Kompose:Cloud Deployments
AWS ECS
Deploy using Amazon Elastic Container Service with Fargate for serverless containers.
Google Cloud Run
Deploy directly from container images with automatic scaling.
Azure Container Instances
Simple container hosting on Microsoft Azure.
Next Steps
Running Inference
Test your deployed API with predictions
MLflow Tracking
Monitor deployed model performance
API Reference
Explore complete API documentation
Model Configuration
Customize models for production
