Isolated, reproducible environment for running the OpenCode CLI against a single project directory.
The goal is to give the AI agent:
- Access only to the project files you point it at
- The same permissions as your current user
- Persistent state / history / cache in a sandboxed directory
- Your existing
~/.config/opencode/opencode.jsonsettings, read‑only
All of this runs inside a lightweight Ubuntu 24.04 Docker container.
This repo contains three main pieces:
Dockerfile– builds an Ubuntu 24.04 image, installs Node.js +opencode-ai, and sets up a simple entrypoint that runsopencodeinside/workspace.entrypoint.sh– minimal wrapper that:- trusts the UID/GID passed via
docker run --user - ensures
HOMEpoints at/home/sandbox - starts
opencodeas the container process.
- trusts the UID/GID passed via
opencode-sandbox– host-side wrapper script that:- builds and runs the container image
- mounts a single project directory at
/workspace - maps sandboxed OpenCode dirs to
~/.opencode-sandbox/*on the host - overlays your real
~/.config/opencode/opencode.jsonas read‑only
Inside the container, OpenCode sees:
/workspace– the project directory you passed on the host/home/sandbox/.config/opencode– backed by~/.opencode-sandbox/config/home/sandbox/.local/share/opencode– backed by~/.opencode-sandbox/share/home/sandbox/.local/state/opencode– backed by~/.opencode-sandbox/state/home/sandbox/.cache/opencode– backed by~/.opencode-sandbox/cache/home/sandbox/.config/opencode/opencode.json– a read‑only bind mount of your real host config at~/.config/opencode/opencode.jsonif present
The container runs as your current user (--user "$(id -u):$(id -g)"),
so file permissions inside /workspace match what you can do on the host.
- Docker (or compatible runtime) installed and working
From the root of this repository:
chmod +x setup.sh
./setup.shWhat setup.sh does:
-
Builds the Docker image as
opencode-sandbox:latest. -
Ensures
~/binexists. -
Creates/refreshes a symlink:
~/bin/opencode-sandbox -> /path/to/this/repo/opencode-sandbox -
Reminds you to add
~/binto yourPATHif it isn’t already.
After running setup.sh, either:
-
Open a new shell, or
-
Add to your shell config (if needed):
export PATH="$HOME/bin:$PATH"
You run opencode-sandbox just like the regular opencode CLI. All arguments will be forwarded to opencode. Only the project directory will be redirected to /workspace inside the container.
From any project directory on the host:
opencode-sandboxor point it to another directory explicitly:
opencode-sandbox /path/to/projectThis will:
-
Start a container from
opencode-sandbox:latest -
Mount the chosen project as
/workspace -
Run
opencodeinside the container, in/workspace, with:HOME=/home/sandbox TERM=$TERM user = your current uid:gid
Your OpenCode state (conversation history, last model, etc.) is stored
under ~/.opencode-sandbox on the host, not inside the project.
cd ~/code/my-app
opencode-sandbox .Once OpenCode starts, interact with it as usual. It will only see:
- The files under
~/code/my-app(mounted at/workspace) - Its own home directory under
/home/sandbox - Your OpenCode configuration (read‑only)
~/bin/opencode-sandbox– symlink created bysetup.sh.~/.opencode-sandbox/– sandboxed OpenCode data:config/→ mapped to/home/sandbox/.config/opencodeshare/→ mapped to/home/sandbox/.local/share/opencodestate/→ mapped to/home/sandbox/.local/state/opencodecache/→ mapped to/home/sandbox/.cache/opencode
Your real OpenCode config remains at:
~/.config/opencode/opencode.json(mounted read‑only into the container).
/workspace– the project directory (single mount)./home/sandbox– the sandbox “home.”opencode– the CLI installed globally vianpm install -g opencode-ai.
This setup is meant to be project‑scoped and unprivileged:
- The container runs as your user (no extra privileges).
- Only the project directory and the sandboxed OpenCode directories are mounted.
- Your real OpenCode config file is mounted read‑only.
- No access is given to the rest of your home directory or arbitrary paths.
You can further harden the container (optional, not done by default):
- Add
--read-onlytodocker runand atmpfsfor/tmp. - Add
--cap-drop=ALLif no extra capabilities are needed.
To update to a newer opencode-ai version:
-
Edit
Dockerfileif you want a specific version, for example:RUN npm install -g opencode-ai@1.0.133or leave it unpinned to always get the latest.
-
Rebuild the image:
./setup.sh
(This will rebuild the image and refresh the symlink again.)
-
opencode-sandboxnot foundMake sure
~/binis in yourPATH:echo "$PATH" | tr ':' '\n' | grep -Fx "$HOME/bin" || \ echo 'Add export PATH="$HOME/bin:$PATH" to your shell config'
-
Permission errors on project files
Confirm you’re running
opencode-sandboxas the same user who owns the project directory.