TUTORIAL-002: Docker Essentials for AI¶
Difficulty: โญ Beginner
Time: 45 minutes
Prerequisites: Basic Linux command line
Learning Objectives¶
By the end of this tutorial, you will:
- โ Understand what Docker is
- โ Build your first Docker image
- โ Run containers with GPUs
- โ Use Docker Compose for multi-container apps
What is Docker?¶
Docker = Container platform
Think of containers like lightweight virtual machines:
- Share the host OS kernel (more efficient)
- Package app + dependencies together
- Run anywhere (dev = prod)
Containers vs VMs:¶
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Virtual Machine โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ App โ โ App โ โ App โ โ
โ โโโโโโโโโโโโค โโโโโโโโโโโโค โโโโโโโโโโโโค โ
โ โ Guest โ โ Guest โ โ Guest โ โ
โ โ OS โ โ OS โ โ OS โ โ
โ โโโโโโโโโโโโค โโโโโโโโโโโโค โโโโโโโโโโโโค โ
โ โ Hypervisor โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ Host OS โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Heavy: Each VM needs full OS โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Docker Containers โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ App โ โ App โ โ App โ โ
โ โ + โ โ + โ โ + โ โ
โ โ Deps โ โ Deps โ โ Deps โ โ
โ โโโโโโโโโโโโค โโโโโโโโโโโโค โโโโโโโโโโโโค โ
โ โ Docker Engine โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ Host OS โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Light: Share kernel, only package app differences โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Step 1: Install Docker¶
Ubuntu/Debian:¶
# Update packages
sudo apt-get update
# Install Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
# Add user to docker group
sudo usermod -aG docker $USER
# Log out and back in for group changes to take effect
Verify:¶
docker --version
docker run hello-world
Step 2: Your First Container¶
Run Python in a Container:¶
docker run -it python:3.13 bash
What happened?
- Downloaded
python:3.13image - Started container with bash shell
-it= interactive + TTY
Inside the container:¶
# Check Python version
python --version
# Install uv (the curriculum's package manager standard)
curl -LsSf https://astral.sh/uv/install.sh | sh
source /root/.local/bin/env
# Install a package
uv pip install --system numpy
# Run Python
python -c "import numpy; print(numpy.__version__)"
# Exit
exit
Note: Changes inside containers are lost when they exit!
Step 3: Build Your First Image¶
Create a simple Python app:¶
# Create project directory
mkdir docker-demo
cd docker-demo
# Create app.py
cat > app.py << 'EOF'
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Prompt(BaseModel):
text: str
@app.post("/generate")
def generate(prompt: Prompt):
return {"response": f"You said: {prompt.text}"}
@app.get("/")
def root():
return {"message": "Hello AI!"}
EOF
# Create the project manifest - pyproject.toml + uv.lock ship with
# the demo and pin the dependency graph.
uv init --bare --python 3.13 .
uv add fastapi==0.141.1 uvicorn==0.52.1
Create Dockerfile:¶
cat > Dockerfile << 'EOF'
FROM python:3.13-slim
WORKDIR /app
# Official uv-in-Docker pattern: copy the uv binary from the uv image
# (https://docs.astral.sh/uv/guides/integration/docker/)
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
# Dependency layer: only manifest/lockfile changes rebuild this.
COPY pyproject.toml uv.lock ./
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --locked --no-install-project
COPY . .
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --locked
# uv sync creates the project venv at /app/.venv - put it on PATH
ENV PATH="/app/.venv/bin:$PATH"
EXPOSE 8000
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
EOF
Build the image:¶
docker build -t my-ai-app:latest .
Run the container:¶
docker run -d -p 8000:8000 --name my-app my-ai-app:latest
Test it:¶
curl http://localhost:8000/
curl -X POST http://localhost:8000/generate \
-H "Content-Type: application/json" \
-d '{"text": "Hello Docker!"}'
Step 4: Volumes (Persisting Data)¶
By default, container data is lost when the container stops. Volumes fix this!
Bind Mount:¶
# Map local directory to container
docker run -d \
-v $(pwd)/data:/app/data \
my-ai-app:latest
Named Volume:¶
# Create a named volume
docker volume create app-data
# Use it
docker run -d \
-v app-data:/app/data \
my-ai-app:latest
Anonymous Volume:¶
docker run -d \
-v /app/data \
my-ai-app:latest
Step 5: Docker Compose¶
Docker Compose = Multi-container orchestrator
Create docker-compose.yml:¶
cat > docker-compose.yml << 'EOF'
# Note: older examples start with `version: "3.8"` - the key is
# obsolete in the Compose Spec; the modern CLI warns on it, so omit it.
services:
web:
build: .
ports:
- "8000:8000"
volumes:
- ./data:/app/data
environment:
- LOG_LEVEL=info
redis:
image: redis:8-alpine
ports:
- "6379:6379"
volumes:
- redis-data:/data
volumes:
redis-data:
EOF
Run multiple services:¶
# Start all services
docker compose up -d
# View logs
docker compose logs -f
# Stop all services
docker compose down
Step 6: GPU Containers (AI/ML)¶
For AI/ML workloads, you need GPU access!
Check GPU:¶
nvidia-smi
Install NVIDIA Container Toolkit:¶
# Add NVIDIA repositories
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | \
sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
sed "s#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#" | \
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
# Install
sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit
# Configure Docker runtime and restart
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker
Run GPU Container:¶
docker run --gpus all -it --rm \
nvidia/cuda:13.0.2-base-ubuntu24.04 \
nvidia-smi
Python with GPU:¶
docker run --gpus all -it --rm \
-v $(pwd):/workspace \
pytorch/pytorch:2.9.0-cuda13.0-cudnn9-runtime \
python -c "import torch; print(f'CUDA available: {torch.cuda.is_available()}')"
Step 7: Dockerfile Best Practices¶
Bad Dockerfile:¶
FROM python:3.13
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
Problems:
- Installs dependencies every time (if code changes)
- No layer caching optimization
- Uses plain
pipinstead of the uv standard (see the good Dockerfile below)
Good Dockerfile:¶
FROM python:3.13-slim
# Install system dependencies
RUN apt-get update && \
apt-get install -y --no-install-recommends \
gcc && \
rm -rf /var/lib/apt/lists/*
# Set working directory
WORKDIR /app
# Copy manifest + lockfile first (better caching): only manifest/
# lockfile changes rebuild the dependency layer.
COPY pyproject.toml uv.lock ./
# Official uv-in-Docker pattern: copy the uv binary from the uv image
# (https://docs.astral.sh/uv/guides/integration/docker/)
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --locked --no-install-project
# Copy application code
COPY . .
# uv sync creates the project venv at /app/.venv - put it on PATH
ENV PATH="/app/.venv/bin:$PATH"
# Create non-root user
RUN useradd -m appuser && \
chown -R appuser:appuser /app
USER appuser
# Health check
HEALTHCHECK CMD curl -f http://localhost:8000/ || exit 1
EXPOSE 8000
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
Improvements:
- โ Separate dependency layer (manifest + lockfile first)
- โ Cleanup apt cache
- โ Run as non-root user (security)
- โ Add health check
- โ Smaller base image
Step 8: Common Docker Commands¶
Images:¶
# List images
docker images
# Remove image
docker rmi ${IMAGE_ID}
# Prune unused images
docker image prune
Containers:¶
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Stop container
docker stop ${CONTAINER_ID}
# Remove container
docker rm ${CONTAINER_ID}
# View logs
docker logs ${CONTAINER_ID}
# Execute command in running container
docker exec -it ${CONTAINER_ID} bash
System:¶
# Disk usage
docker system df
# Prune everything (careful!)
docker system prune -a
Step 9: Debugging Containers¶
Container won't start?¶
# Check logs
docker logs ${CONTAINER_ID}
# Run in foreground to see output
docker run ${IMAGE}
# Check if port is already in use
netstat -tulpn | grep ${PORT}
Out of memory?¶
# Check container stats
docker stats
# Limit memory
docker run -m 512m ${IMAGE}
Need to inspect?¶
# View container details
docker inspect ${CONTAINER_ID}
# View image layers
docker history ${IMAGE}
Step 10: Minder Academy Context¶
How Docker fits into Minder Academy:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Minder Academy Stack โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ External NAS: โ
โ โโ Qdrant (Vector DB) โ Docker container โ
โ โโ Neo4j (Graph DB) โ Docker container โ
โ โโ Prometheus โ Docker container โ
โ โโ Grafana โ Docker container โ
โ โโ Redis โ Docker container โ
โ โ
โ GPU VM: โ
โ โโ vLLM โ Docker container + GPU โ
โ โโ TGI โ Docker container + GPU โ
โ โโ ReAct Agent โ Docker container + GPU โ
โ โโ GraphRAG โ Docker container + GPU โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Knowledge Check¶
-
What is the main benefit of containers over VMs?
- More efficient resource usage (shared kernel)
-
How do you persist container data?
- Use volumes (bind mounts or named volumes)
-
What's the difference between
docker runanddocker compose up?docker run= single containerdocker compose up= multi-container orchestrator
-
Why do we copy pyproject.toml + uv.lock before code in Dockerfile?
- Better layer caching (dependencies only reinstall when changed)
What's Next?¶
- 1301: K3s Master-Worker Architecture - Orchestrate containers at cluster scale
- 1404: TGI Deployment Guide - Serve LLMs in GPU containers
- 0000-LEARNING-PATH.md - Full curriculum
Quick Reference¶
# Essential commands
docker build -t ${NAME} . # Build image
docker run -d -p 80:80 ${NAME} # Run container
docker ps # List containers
docker logs ${CONTAINER_ID} # View logs
docker exec -it ${CONTAINER_ID} bash # Enter container
docker compose up -d # Start services
docker compose down # Stop services
Next Steps¶
-
TUTORIAL-003: RAG Basics - the next tutorial in the series
-
Docker Commands - Containers, images, volumes and networking in one page.