|
| 1 | +# Ollama with Tailscale Sidecar Configuration |
| 2 | + |
| 3 | +This Docker Compose configuration sets up [Ollama](https://ollama.com) with Tailscale as a sidecar container to keep the API reachable securely over your Tailnet. |
| 4 | + |
| 5 | +## Ollama |
| 6 | + |
| 7 | +[Ollama](https://ollama.com) lets you run large language models (LLMs) locally — such as Llama 3, Mistral, and Gemma — with a simple API compatible with the OpenAI client format. Pairing it with Tailscale means you can access your local models from any device on your Tailnet (phone, laptop, remote machine) without exposing the API to the public internet. |
| 8 | + |
| 9 | +## Configuration Overview |
| 10 | + |
| 11 | +In this setup, the `tailscale-ollama` service runs Tailscale, which manages secure networking for Ollama. The `app-ollama` service uses Docker's `network_mode: service:tailscale` so all traffic is routed through the Tailscale network stack. The Ollama API remains Tailnet-only by default unless you explicitly expose the port to your LAN. |
| 12 | + |
| 13 | +An optional `yourNetwork` external Docker network is attached to the `tailscale` container. This allows other containers on the same host (such as Open WebUI or other LLM frontends) to reach Ollama via its Tailscale IP, keeping inter-container communication on the same overlay network. |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- The host user must be in the `docker` group. |
| 18 | +- The `/dev/net/tun` device must be available on the host (standard on most Linux systems). |
| 19 | +- Pre-create the bind-mount directories before starting the stack to avoid Docker creating root-owned folders: |
| 20 | + |
| 21 | +```bash |
| 22 | +mkdir -p config ts/state ollama-data |
| 23 | +``` |
| 24 | + |
| 25 | +- If you use the optional `yourNetwork` network, create it first if it does not already exist: |
| 26 | + |
| 27 | +```bash |
| 28 | +docker network create yourNetwork |
| 29 | +``` |
| 30 | + |
| 31 | +If you don't use a shared proxy network, remove the `networks:` sections from `compose.yaml`. |
| 32 | + |
| 33 | +## Volumes |
| 34 | + |
| 35 | +| Path | Purpose | |
| 36 | +| --------------- | ------------------------------------------------------------------ | |
| 37 | +| `./config` | Tailscale serve config (`serve.json`) | |
| 38 | +| `./ts/state` | Tailscale persistent state | |
| 39 | +| `./ollama-data` | Downloaded Ollama models (can be large — ensure enough disk space) | |
| 40 | + |
| 41 | +## MagicDNS and HTTPS |
| 42 | + |
| 43 | +Tailscale Serve is pre-configured to proxy HTTPS on port 443 to Ollama's internal port 11434. To enable it: |
| 44 | + |
| 45 | +1. Uncomment `TS_ACCEPT_DNS=true` in the `tailscale` service environment. |
| 46 | +2. Ensure your Tailnet has MagicDNS and HTTPS certificates enabled in the [Tailscale admin console](https://login.tailscale.com/admin/dns). |
| 47 | +3. The `serve.json` config in `compose.yaml` uses `$TS_CERT_DOMAIN` automatically — no manual editing needed. |
| 48 | + |
| 49 | +You can then reach Ollama at `https://ollama.<your-tailnet-name>.ts.net`. |
| 50 | + |
| 51 | +## Port Exposure (LAN access) |
| 52 | + |
| 53 | +By default, the `ports:` section is commented out — Ollama is only accessible over your Tailnet. If you also want LAN access (e.g. from devices not on Tailscale), uncomment it in `compose.yaml`: |
| 54 | + |
| 55 | +```yaml |
| 56 | +ports: |
| 57 | + - 0.0.0.0:11434:11434 |
| 58 | +``` |
| 59 | +
|
| 60 | +This is optional and not required for Tailnet-only usage. |
| 61 | +
|
| 62 | +## API Key (Optional) |
| 63 | +
|
| 64 | +Ollama supports a simple bearer token for API access. Set `OLLAMA_API_KEY` in your `.env` file to enable it. Leave it blank to allow unauthenticated access (safe when Tailnet-only). |
| 65 | + |
| 66 | +## First-time Setup |
| 67 | + |
| 68 | +After starting the stack, pull a model to get started: |
| 69 | + |
| 70 | +```bash |
| 71 | +docker exec app-ollama ollama pull llama3 |
| 72 | +``` |
| 73 | + |
| 74 | +You can then send requests to the API: |
| 75 | + |
| 76 | +```bash |
| 77 | +curl http://<tailscale-ip>:11434/api/generate \ |
| 78 | + -d '{"model": "llama3", "prompt": "Hello!"}' |
| 79 | +``` |
| 80 | + |
| 81 | +Or if using HTTPS via Tailscale Serve: |
| 82 | + |
| 83 | +```bash |
| 84 | +curl https://ollama.<your-tailnet-name>.ts.net/api/generate \ |
| 85 | + -d '{"model": "llama3", "prompt": "Hello!"}' |
| 86 | +``` |
| 87 | + |
| 88 | +## Files to check |
| 89 | + |
| 90 | +Please check the following contents for validity as some variables need to be defined upfront. |
| 91 | + |
| 92 | +- `.env` — Set `TS_AUTHKEY` (required). Optionally set `OLLAMA_API_KEY`. |
| 93 | + |
| 94 | +## Useful Links |
| 95 | + |
| 96 | +- [Ollama official site](https://ollama.com) |
| 97 | +- [Ollama model library](https://ollama.com/library) |
| 98 | +- [Ollama GitHub](https://github.com/ollama/ollama) |
| 99 | +- [Tailscale auth keys](https://tailscale.com/kb/1085/auth-keys) |
| 100 | +- [Tailscale Serve docs](https://tailscale.com/kb/1312/serve) |
| 101 | +- [Open WebUI](https://github.com/open-webui/open-webui) — a popular browser-based UI for Ollama |
0 commit comments