14. Docker Compose Stack

Chapter 14 of 20 · 20 min

Docker Compose manages multi-container deployments. A typical Ollama setup includes the model server and a web interface for easier interaction.

Basic Compose File

version: '3.8'

services:
  ollama:
    image: ollama/ollama:latest
    container_name: ollama
    ports:
      - "11434:11434"
    volumes:
      - ollama-data:/root/.ollama
    environment:
      - OLLAMA_HOST=0.0.0.0
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [gpu]
    restart: unless-stopped

volumes:
  ollama-data:

Save as docker-compose.yml and run:

docker-compose up -d
docker-compose ps

Adding Open WebUI

Open WebUI provides a ChatGPT-like interface:

version: '3.8'

services:
  ollama:
    image: ollama/ollama:latest
    container_name: ollama
    ports:
      - "11434:11434"
    volumes:
      - ollama-data:/root/.ollama
    environment:
      - OLLAMA_HOST=0.0.0.0
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [gpu]
    restart: unless-stopped

  open-webui:
    image: ghcr.io/open-webui/open-webui:main
    container_name: open-webui
    ports:
      - "3000:8080"
    environment:
      - OLLAMA_BASE_URL=http://ollama:11434
    depends_on:
      - ollama
    restart: unless-stopped

volumes:
  ollama-data:

Start the stack:

docker-compose up -d

Access Open WebUI at http://localhost:3000. It automatically connects to the Ollama API at http://ollama:11434.

Adding Ollama WebUI with Persistent Storage

version: '3.8'

services:
  ollama:
    image: ollama/ollama:latest
    container_name: ollama
    ports:
      - "11434:11434"
    volumes:
      - ./models:/root/.ollama
      - ./Modelfiles:/modelfiles
    environment:
      - OLLAMA_HOST=0.0.0.0
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [gpu]
    restart: unless-stopped

volumes:
  models:
  modelfiles:

This uses a host-mounted directory for models, making them accessible from the host filesystem.

Health Checks

Add health checks to ensure services are responding:

services:
  ollama:
    # ... existing config ...
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:11434/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

Upgrading Services

# Pull new images
docker-compose pull

# Recreate containers with new images
docker-compose up -d

# Check logs after upgrade
docker-compose logs -f
EXERCISE

Create a docker-compose.yml with Ollama and Open WebUI. Start the stack, pull a model via docker exec, then access it through the web interface.