Cloud LLM APIs cost money and see your prompts. Local LLMs are free and private — but they need a sane network architecture if you want to reach them safely from anywhere on your tailnet. This project builds that architecture properly the first time, so you don't have to walk it back when you add a chat UI (part 2) or invite friends (part 3).
The shape: engine + LiteLLM + Tailscale sidecar in one Docker stack, on a private internal network. The engine never opens a port on your host. The Tailscale sidecar is the only thing tagged for the tailnet, and it forwards to LiteLLM, which forwards to the engine. Master key gates access. Tailscale ACL gates who can even reach the master-key prompt.
Who this helps
What you get
docker compose uphttps://ai-rig.tail-abc.ts.net, behind a master keyPrerequisites
docker compose version. If it errors with "unknown command", see the callout below.docker group (sudo usermod -aG docker $USER then newgrp docker)Ubuntu / Pop!_OS / Debian gotcha: the system docker.io package ships Docker Engine but not the Compose v2 plugin. docker compose up will fail with "unknown command: docker compose". Fix with sudo apt install docker-compose-v2. (Or use the legacy hyphenated tool: sudo apt install docker-compose and replace docker compose with docker-compose in every command below.)
The Docker sidecar pattern from Core #4 with two more containers behind the proxy. The Tailscale sidecar is the only outward-facing piece; everything else is on a private Docker network.
The dashed teal box is the Docker bridge network. Nothing inside that box has a port published to your host. The only way in from the tailnet is through the ts-ai sidecar at the top, which speaks HTTPS, and which forwards only to LiteLLM. Part 2 adds Open WebUI on the same private network. Part 3 adds cloud fallback as a config change to LiteLLM.
Both engines speak OpenAI-compatible APIs, so the rest of the project (and parts 2 and 3) work with either. Default in this project: Ollama; the walkthrough also has a llama.cpp tab.
| 🦙 Ollama | ⚡ llama.cpp | 🚀 vLLM | |
|---|---|---|---|
| Audience | Self-hosters who want it to "just work" | Power users, custom quantizations | Production teams serving many users on Nvidia GPUs |
| Docker image | ollama/ollama:latest |
ghcr.io/ggml-org/llama.cpp:server |
vllm/vllm-openai:latest |
| Models | ollama pull by name · easy |
Mount GGUF files from host disk | Reference HF models · downloads on first run |
| Best at | Personal use, small homelabs | Maximum control, specific quants | Continuous batching · multi-user concurrency |
| API | OpenAI-compat (/v1/...) |
OpenAI-compat | OpenAI-compat |
Apple Silicon footnote: Docker Desktop on Mac can't pass Metal through to containers, so Ollama-in-Docker on a Mac runs CPU-only — much slower than native. If you're on Apple Silicon and want Metal, see the "Native engine on Apple Silicon" Variation at the bottom of this page. The compose file in the main path still works on Mac; it'll just be slower than native.
Tier reference: 8 GB ≈ RTX 3070 · 16 GB ≈ Apple Silicon mid · 24 GB ≈ RTX 4090 / M-series Pro · 40+ GB ≈ M3 Max or dual GPU
Six steps. Declare the tag in your ACL, mint the auth key + master key, write the compose file, bring it up + pull a model, verify from another tailnet device, lock down access with ACLs.
Placeholder note. Throughout these steps you'll see tail-abc.ts.net as a placeholder for your actual tailnet name. Replace it with yours everywhere it appears. To find your tailnet name: admin console → DNS → "Tailnet name" at the top, OR run tailscale status on any tailnet device and look at the suffix on any device row. It's something like tailXXXXXX by default; you can rename it in admin → DNS to something prettier (e.g. your-name.ts.net) any time, but it's purely cosmetic.
tag:ai in your tailnet ACL ~2 minThe Tailscale sidecar will register itself with the tag tag:ai. Tailscale rejects any auth key that uses a tag that isn't declared as owned by some group or user — so do this before minting the auth key, or you'll get a requested tags [tag:ai] are invalid or not permitted error from the sidecar at startup.
Admin console → Access controls. Add or merge:
{
"tagOwners": {
"tag:ai": ["autogroup:admin"]
},
"acls": [
// ... your existing rules — don't replace them ...
]
}
Save. Tailscale validates the JSON; a successful save means the tag exists. autogroup:admin is fine for a single-admin tailnet (you); use a named group like group:family if you've defined one.
Two secrets the stack needs:
tag:ai from the tag picker (the option appears now that you declared it in the previous step). Copy the tskey-auth-... string.sk-. openssl rand -hex 24 | sed 's/^/sk-/' works.Save both somewhere temporary; you'll paste them into a .env file in step 3.
SSH into your home server and create the project:
mkdir -p ~/local-ai && cd ~/local-ai
1. .env — secrets only, never committed anywhere:
TS_AUTHKEY=tskey-auth-XXXXX
LITELLM_MASTER_KEY=sk-your-random-string
2. litellm-config.yaml — minimal: one model, master key required for any request:
model_list:
- model_name: llama-3.2-3b
litellm_params:
model: ollama/llama3.2:3b
api_base: http://ollama:11434
litellm_settings:
num_retries: 2
general_settings:
master_key: os.environ/LITELLM_MASTER_KEY
3. serve-config.json — Tailscale Serve config, lives inside the ts-ai sidecar; forwards to LiteLLM by Docker service name:
{
"TCP": { "443": { "HTTPS": true } },
"Web": {
"${TS_CERT_DOMAIN}:443": {
"Handlers": { "/": { "Proxy": "http://litellm:4000" } }
}
}
}
4. docker-compose.yml — three services on one private bridge network. The pattern is the same as Core #4 (Docker sidecar), extended to multiple backend services:
services:
ts-ai:
image: tailscale/tailscale:latest
hostname: ai-rig
environment:
- TS_AUTHKEY=${TS_AUTHKEY?required}
- TS_EXTRA_ARGS=--advertise-tags=tag:ai
- TS_SERVE_CONFIG=/config/serve.json
- TS_STATE_DIR=/var/lib/tailscale
volumes:
- ts-state:/var/lib/tailscale
- ./serve-config.json:/config/serve.json
- /dev/net/tun:/dev/net/tun
cap_add: [net_admin, sys_module]
restart: unless-stopped
networks: [ai-net]
litellm:
image: ghcr.io/berriai/litellm:main-stable
command: ["--config", "/app/config.yaml"]
volumes:
- ./litellm-config.yaml:/app/config.yaml
environment:
- LITELLM_MASTER_KEY=${LITELLM_MASTER_KEY?required}
restart: unless-stopped
networks: [ai-net]
depends_on: [ollama]
ollama:
image: ollama/ollama:latest
volumes:
- ollama-models:/root/.ollama
restart: unless-stopped
networks: [ai-net]
# GPU passthrough — uncomment for Linux + Nvidia:
# deploy:
# resources:
# reservations:
# devices:
# - { driver: nvidia, count: all, capabilities: [gpu] }
networks:
ai-net:
driver: bridge
volumes:
ts-state:
ollama-models:
Notice what's missing: there's no ports: stanza on any service. The engine listens on the Docker bridge network only; LiteLLM listens on the bridge only. The only path in is through the ts-ai sidecar, which Tailscale itself handles.
docker compose up -d
docker compose logs -f ts-ai
# Watch for these lines (in order) — they mean the sidecar registered:
# active login: ai-rig.<your-tailnet>.ts.net
# Switching ipn state Starting -> Running
# serve: creating a new proxy handler for http://litellm:4000
# Startup complete, waiting for shutdown signal
# Then Ctrl-C out of the log tail.
# Confirm from any other tailnet device that ai-rig appears in the status:
# tailscale status | grep ai-rig
# Pull a model into the ollama container:
docker compose exec ollama ollama pull llama3.2:3b
Browse ollama.com/library for more model names. Pull whatever fits your hardware tier; update litellm-config.yaml with new model_name entries as you add them.
From your laptop or phone (any tailnet device that isn't the engine host), call the API. The master key is required:
curl https://ai-rig.tail-abc.ts.net/v1/chat/completions \
-H "Authorization: Bearer sk-your-random-string" \
-H "Content-Type: application/json" \
-d '{
"model": "llama-3.2-3b",
"messages": [{"role":"user","content":"Why is the sky blue?"}]
}'
Wait a few seconds (first call loads the model into memory), get a JSON response. This is the moment the project pays off. Your home rig is doing the inference; LiteLLM is enforcing the master key; Tailscale Serve is the only public face.
While you're at it, confirm the engine is genuinely not reachable from your LAN:
# From any device on your home LAN that's NOT on the tailnet:
curl http://<your-server-LAN-ip>:11434/api/tags
# Should fail (connection refused / timeout). The engine has no host port.
The tag tag:ai was applied to ts-ai when it joined the tailnet. Restrict who can reach it:
{
"tagOwners": { "tag:ai": ["group:family"] },
"acls": [
{ "action": "accept", "src": ["group:family"], "dst": ["tag:ai:443"] }
]
}
Now even a guest user or a tagged container that's on your tailnet can't reach the API unless they're family. Combined with the master key requirement, you have two independent layers gating access.
Reference
Verify
curl /v1/models with the master key returns your model listAuthorization header returns 401Common issues
tag:ai in tagOwners, or the auth key was minted before the declaration was saved. Add "tag:ai": ["autogroup:admin"] to tagOwners, save, mint a fresh auth key, update .env, then docker compose down && docker compose up -d.Authorization: Bearer sk-... with no quotes around the value.docker compose logs ollama to confirm; first run downloads the image.docker compose exec ollama ollama pull <model>. Models live in the container volume and must be pulled explicitly after up.Caveats
Engine swap · Apple Silicon
Native Ollama for Metal acceleration
Docker Desktop on Mac can't pass Metal through to containers, so the in-container Ollama runs CPU-only. If you're on Apple Silicon and want full Metal speed:
launchctl setenv OLLAMA_HOST "0.0.0.0:11434" and restart the Ollama app (this opens a 0.0.0.0 bind — see Caveats above before deciding)ollama service from docker-compose.ymlapi_base from http://ollama:11434 to http://host.docker.internal:11434Trades the clean "engine bound to Docker network only" architecture for native Metal performance. Worth it for serious local model use on Mac.
Engine swap · all platforms
llama.cpp instead of Ollama
Three coordinated changes — the architecture (sidecar / LiteLLM / Tailscale Serve / ACL) is unchanged, only the engine container and how LiteLLM addresses it differ.
1. Swap the ollama service for llama-cpp:
llama-cpp:
image: ghcr.io/ggml-org/llama.cpp:server
# For Nvidia GPU: image: ghcr.io/ggml-org/llama.cpp:server-cuda
command:
- "-m"
- "/models/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf"
- "--host"
- "0.0.0.0"
- "--port"
- "8080"
- "-c"
- "4096"
volumes:
- ./models:/models
restart: unless-stopped
networks: [ai-net]
2. Download a GGUF instead of using ollama pull. llama.cpp loads model files from disk:
mkdir -p ~/local-ai/models && cd ~/local-ai/models
huggingface-cli download \
bartowski/Meta-Llama-3.1-8B-Instruct-GGUF \
Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf \
--local-dir .
cd ..
docker compose up -d
Browse Hugging Face's GGUF models for more. Q4_K_M is a sensible default quantization; Q5_K_M or Q8_0 for higher fidelity at larger size.
3. Change LiteLLM's model entry from ollama/... to openai/...:
model_list:
- model_name: llama-3.1-8b
litellm_params:
model: openai/llama-3.1-8b # 'openai/' prefix = OpenAI-compat backend
api_base: http://llama-cpp:8080
api_key: dummy # llama.cpp doesn't check by default; field still required
The openai/ prefix tells LiteLLM to use the standard OpenAI API shape. llama.cpp's server is OpenAI-compatible at /v1/..., so this routes correctly.
Scaling
One model per llama.cpp process
llama.cpp's server loads one GGUF per container. For multiple models, run multiple llama-cpp services on different ports:
llama-cpp-8b:
image: ghcr.io/ggml-org/llama.cpp:server
command: ["-m", "/models/llama-8b.gguf", "--host", "0.0.0.0", "--port", "8080"]
volumes: [./models:/models]
networks: [ai-net]
llama-cpp-3b:
image: ghcr.io/ggml-org/llama.cpp:server
command: ["-m", "/models/llama-3b.gguf", "--host", "0.0.0.0", "--port", "8081"]
volumes: [./models:/models]
networks: [ai-net]
Then add two LiteLLM model_list entries pointing at llama-cpp-8b:8080 and llama-cpp-3b:8081. Ollama avoids this by loading/unloading on the fly inside a single process — a real ergonomic win, even if llama.cpp is faster per-model on tuned setups.
Hardening
API-key auth at the engine (defense in depth)
If you want application-layer auth at the engine itself (in addition to LiteLLM's master key), pass --api-key <key> in llama.cpp's command array. Then update LiteLLM's api_key: to that value. Most readers don't need this — LiteLLM's master/virtual key system already gates access — but it's there if you want defense in depth.
Engine swap · GPU
vLLM for production-style serving
Swap in vllm/vllm-openai with appropriate model args. Same compose pattern; same Tailscale exposure. Practical mostly with Nvidia GPUs. See Part 3's "Choosing an engine" comparison for when this is worth the effort.
Use case
Editor integrations (Continue / Aider / Cursor)
All three accept custom OpenAI-compatible base URLs. Point them at https://ai-rig.tail-abc.ts.net with the master key. Free, private code completion that uses your home rig. Part 3 covers minting per-tool virtual keys so you can rotate independently and revoke without disrupting the others.
Anti-pattern · not recommended
Engine-direct path (skipping LiteLLM)
Technically possible — drop the litellm service from compose, point ts-ai's serve config at http://ollama:11434, and inside the engine container set OLLAMA_HOST=0.0.0.0:11434 as an env to relax the host-header check. You'll lose the per-key auth that LiteLLM gives you for free, and you can't grow into parts 2 and 3 without rebuilding. Not recommended unless you've decided you genuinely never want a proxy.
You have a private, master-key-gated API. Great for scripts and editor extensions. But chatting via curl is awkward — most readers want a real UI for browsing past conversations, switching models, and just talking to the thing. Part 2 adds Open WebUI on the same Docker network without modifying anything you just built — Open WebUI becomes another client of the LiteLLM you already have.
More natural follow-ups: