---
title: "How to Run Hugging Face Models Programmatically Using Ollama and Testcontainers"
description: "Testcontainers' Ollama module can spin up Ollama in Docker from Java code, pull a model with execInContainer, and use commitToImage to snapshot the container so large models aren't re-downloaded on later runs."
url: "https://swe.quest/articles/how-to-run-hugging-face-models-programmatically-using-ollama-and-testcontainers-dfd8cc8a8d8160a6"
canonical: "https://www.docker.com/blog/how-to-run-hugging-face-models-programmatically-using-ollama-and-testcontainers/"
source: "Docker"
published: "2024-07-11"
difficulty: "medium"
kind: "How-to"
reading_minutes: 5
tags: ["testing","containers","llms","jvm"]
---

# How to Run Hugging Face Models Programmatically Using Ollama and Testcontainers

A post on [Docker](https://swe.quest/sources/docker.md), published 2024-07-11, 5 min read, How-to. Original: [https://www.docker.com/blog/how-to-run-hugging-face-models-programmatically-using-ollama-and-testcontainers/](https://www.docker.com/blog/how-to-run-hugging-face-models-programmatically-using-ollama-and-testcontainers/)

## Summary

Testcontainers' Ollama module can spin up Ollama in Docker from Java code, pull a model with execInContainer, and use commitToImage to snapshot the container so large models aren't re-downloaded on later runs. Hugging Face models in GGUF format can be loaded the same way by installing huggingface-cli inside the container, downloading the GGUF file, writing a Modelfile, and running ollama create, all wrapped in a custom container class such as a TinyLlama subclass. The result is reproducible, codified model setup for local development and CI, queried through Ollama's /api/generate and /api/chat endpoints.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Testing](https://swe.quest/tags/testing.md) (Engineering practices)
- [Containers](https://swe.quest/tags/containers.md) (Infrastructure)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Java & JVM](https://swe.quest/tags/jvm.md) (Languages)

## Prerequisites

- Docker containers ([easy posts on Containers](https://swe.quest/tags/containers.md?difficulty=easy))
- Java ([easy posts on Java & JVM](https://swe.quest/tags/jvm.md?difficulty=easy))
- Testcontainers basics

## Keywords

Testcontainers, Ollama, Hugging Face, GGUF, Docker, TinyLlama, moondream

## Related posts

- [How to Build, Run, and Package AI Models Locally with Docker Model Runner](https://swe.quest/articles/how-to-build-run-and-package-ai-models-locally-with-docker-model-runner-acbc6d65548e5373.md): Docker · 2025-06-12 · Medium · 6 min read · Inference & serving, LLMs, Containers, Developer tools
  Docker Model Runner, enabled as a beta feature in Docker Desktop 4.40+, pulls GGUF-format LLMs from Docker Hub or Hugging Face and runs them locally via the \`docker model\` CLI or an OpenAI-compatible HTTP endpoint on localhost.
- [Beyond Containers: llama.cpp Now Pulls GGUF Models Directly from Docker Hub](https://swe.quest/articles/beyond-containers-llama-cpp-now-pulls-gguf-models-directly-from-docker-hub-cdf9961778a715be.md): Docker · 2025-09-19 · Medium · 3 min read · Inference & serving, Containers, LLMs, ML platforms
  llama.cpp can now pull and run GGUF models directly from Docker Hub via a new --docker-repo (-dr) flag, treating the registry as a versioned model repository using repository:tag syntax.
- [Run LLMs Locally with Docker: A Quickstart Guide to Model Runner](https://swe.quest/articles/run-llms-locally-with-docker-a-quickstart-guide-to-model-runner-ccfbbb71db5d1485.md): Docker · 2025-04-04 · Medium · 5 min read · Inference & serving, LLMs, Containers, Developer tools
  Docker Model Runner, in beta with Docker Desktop 4.40 on Apple silicon, pulls LLMs packaged as OCI artifacts and serves them through an OpenAI-compatible API backed by llama.cpp.
- [Run Gemma 3 with Docker Model Runner: Fully Local GenAI Developer Experience](https://swe.quest/articles/run-gemma-3-with-docker-model-runner-fully-local-genai-developer-experience-c005339b3915484f.md): Docker · 2025-04-09 · Medium · 7 min read · LLMs, Inference & serving, Containers, JavaScript
  Docker Model Runner (in Docker Desktop for macOS 4.40.0+) exposes an OpenAI-compatible API on localhost, so Gemma 3 can be pulled with \`docker model pull ai/gemma3\` and called through the standard OpenAI SDK by pointing baseURL at http://localhost:12434/engines/v1.
- [OpenCode with Docker Model Runner for Private AI Coding](https://swe.quest/articles/opencode-with-docker-model-runner-for-private-ai-coding-640e2f146435d737.md): Docker · 2026-01-15 · Medium · 8 min read · AI for coding, Inference & serving, LLMs, Containers
  OpenCode can use locally hosted models by pointing an @ai-sdk/openai-compatible provider in opencode.json at Docker Model Runner's OpenAI-compatible endpoint (localhost:12434/v1, with TCP enabled on Docker Desktop).

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
