Course contents · Lesson 16 of 16

Capstone: Package and Run Your LLM App with Docker

Ship It · Lesson 16 of 16 · 11 min Hands-on

Your handbook assistant works, but only on your laptop and only after a ritual: start Ollama, make a virtual environment, install packages, build the index, keep uvicorn running. A colleague would have to repeat all of it. In this capstone you package it as an LLM app with Docker: one image for your code, one Compose file that starts the app and its model server together, and volumes that keep the slow parts between runs.

The food truck: the app and its model server together

Until now the app ran as a Python process on your laptop, and Ollama in lesson 1's container. Packaging means putting your code, its Python and its packages into an image, then describing both servers in one Compose file. Your colleague then needs only Docker: no Python and no virtual environment.

The lab, course-labs/building-llm-apps/16-capstone, holds lesson 15's app plus a GET /health endpoint, with its settings read from the environment, next to a Dockerfile and a docker-compose.yml. The diagram shows what Compose builds from them.

Box diagram. Your browser opens localhost:8000, and the one published port, 127.0.0.1:8000 to 8000, leads into the app container. A dashed box is the Compose network 16-capstone_default, holding the app container, FastAPI and uvicorn built from your Dockerfile on port 8000, and the ollama container from ollama/ollama:0.40.19 on port 11434. The app calls http://ollama:11434, not localhost:11434, because Docker's DNS turns the name into an address. A note says port 11434 is not published, so only the app can call Ollama. Below, two named volumes: index-data at /data in the app, holding index.json with 34 chunks, and ollama-models at /root/.ollama, holding the models. Both survive docker compose down; down -v deletes them.
Your browser uses the one published port; the app finds Ollama by name inside the network.

Notice the two kinds of address: your browser uses localhost:8000, the only published port, while the app reaches Ollama by its service name.

A small Dockerfile for the app

A Dockerfile is the recipe for an image; Dockerfile Basics explains each instruction. This one keeps that lesson's habits, a slim pinned base, a non-root user and the packages before the code, and starts uvicorn as the container's main process:

FROM python:3.13.18-slim
ENV PYTHONUNBUFFERED=1
WORKDIR /app
RUN useradd --create-home appuser && mkdir /data && chown appuser /data
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER appuser
EXPOSE 8000
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]

python:3.13.18-slim pins an exact Python release on slim Debian; lesson 15's FastAPI needs 3.11 or newer. PYTHONUNBUFFERED=1 makes Python write each line at once, so docker compose logs is never behind. /data, owned by appuser, is where the index volume goes: Docker gives a new, empty named volume the owner of the folder it covers, so the app may write there.

--host 0.0.0.0 looks like the opposite of lesson 15's advice, but it is not. Inside a container, 127.0.0.1 is the container's own loopback, and Docker delivers published traffic to the container's network address, so uvicorn must listen on all of them. What keeps the app private is the publish rule on your laptop's side, 127.0.0.1:8000:8000.

Five stacked slabs, built bottom to top. Bottom: FROM python:3.13.18-slim, Debian with Python 3.13, downloaded once. Then RUN useradd appuser and mkdir /data, cached because it never changes. Then COPY requirements.txt and RUN pip install, cached until requirements.txt changes. Then COPY . ., your code, rebuilt on every code edit, in seconds. On top, a dashed lid of settings with no files: USER appuser, EXPOSE 8000 and CMD uvicorn. Footer: code changes most, so it goes last, and everything below it stays cached.
Code goes last, so a code edit rebuilds one thin layer instead of reinstalling FastAPI.

Each build step stores a layer, and Docker reuses a stored layer until it or one below it changes. The aha is the order: editing app.py rebuilds only the top layer, in seconds.

Compose: two services on one network

The Compose file describes both servers, their storage and their start order (trimmed: the lab file adds comments and the app's own health check):

services:
  ollama:
    image: ollama/ollama:0.40.19
    volumes:
      - ollama-models:/root/.ollama
    healthcheck:
      test: ["CMD", "ollama", "list"]
      interval: 5s
      timeout: 5s
      retries: 12
  app:
    build: .
    ports:
      - "127.0.0.1:8000:8000"
    environment:
      OLLAMA_URL: http://ollama:11434
      INDEX_PATH: /data/index.json
      CHAT_MODEL: llama3.2:3b
    volumes:
      - index-data:/data
    depends_on:
      ollama:
        condition: service_healthy

volumes:
  ollama-models: {}
  index-data: {}

Compose joins both services to one network, 16-capstone_default, and Docker's built-in DNS, the service that turns names into addresses, answers each service name with that container's address (Docker Compose Basics). So OLLAMA_URL is http://ollama:11434. Inside the app container, localhost is the app container itself, where nothing listens on 11434.

Only app has ports. Containers on one network reach each other's ports directly, so ollama publishes nothing: its API, which has no password, stays off your laptop's ports and cannot clash with lesson 1's container.

Two named volumes, storage Docker keeps outside any container, hold what is slow to make. ollama-models sits where Ollama stores models, as in lesson 1; index-data sits at /data, where INDEX_PATH points index.py and app.py. Without it, the index would live inside the container and vanish with docker compose down, which removes containers. Volume names start with the project name, so the models download once more here.

Health checks, start order and a one-off model pull

A plain depends_on only orders the start, and a running container is not yet a ready one. A health check is a command Docker runs inside the container at intervals; exit code 0 means healthy. The Ollama image installs no curl, so the check is ollama list, which asks the server for its models and fails while it gets no answer. It runs every 5 seconds; 12 failures in a row mean unhealthy.

With condition: service_healthy, Compose starts app only once ollama passes. The app's own check fetches /health with Python's urllib, as the slim image has no curl either; that endpoint never calls Ollama, so a slow model cannot make the app look broken.

Models are data, not code, so they stay out of the image, which would otherwise grow by gigabytes. Instead, docker compose exec runs a one-off command inside a running container: pull the models into the volume, then run the index step in the app container.

Flowchart in six steps. docker compose up --build -d covers the first row: 1, build the app image 16-capstone-app from the Dockerfile; 2, start ollama from ollama/ollama:0.40.19 with its models volume; then the question healthy? If no, run ollama list again in 5 seconds; if yes, 3, start the app, uvicorn on port 8000, with its index volume. The second row runs with docker compose exec, right to left: 4, once, pull llama3.2:3b and nomic-embed-text; 5, once, index the documents with python index.py, 34 chunks to /data; 6, ask in the browser at localhost:8000 and get answers with sources. Footer: next time, docker compose up -d, then go straight to 6, because the models and the index are still in their volumes.
The bottom row happens once per machine.

The aha is that bottom row: pull and index once, and from then on docker compose up -d leads from a healthy Ollama straight to answers.

Final checklist: README, clean-up and what next

A packaged app is only as easy as its README. The lab's is a pattern to copy: what you need (Docker with about 6 GB of memory), these three steps, what you should see after each, how to stop, and the common errors:

docker compose up --build -d                          # 1. build and start
docker compose exec ollama ollama pull llama3.2:3b    # 2. pull the models, once
docker compose exec ollama ollama pull nomic-embed-text
docker compose exec app python index.py               # 3. index the documents, once

To stop, docker compose down removes the containers and the network and keeps both volumes. docker compose down -v --rmi local also deletes the volumes and the app image Compose built (see the down reference), but keeps ollama/ollama:0.40.19, which lesson 1 uses too.

Ideas for what to build next:

  • A different document set: replace the files in data/handbook, run up --build -d and the index step, then docker compose restart app, as the app keeps the old index in memory.
  • A better model: pull it and set CHAT_MODEL. Bigger models need more memory and are slower on a CPU; a new embedding model needs a new index.
  • A hosted API swapped in: make chat_stream() call it, with its key passed in the environment at run time, never in the image. Your text then leaves the laptop, and every token costs money (lesson 12).

Check yourself

A teammate sets OLLAMA_URL to http://localhost:11434 in the app service. Why does every question now fail?

In the app container, localhost is the app container itself, and nothing listens on 11434 there; the service name ollama leads to the other container.

Someone deletes the index-data volume line to shorten the file. What breaks, and when?

Nothing at first: the index is written inside the container. But docker compose down removes the container and the index with it, so the page then reports the index missing.

Uvicorn listens on 0.0.0.0 inside the container. Can a classmate on your Wi-Fi open the app?

No. 0.0.0.0 covers the container's own addresses; from outside, the only way in is the published port, which Compose binds to 127.0.0.1 on your laptop.