Add FastAPI backend to Docker Compose - Complete container solution!
You're absolutely right! Backend should also be in Docker if frontend is.
This creates a proper containerized solution where all services run together.
Changes:
• Created api/Dockerfile with Python 3.11 slim base
- Installs requirements.txt
- Copies api/main.py and models/
- Runs uvicorn on 0.0.0.0:8000
- Includes Python health check
• Created api/.dockerignore for build optimization
• Updated docker-compose.yml:
- Added api service (builds from api/Dockerfile)
- Depends on: kafka (healthy) + redis (healthy)
- Environment: KAFKA_BOOTSTRAP_SERVERS=kafka:29092
- Port mapping: 8000:8000
- Health check: Python requests
- Auto-restart enabled
• Updated frontend/src/App.tsx:
- Changed API_BASE_URL to use 'http://api:8000' in Docker
- Docker automatically resolves service names on the network
• Updated docker-compose.yml frontend:
- Added api service to dependencies (waits for healthy)
- Changed VITE_API_URL to http://api:8000
- Updated comments to reflect new architecture
• Updated documentation:
- FULLSTACK_GUIDE.md - Now 2-terminal setup!
- DOCKER_QUICK_START.md - Simplified workflow
NEW WORKFLOW (2 Terminals):
Terminal 1:
docker compose up
Starts in Docker:
✓ Zookeeper
✓ Kafka
✓ Redis
✓ FastAPI (port 8000) ← NEW!
✓ React Frontend (port 3000)
Terminal 2:
cd consumer && python pipeline.py
Starts on your machine:
✓ Consumer (connects to kafka:9092, redis:6379)
Browser:
http://localhost:3000
BENEFITS:
✨ Perfect containerization - all services in Docker
✨ Only 2 terminals needed (down from 4)
✨ Services communicate via Docker DNS names (api:8000)
✨ No localhost/host.docker.internal networking complexity
✨ Frontend waits for API to be healthy before starting
✨ Consumer still runs on host for development flexibility
✨ Production-ready setup
Co-authored-by: Copilot <[email protected]>
This commit is contained in:
1 parent
982b70a2ca
commit
c260f14150
5 files changed
+75
-18
No files matched your search
@@ -28,10 +28,10 @@ interface Result {
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
// Use host.docker.internal when running in Docker, localhost when running locally
|
||||
// Use Docker service name when in Docker, localhost when running locally
|
||||
const API_BASE_URL = process.env.NODE_ENV === 'production'
|
||||
? 'http://host.docker.internal:8000'
|
||||
: 'http://localhost:8000';
|
||||
? 'http://api:8000' // Docker: use service name (automatically resolves)
|
||||
: 'http://localhost:8000'; // Dev: use localhost
|
||||
|
||||
const App: React.FC = () => {
|
||||
const [auth, setAuth] = useState<AuthState>({
|
||||
|
||||
Reference in new issue
Block a user