---
title: "Deploy AI Models with Ollama and Docker"
url: "https://raoofaltaher.com/tutorials/ollama-docker"
description: "Learn how to run large language models locally using Ollama and Docker for development and testing."
---

# Deploy AI Models with Ollama and Docker

-   ollama
-   docker
-   docker-compose
-   local-llm
-   llm

Learn how to run large language models locally using Ollama and Docker. This guide walks through setting up a local AI development environment for experimentation and engineering workflows.

## Prerequisites

[Section titled “Prerequisites”](#prerequisites)

Before we begin, make sure you have:

-   Docker installed ([Get Docker](https://docs.docker.com/get-docker/))
-   Basic familiarity with command line
-   At least 8GB RAM (16GB+ recommended for larger models)

## What is Ollama?

[Section titled “What is Ollama?”](#what-is-ollama)

[Ollama](https://ollama.ai/) is a tool that makes it easy to run large language models locally. It handles:

-   Model downloading and management
-   Optimal resource utilization
-   A simple API for inference

## Step 1: Pull the Ollama Docker Image

[Section titled “Step 1: Pull the Ollama Docker Image”](#step-1-pull-the-ollama-docker-image)

```
docker pull ollama/ollama
```

## Step 2: Run Ollama Container

[Section titled “Step 2: Run Ollama Container”](#step-2-run-ollama-container)

```
docker run -d \  --name ollama \  -p 11434:11434 \  -v ollama:/root/.ollama \  ollama/ollama
```

This command:

-   Runs Ollama in detached mode (`-d`)
-   Maps port 11434 for API access
-   Creates a volume to persist downloaded models

## Step 3: Download a Model

[Section titled “Step 3: Download a Model”](#step-3-download-a-model)

```
docker exec -it ollama ollama pull llama2
```

You can replace `llama2` with other models like:

-   `mistral` - Fast and efficient
-   `codellama` - Optimized for code
-   `llama2:13b` - Larger, more capable

## Step 4: Test the Model

[Section titled “Step 4: Test the Model”](#step-4-test-the-model)

```
docker exec -it ollama ollama run llama2 "Hello, how are you?"
```

## Step 5: Use the API

[Section titled “Step 5: Use the API”](#step-5-use-the-api)

Ollama exposes a REST API on port 11434:

```
curl http://localhost:11434/api/generate -d '{  "model": "llama2",  "prompt": "Explain Docker in simple terms"}'
```

### Python Example

[Section titled “Python Example”](#python-example)

```
import requests
response = requests.post(    "http://localhost:11434/api/generate",    json={        "model": "llama2",        "prompt": "Write a haiku about coding",        "stream": False    })
print(response.json()["response"])
```

## Docker Compose Setup

[Section titled “Docker Compose Setup”](#docker-compose-setup)

For a more robust local setup, use Docker Compose:

```
version: '3.8'
services:  ollama:    image: ollama/ollama    container_name: ollama    ports:      - "11434:11434"    volumes:      - ollama_data:/root/.ollama    restart: unless-stopped    deploy:      resources:        reservations:          devices:            - driver: nvidia              count: all              capabilities: [gpu]
volumes:  ollama_data:
```

> **Note:** The GPU configuration is optional and requires NVIDIA Container Toolkit.

## Tips and Best Practices

[Section titled “Tips and Best Practices”](#tips-and-best-practices)

### 1\. Model Selection

[Section titled “1. Model Selection”](#1-model-selection)

Choose the right model for your use case:

-   **Chat/General:** `llama2`, `mistral`
-   **Code:** `codellama`, `deepseek-coder`
-   **Small/Fast:** `phi`, `tinyllama`

### 2\. Resource Management

[Section titled “2. Resource Management”](#2-resource-management)

```
# Check running modelsdocker exec ollama ollama list
# Stop a model to free memorydocker exec ollama ollama stop llama2
```

### 3\. GPU Acceleration

[Section titled “3. GPU Acceleration”](#3-gpu-acceleration)

For faster inference, use GPU passthrough:

```
docker run -d --gpus all ollama/ollama
```

## Troubleshooting

[Section titled “Troubleshooting”](#troubleshooting)

### Out of Memory

[Section titled “Out of Memory”](#out-of-memory)

-   Try a smaller model (e.g., `llama2:7b` instead of `llama2:13b`)
-   Increase Docker memory limits
-   Close other applications

### Connection Refused

[Section titled “Connection Refused”](#connection-refused)

-   Ensure the container is running: `docker ps`
-   Check port mapping: `docker port ollama`

### Slow Performance

[Section titled “Slow Performance”](#slow-performance)

-   Enable GPU acceleration
-   Use quantized models (e.g., `llama2:7b-q4_0`)

## Next Steps

[Section titled “Next Steps”](#next-steps)

Now that you have Ollama running locally, you can:

1.  **Build a chatbot** - Create a web interface with Next.js
2.  **RAG applications** - Combine with vector databases
3.  **Fine-tuning** - Customize models for your use case

* * *

## Resources

[Section titled “Resources”](#resources)

-   [Ollama Documentation](https://github.com/ollama/ollama)
-   [Docker Documentation](https://docs.docker.com/)
-   [Available Models](https://ollama.ai/library)

* * *

_Found this helpful? Share it with colleagues who are building local AI workflows._

_Have questions? Reach out on [GitHub](https://github.com/raoofaltaher) or [LinkedIn](https://linkedin.com/in/raoofaltaher)._