Self-hosted OpenCode server running in Docker behind a Caddy reverse proxy with automatic HTTPS. Connect from any machine via TUI (opencode attach). Only the API endpoints required by the TUI are exposed, the web UI and all other endpoints are blocked.
[Your laptop / workstation]
│
│ opencode attach https://oc.yourdomain.com
▼
┌─────────────────────────────────────────┐
│ Caddy :443 (TLS auto, Let's Encrypt) │
│ ↓ API whitelist (TUI endpoints only) │
│ opencode web :4096 (localhost only) │
│ ↓ │
│ opencode.db + projects/ (volumes) │
└─────────────────────────────────────────┘
[VPS]
The server holds your sessions, projects, and tools. You connect to it remotely, your local machine doesn’t need the code or dependencies installed.
Clone the repo
git clone https://github.com/danifernandezs/opencode-selfhost.git
cd opencode-selfhost
Copy and edit the environment file
cp .env.example .env
# Edit .env: set DOMAIN and OPENCODE_SERVER_PASSWORD
# Generate a password: openssl rand -base64 32
Copy and edit the LLM config
cp opencode.json.example opencode.json
# Edit opencode.json: set your provider, API key, and models
Point your domain to the VPS (A record to your VPS IP)
Start the services
docker compose up -d
Connect from any machine:
# TUI (from any terminal with opencode installed)
opencode attach https://oc.yourdomain.com \
--password your-server-password \
--dir /projects/my-project
The web UI is blocked by default. Only the API endpoints used by
opencode attachare proxied by Caddy.
.env)| Variable | Description |
|---|---|
DOMAIN |
Domain name for Caddy TLS (e.g. oc.yourdomain.com) |
OPENCODE_SERVER_PASSWORD |
Basic auth password for the server |
opencode.json)See opencode.json.example for the template. Configure your OpenAI-compatible endpoint, API key, and model definitions (cost, context limits, output limits).
| Volume | Contains | Survives rebuild? |
|---|---|---|
opencode-data |
Sessions, DB, snapshots | Yes |
opencode-cache |
Downloaded tools (ripgrep, LSPs) | Yes (re-downloadable) |
caddy-data |
TLS certificates | Yes |
./projects (bind mount) |
Your code repos | Yes |
Never run
docker compose down -v— the-vflag destroys volumes and all session history.
This project is licensed under CC BY-SA 4.0 — see LICENSE.txt for details.