Hook — ‘আমার machine এ চলে!’
তোমার laptop এ Python 3.11 + CUDA 12, server এ Python 3.9 + CUDA 11 — model crash। Docker container এ সব dependency পুরে দাও — যেখানেই চালাও, একই behavior।
Core Concepts
- Image — blueprint (read-only)।
- Container — running instance।
- Dockerfile — image build recipe।
- Registry — image store (Docker Hub, GHCR, ECR)।
- Volume — persistent data।
- Network — container যোগাযোগ।
Dockerfile — ML API
Dockerfile
FROM python:3.11-slim
WORKDIR /app
# system dep
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential && rm -rf /var/lib/apt/lists/*
# requirements first → cache layer
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# app code
COPY app.py model.pkl ./
EXPOSE 8000
HEALTHCHECK CMD curl -f http://localhost:8000/health || exit 1
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]build & run
docker build -t iris-api:1.0 .
docker run -d -p 8000:8000 --name iris iris-api:1.0
docker logs -f irisGPU Container (PyTorch)
Dockerfile.gpu
FROM pytorch/pytorch:2.4.0-cuda12.1-cudnn9-runtime
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "serve.py"]run with GPU
docker run --gpus all -p 8000:8000 my-llm-apidocker-compose — Multi-service
docker-compose.yml
services:
api:
build: .
ports: ["8000:8000"]
environment:
- REDIS_URL=redis://cache:6379
depends_on: [cache, db]
cache:
image: redis:7-alpine
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: secret
volumes: ["pgdata:/var/lib/postgresql/data"]
volumes:
pgdata:Best Practices
- Slim base image (python:slim, distroless)।
- Multi-stage build — final image ছোট।
- .dockerignore দিয়ে অপ্রয়োজনীয় ফাইল বাদ।
- Layer caching — কম-পরিবর্তনশীল layer আগে।
- Non-root user দিয়ে run।
- Tag image semver (1.0.0, not :latest)।
- Secret env var দিয়ে, image এ নয়।
multi-stage
FROM python:3.11 AS builder
COPY requirements.txt .
RUN pip wheel --wheel-dir=/wheels -r requirements.txt
FROM python:3.11-slim
COPY --from=builder /wheels /wheels
RUN pip install --no-index --find-links=/wheels /wheels/*
COPY . /app
WORKDIR /app
CMD ["uvicorn", "app:app", "--host", "0.0.0.0"]Summary
এক নজরে
Docker = Reproducible environment। Dockerfile + compose + GPU runtime + multi-stage = production-ready ML container।