โ† Back|CLOUD-DEVOPSโ€บSection 1/15
0 of 15 completed

Docker for AI apps

Intermediateโฑ 14 min read๐Ÿ“… Updated: 2026-02-17

Introduction

"My machine la work aagudhu, unga machine la yenda work aagala?" โ€” Developer life oda biggest problem! ๐Ÿ˜ค


AI apps ku idhu worse โ€” TensorFlow version mismatch, CUDA driver issues, Python package conflicts. Oru developer machine la train aagura model vera developer machine la load aagaadhu.


Docker indha problem completely solve pannum! Unga app, dependencies, model โ€” ellam oru container la pack panniduveenga. Enga run pannaalum same result! ๐Ÿณ


Indha article la Docker basics, AI-specific Dockerfiles, multi-stage builds, GPU containers โ€” ellam paapom! ๐Ÿ“ฆ

Docker Core Concepts

Docker la 4 key concepts:


1. Image ๐Ÿ“ธ

  • Container oda blueprint/template
  • Read-only
  • Dockerfile la irundhu build aagum
  • Example: python:3.11-slim, nvidia/cuda:12.0

2. Container ๐Ÿ“ฆ

  • Image oda running instance
  • Isolated environment
  • Start, stop, delete pannalam
  • Multiple containers from same image

3. Dockerfile ๐Ÿ“

  • Image build panna instructions
  • Step-by-step recipe
  • FROM, RUN, COPY, CMD commands

4. Registry ๐Ÿช

  • Images store pannra place
  • Docker Hub (public, free)
  • ECR (AWS), GCR (Google), ACR (Azure)

Analogy ๐Ÿฐ:

  • Dockerfile = Recipe
  • Image = Frozen cake (ready to use)
  • Container = Cake cut panni serve pannradhu
  • Registry = Bakery shop (cakes store)

Docker Architecture

๐Ÿ—๏ธ Architecture Diagram
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              DOCKER ARCHITECTURE                  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                   โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚            Docker Host (Your Machine)     โ”‚    โ”‚
โ”‚  โ”‚                                           โ”‚    โ”‚
โ”‚  โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚    โ”‚
โ”‚  โ”‚  โ”‚Containerโ”‚ โ”‚Containerโ”‚ โ”‚Containerโ”‚   โ”‚    โ”‚
โ”‚  โ”‚  โ”‚ AI App  โ”‚ โ”‚ Redis   โ”‚ โ”‚Postgres โ”‚   โ”‚    โ”‚
โ”‚  โ”‚  โ”‚ + Model โ”‚ โ”‚ Cache   โ”‚ โ”‚   DB    โ”‚   โ”‚    โ”‚
โ”‚  โ”‚  โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜   โ”‚    โ”‚
โ”‚  โ”‚       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜         โ”‚    โ”‚
โ”‚  โ”‚            โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”              โ”‚    โ”‚
โ”‚  โ”‚            โ”‚Docker Engineโ”‚              โ”‚    โ”‚
โ”‚  โ”‚            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜              โ”‚    โ”‚
โ”‚  โ”‚            โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”              โ”‚    โ”‚
โ”‚  โ”‚            โ”‚  Host OS    โ”‚              โ”‚    โ”‚
โ”‚  โ”‚            โ”‚  (Linux)    โ”‚              โ”‚    โ”‚
โ”‚  โ”‚            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜              โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                   โ”‚
โ”‚  vs Virtual Machine:                             โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”               โ”‚
โ”‚  โ”‚ App    โ”‚ โ”‚ App    โ”‚ โ”‚ App    โ”‚               โ”‚
โ”‚  โ”‚ Guest  โ”‚ โ”‚ Guest  โ”‚ โ”‚ Guest  โ”‚ โ† Full OS    โ”‚
โ”‚  โ”‚  OS    โ”‚ โ”‚  OS    โ”‚ โ”‚  OS    โ”‚   each!      โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜               โ”‚
โ”‚      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                     โ”‚
โ”‚           Hypervisor (VMware/KVM)                โ”‚
โ”‚                                                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

AI App Dockerfile โ€” Step by Step

โœ… Example

Complete Dockerfile for AI inference app:

dockerfile
# Base image โ€” slim version (smaller)
FROM python:3.11-slim

# Working directory set
WORKDIR /app

# System dependencies (for ML libraries)
RUN apt-get update && apt-get install -y \
    gcc g++ && \
    rm -rf /var/lib/apt/lists/*

# Python dependencies first (Docker caching!)
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# App code copy
COPY . .

# Model download (or COPY from local)
RUN python download_model.py

# Port expose
EXPOSE 8080

# Health check
HEALTHCHECK CMD curl -f http://localhost:8080/health

# Run the app
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]

Key tip: requirements.txt COPY first, then code. Dependencies change aagama iruntha cached layer use aagum โ€” build fast! โšก

Multi-Stage Builds (Smaller Images)

AI Docker images romba perusa aagum โ€” 5GB+! Multi-stage builds use pannina size 50-70% reduce aagum:


dockerfile
# Stage 1: Build (install everything)
FROM python:3.11 AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user -r requirements.txt

# Stage 2: Runtime (only needed files)
FROM python:3.11-slim
WORKDIR /app
COPY --from=builder /root/.local /root/.local
COPY . .
ENV PATH=/root/.local/bin:$PATH
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]

Size comparison:


ApproachImage Size
Single stage (python:3.11)3.5 GB
Single stage (slim)2.1 GB
Multi-stage (slim)1.2 GB
Multi-stage + .dockerignore800 MB

Always use multi-stage for production AI apps! Storage cost um reduce aagum, deploy um fast aagum. ๐Ÿ“ฆ

GPU Support in Docker

AI model inference ku GPU venum na Docker la um GPU use pannalam!


Prerequisites:

  • NVIDIA GPU installed
  • NVIDIA Container Toolkit installed
  • Docker 19.03+

GPU Dockerfile:

dockerfile
FROM nvidia/cuda:12.0-runtime-ubuntu22.04

RUN apt-get update && apt-get install -y python3 python3-pip
COPY requirements.txt .
RUN pip3 install -r requirements.txt
COPY . .
CMD ["python3", "app.py"]

Run with GPU:

bash
docker run --gpus all -p 8080:8080 my-ai-app
# Specific GPU:
docker run --gpus '"device=0"' my-ai-app

Verify GPU inside container:

bash
docker run --gpus all nvidia/cuda:12.0-base nvidia-smi

Common CUDA images:


Base ImageSizeUse
nvidia/cuda:12.0-base200MBRuntime only
nvidia/cuda:12.0-runtime1.5GB+ CUDA runtime
nvidia/cuda:12.0-devel3.5GB+ compilation tools

Docker Compose for AI Stack

Multiple containers manage panna Docker Compose use pannunga:


yaml
# docker-compose.yml
version: '3.8'
services:
  ai-app:
    build: .
    ports:
      - "8080:8080"
    environment:
      - MODEL_PATH=/models/bert
      - REDIS_URL=redis://cache:6379
    volumes:
      - ./models:/models
    depends_on:
      - cache
      - db

  cache:
    image: redis:7-alpine
    ports:
      - "6379:6379"

  db:
    image: postgres:15-alpine
    environment:
      - POSTGRES_DB=aiapp
      - POSTGRES_PASSWORD=secret
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

One command la entire stack up:

bash
docker compose up -d    # Start all services
docker compose logs -f  # View logs
docker compose down     # Stop all

AI app + Redis cache + PostgreSQL โ€” full stack 30 seconds la up! ๐Ÿš€

Docker Optimization for AI

AI Docker images optimize panna tips:


1. .dockerignore file ๐Ÿ“‹

code
__pycache__
*.pyc
.git
.env
data/raw/
notebooks/
*.ipynb

2. Layer caching strategy ๐Ÿ“ฆ

  • Rarely changing layers first (OS, system deps)
  • Dependencies next (requirements.txt)
  • Code last (changes most often)

3. Slim base images ๐Ÿ‹๏ธ

  • python:3.11 โ†’ 1.0GB
  • python:3.11-slim โ†’ 150MB
  • python:3.11-alpine โ†’ 60MB (compatibility issues possible)

4. Model optimization ๐Ÿง 

  • ONNX format use pannunga (smaller, faster)
  • Quantized models use pannunga (INT8)
  • Model weights separate volume la mount pannunga

5. Multi-stage builds ๐Ÿ—๏ธ

  • Build dependencies final image la include aagaadhu
  • Compiler, dev tools โ€” build stage la mattum

Result: 5GB image โ†’ 800MB image. Deploy 6x faster! โšก

Common Docker Issues (AI Apps)

โš ๏ธ Warning

AI apps la common Docker issues & solutions:

โš ๏ธ Issue: Image too large (5GB+)

โ†’ Multi-stage build, slim base, .dockerignore

โš ๏ธ Issue: Build takes 30+ minutes

โ†’ Layer caching optimize, requirements.txt separate COPY

โš ๏ธ Issue: Out of memory (OOM killed)

โ†’ Docker memory limit increase: docker run -m 4g

โš ๏ธ Issue: GPU not detected

โ†’ nvidia-container-toolkit install, --gpus all flag

โš ๏ธ Issue: Model file not found

โ†’ COPY path check, or volume mount use

โš ๏ธ Issue: Permission denied

โ†’ RUN chown or USER directive add

โš ๏ธ Issue: Slow inference in container

โ†’ CPU cores limit check, OMP_NUM_THREADS set

Essential Docker Commands

Daily use pannra Docker commands:


CommandPurpose
`docker build -t name .`Image build
`docker run -p 8080:8080 name`Container start
`docker ps`Running containers list
`docker logs container_id`View logs
`docker exec -it container bash`Shell access
`docker stop container_id`Stop container
`docker images`List images
`docker rmi image_id`Delete image
`docker system prune -a`Cleanup everything
`docker compose up -d`Start all services
`docker compose down`Stop all services
`docker stats`Resource usage monitor

Pro tip: docker system prune -a regularly run pannunga โ€” unused images, containers disk space eat pannudum! ๐Ÿงน

Prompt: Docker Setup

๐Ÿ“‹ Copy-Paste Prompt
You are a Docker expert specializing in AI applications.

I have a Python FastAPI app that:
- Loads a HuggingFace BERT model (500MB)
- Serves text classification API
- Uses Redis for caching
- Needs to run on CPU (no GPU)

Create:
1. Optimized multi-stage Dockerfile
2. docker-compose.yml (app + Redis)
3. .dockerignore file
4. Build and run commands
5. Production optimization tips

Target: Image size under 1GB, startup time under 30 seconds.

โœ… Key Takeaways

โœ… Docker Solves Reproducibility โ€” App + dependencies package. Same image enga run pannaalum consistent result. "Works on my machine" forever solve


โœ… Dockerfile Concepts โ€” Image (blueprint), Container (running instance), Layer (caching). FROM slim base, COPY dependencies first, code last (caching efficiency)


โœ… Multi-Stage Builds โ€” Build stage (install everything) + runtime stage (only needed). 5GB image โ†’ 800MB possible. AI apps mandatory for large models


โœ… Optimization Techniques โ€” .dockerignore (unnecessary files exclude), layer caching strategy, slim base images, non-root user (security), health checks


โœ… GPU in Docker โ€” nvidia/cuda base image, nvidia-container-toolkit install, --gpus all flag. Model inference GPU-accelerated containers possible


โœ… Docker Compose โ€” Multiple containers (app, Redis, database) single YAML file. Local development perfect; production Kubernetes recommended


โœ… Common Issues โ€” Image too large (multi-stage), build slow (cache), OOM killed (memory limit), GPU not detected (toolkit), model missing (volume mount)


โœ… AI App Pattern โ€” Python:slim base, requirements.txt separate COPY (caching), app code last, ONNX models volume mount, health endpoint, proper signals handling

๐Ÿ ๐ŸŽฎ Mini Challenge

Challenge: Containerize an AI App (HuggingFace Model)


Real Docker practice โ€” sentiment analysis model containerize pannu:


Step 1: Python App Create Pannunga ๐Ÿ

python
# app.py
from transformers import pipeline
from fastapi import FastAPI

app = FastAPI()
classifier = pipeline("sentiment-analysis")

@app.post("/analyze")
def analyze(text: str):
    result = classifier(text)
    return {"text": text, "sentiment": result}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

Step 2: Requirements Create Pannunga ๐Ÿ“‹

bash
# requirements.txt
torch==2.0.0
transformers==4.28.0
fastapi==0.104.0
uvicorn==0.24.0

Step 3: Dockerfile Write Pannunga ๐Ÿณ

dockerfile
FROM python:3.10-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY app.py .

EXPOSE 8000

CMD ["python", "app.py"]

Step 4: Build & Run Locally ๐Ÿš€

bash
# Build image
docker build -t sentiment-ai:latest .

# Run container
docker run -p 8000:8000 sentiment-ai:latest

# Test endpoint
curl -X POST "http://localhost:8000/analyze?text=I love machine learning"

# Stop container
docker ps  # see running container
docker stop <container-id>

Step 5: Push to Docker Hub ๐Ÿ“ฆ

bash
# Create Docker Hub account (free)
docker tag sentiment-ai:latest your-username/sentiment-ai:latest
docker push your-username/sentiment-ai:latest

Step 6: Run from Registry ๐ŸŒ

bash
# Anyone can now run:
docker run -p 8000:8000 your-username/sentiment-ai:latest

Completion Time: 90 minutes

Key Skills: Docker build, tagging, pushing, running

Real-world project โญ

๐Ÿ’ผ Interview Questions

Q1: Docker vs Virtual Machine โ€” difference? When use?

A: VM = full OS (heavy, slow). Docker = lightweight container (fast, efficient). Docker: modern applications, microservices, CI/CD. VM: legacy apps, full OS needed, security isolation critical. AI apps: Docker perfect โ€” quick deployment, reproducible.


Q2: Docker image size large issue โ€” optimization?

A: Use slim/alpine base images. Multi-stage build (build stage, runtime stage). Remove unnecessary packages. .dockerignore use (large files exclude). Model caching (separate layer). Tool: docker-slim (automatic optimization). Large models: volume mount kara, image la store panna vendaam.


Q3: Docker security concerns โ€” best practices?

A: Scan images (vulnerabilities). Don't run as root. Read-only filesystem. Secrets management (environment variables, not hardcoded). Registry authentication. Regular updates. Minimal images (less attack surface). Private registries for sensitive models.


Q4: Docker volume vs bind mount โ€” AI apps la which use?

A: Volume = Docker-managed, portable, consistent. Bind mount = host directory directly mount. AI apps: models (volume, persistent), code (bind mount, local dev). Production: volumes recommended. Development: bind mounts faster iteration.


Q5: Multi-GPU Docker setup โ€” how?

A: ```bash

# Dockerfile

FROM nvidia/cuda:12.0-runtime

# Rest setup same

code
Run: `docker run --gpus all -it image-name`
All GPUs automatically visible. CUDA runtime take care. Multi-GPU model training: torch.distributed use, GPUs automatically distributed.

Frequently Asked Questions

โ“ Docker na enna?
Docker is a containerization platform โ€” unga app and all dependencies oru "container" la pack panniduveenga. Any machine la same ah run aagum.
โ“ Docker vs Virtual Machine enna difference?
VM full OS run pannum (heavy, slow). Docker OS kernel share pannum (lightweight, fast). Docker containers seconds la start aagum, VMs minutes edukum.
โ“ AI apps ku Docker yenda special?
AI apps ku specific Python versions, CUDA drivers, ML libraries venum. Docker la ivanga ellam package pannidalam โ€” "works on my machine" problem solve!
โ“ Docker free ah?
Yes! Docker Engine completely free and open-source. Docker Desktop free for personal use and small businesses (<250 employees).
๐Ÿง Knowledge Check
Quiz 1 of 1

Docker la multi-stage build use pannra main reason enna?

0 of 1 answered