Phase 2 · Chapter 2.03
Docker Compose
ML service একা চলে না — দরকার cache, DB, monitoring। Compose এই multi-container stack এক file-এ define করে।
Hook
One command, full stack
docker compose up — এই এক command-এ API, Redis, Postgres, Prometheus সব একসাথে চালু। Dev environment setup-এর সবচেয়ে সহজ পথ।
Concept
Compose-এর মূল ধারণা
- Service — একটি container definition।
- Network — service-গুলো নিজেদের মধ্যে নাম দিয়ে কথা বলে।
- Volume — persistent data storage।
- Depends_on — startup order।
- Profiles — optional services group।
Code
Production-style compose file
yamlproduction
# compose.yaml
services:
api:
build: .
image: iris-api:1.0
ports:
- "8000:8000"
environment:
REDIS_URL: redis://cache:6379/0
DATABASE_URL: postgresql://mlops:mlops@db:5432/mlops
depends_on:
cache:
condition: service_healthy
db:
condition: service_healthy
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"]
interval: 30s
timeout: 5s
retries: 3
restart: unless-stopped
cache:
image: redis:7-alpine
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
retries: 5
volumes:
- redis_data:/data
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: mlops
POSTGRES_PASSWORD: mlops
POSTGRES_DB: mlops
healthcheck:
test: ["CMD-SHELL", "pg_isready -U mlops"]
interval: 10s
retries: 5
volumes:
- pg_data:/var/lib/postgresql/data
prometheus:
image: prom/prometheus:latest
profiles: ["monitoring"]
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
volumes:
redis_data:
pg_data:ব্যাখ্যা: Service name-ই DNS — API container-এ redis://cache:6379লিখলেই connect হবে। Volume দিয়ে DB ও Redis data persist থাকবে। Prometheus optional profiles-এ।
Commands
Daily commands
bashproduction
# Start full stack
docker compose up -d
# With optional profile
docker compose --profile monitoring up -d
# Logs
docker compose logs -f api
# Restart single service
docker compose restart api
# Scale (Compose v2)
docker compose up -d --scale api=3
# Stop & remove
docker compose down
# Stop, remove + delete volumes
docker compose down -vIntuition
Compose vs Kubernetes
Compose
Single-host, dev/staging, small prod। File simple, learning curve কম।
Kubernetes
Multi-host cluster, auto-scaling, self-healing — Phase 5-এ আসছে।
Real-world
কখন compose যথেষ্ট
- Local dev environment — সব team-এ uniform।
- CI pipeline-এ integration test।
- Small SaaS বা internal tool — single VM-এ stack।
- Prototype/MVP — fast iteration।
Common Mistakes
যেসব ভুল বেশি হয়
depends_oncondition না দেওয়া — DB ready হওয়ার আগেই API চালু হয়ে crash।- Volume mount না করে DB চালানো — restart-এ data হারায়।
- Secret plain compose file-এ —
.envবা secret manager ব্যবহার করুন। - Production-এ compose দিয়ে scale করার চেষ্টা — orchestrator দরকার।
Practice Tasks
অনুশীলন
- উপরের stack চালান এবং
docker compose ps-এ health দেখুন। - API container থেকে
redis-cliদিয়ে cache ping করুন। - Compose file-এ Prometheus profile activate করে metrics দেখুন।
down -vদিয়ে volume মুছে আবার চালু করে data behavior বুঝুন।
Mini Project
Mini Project — Iris Stack
একটি compose file বানান যেখানে: iris-api + Redis (prediction cache) + Postgres (prediction log) + Prometheus (optional)। API-তে cache lookup ও DB insert logic যোগ করুন। README-এ architecture diagram দিন।
Summary
Phase 2 শেষ
- Docker basics → ML model containerize → multi-service compose।
- আপনার ML service এখন portable, reproducible, multi-component।
- পরবর্তী Phase: CI/CD — automated build, test, deploy pipeline।
← Roadmap-এ ফিরুন
পরবর্তী: Phase 3 — CI/CD Conceptsশীঘ্রই