A script to run OpenCode server with HTTPS support via Caddy reverse proxy. Designed to work seamlessly inside devcontainers.
Run this one-liner inside your devcontainer terminal:
# Start the server on port 5001
curl -fsSL https://raw.githubusercontent.com/PabloZaiden/opencode-server-runner/main/opencode-server.sh | OPENCODE_PORT=5001 bash# Stop the server
curl -fsSL https://raw.githubusercontent.com/PabloZaiden/opencode-server-runner/main/opencode-server.sh | bash -s -- --stopVS Code will automatically detect the exposed port and offer to forward it.
On first run, if you're not authenticated, the script will initiate an authentication flow:
- A URL and device code will be displayed in the terminal
- Visit the URL in your browser
- Enter the device code
- Authorize OpenCode to access your chosen provider
- The script will continue once authentication is complete
Your authentication persists in ~/.local/share/opencode/auth.json and will survive container restarts (but not rebuilds).
curl -fsSL https://raw.githubusercontent.com/PabloZaiden/opencode-server-runner/main/opencode-server.sh | OPENCODE_PORT=5001 bash -s -- --skip-auth- macOS or Linux (including devcontainers)
- OpenSSL (for certificate generation)
Note: OpenCode CLI and Caddy will be installed automatically if not present.
Using different ports for multiple devcontainers:
# Devcontainer 1
curl -fsSL https://raw.githubusercontent.com/PabloZaiden/opencode-server-runner/main/opencode-server.sh | OPENCODE_PORT=5001 bash
# Devcontainer 2
curl -fsSL https://raw.githubusercontent.com/PabloZaiden/opencode-server-runner/main/opencode-server.sh | OPENCODE_PORT=5002 bash
# Devcontainer 3
curl -fsSL https://raw.githubusercontent.com/PabloZaiden/opencode-server-runner/main/opencode-server.sh | OPENCODE_PORT=5003 bashIf you have the script locally:
# Start the server (runs in background)
OPENCODE_PORT=5001 ./opencode-server.sh
# Check status / print connection info (if already running)
./opencode-server.sh
# Stop the server
./opencode-server.sh --stop- Installs OpenCode CLI (if not present)
- Installs Caddy (if not present, Linux only)
- Authenticates with a provider (if not already authenticated)
- Generates a persistent password
- Creates a self-signed SSL certificate
- Starts OpenCode server on localhost:4097
- Starts Caddy as an HTTPS reverse proxy on the configured port
- Prints connection info (IP, port, username, password)
When run inside a git repository, all server data (password, PID file, certificates, logs, Caddyfile) is stored in .opencode-server/ at the repository root. This directory is automatically added to .git/info/exclude so it won't be committed.
When run outside a git repository, data is stored in ~/.config/ (the previous default behavior).
| Environment Variable | Default | Description |
|---|---|---|
OPENCODE_PORT |
5000 |
HTTPS port for external connections |
| File | Description |
|---|---|
~/.local/share/opencode/auth.json |
Provider authentication |
<data-dir>/opencode-server-local |
Persistent password |
<data-dir>/opencode-server.pid |
PID file for running instance |
<data-dir>/opencode-server.log |
Server logs |
<data-dir>/opencode-certs/ |
SSL certificates |
<data-dir>/opencode-caddyfile |
Generated Caddy configuration |
<data-dir> is .opencode-server/ (in git repos) or ~/.config/ (otherwise).
After starting, connect using the displayed URL:
https://<your-ip>:5001
- Username:
opencode - Password: (displayed on start, persisted in config)
Note: You'll need to accept the self-signed certificate warning in your browser/client.
Automated tests are available using Docker:
./tests/test-devcontainer.shThis will spin up a fresh container, run the script, and verify everything works correctly.