Why Orchestrate a Local Inference Stack with Docker Compose
In 2026, inference tools for open-source models (such as Llama 3.2, Qwen2.5, Mistral, etc.) are mature. Ollama is suitable for lightweight always-on services, while vLLM excels at high-throughput batch tasks. However, manually switching between them requires changing ports and restarting processes, which is error-prone. Docker Compose allows you to define both services, switch with one command via environment variables or profiles, and share a unified OpenAI-compatible endpoint (e.g., through Nginx reverse proxy or client configuration), enabling flexible scheduling of "local token supply".
Step-by-Step Guide
1. Prerequisites
- A Linux host with an NVIDIA GPU (≥ 8GB VRAM, 16GB+ recommended), with Docker and NVIDIA Container Toolkit installed.
- Install docker-compose (v2 plugin).
2. Write docker-compose.yml
```yaml
version: '3.8'
services:
ollama:
image: ollama/ollama:latest
container_name: ollama
ports:
- "11434:11434"
volumes:
- ./ollama:/root/.ollama
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: all
capabilities: [gpu]
profiles: ["light"]
vllm:
image: vllm/vllm-openai:latest
container_name: vllm
ports:
- "8000:8000"
volumes:
- ./hf_cache:/root/.cache/huggingface
environment:
- HF_TOKEN=${HF_TOKEN}
command: >
--model Qwen/Qwen2.5-7B-Instruct
--tensor-parallel-size 1
--gpu-memory-utilization 0.9
--max-model-len 8192
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: all
capabilities: [gpu]
profiles: ["batch"]
```
3. Start and Switch
- Start lightweight always-on service:
docker compose --profile light up -d - Start batch service:
docker compose --profile batch up -d(stop light first or ensure no port conflict) - Switch client base_url between
http://localhost:11434/v1andhttp://localhost:8000/v1.
4. Pull Models and Verify
- Ollama:
docker exec ollama ollama pull llama3.2 - vLLM: downloads model from Hugging Face on first start (set HF_TOKEN for faster download).
- Test with curl:
curl http://localhost:11434/v1/chat/completions -H "Content-Type: application/json" -d '{"model":"llama3.2","messages":[{"role":"user","content":"Hello"}]}'
5. Integrate into Your Application
Point the OpenAI client's base_url to the corresponding port; api_key can be any string (no auth locally).
Precautions
- VRAM limits: Running both services simultaneously may cause OOM; start on demand.
- Model licenses: Some models (e.g., Llama series) have community licenses; verify before commercial use.
- Network security: Local endpoints have no auth by default; never expose to public internet. Use reverse proxy with authentication for remote access.
- Performance tuning: Adjust vLLM's
--gpu-memory-utilizationand--max-model-lenbased on VRAM; control Ollama concurrency viaOLLAMA_NUM_PARALLEL. - Data persistence: Mount volumes to retain model caches and avoid re-downloading.
Use Cases
- Individual developers or small teams needing low-cost, customizable AI token supply.
- Development/testing environments requiring quick switching between models or inference backends.
- Scenarios with data privacy requirements favoring fully local inference.
- As a backend for an API aggregation gateway, complementing cloud free tiers.