Claude Code in a container that delegates to OpenCode

Introduction

Claude Code normally runs on the host, where it can read the home directory, every checkout and the Docker socket. This setup puts it in an Ubuntu 26.04 container that sees one project directory and nothing else of the host, and can still hand a task to an OpenCode worker in another container when it is started with --delegate.

The worker stays outside the sandbox, on the host. The two sides do not share a socket or a network service: the sandbox writes a request into a folder of the mounted project, and a watcher on the host answers it.

This page is the machinery: what is mounted, the image and the launcher, sign-in, the queue and the watcher, the client commands, and what delegation may and may not do. The skill, the brief and the review loop are in Claude Code as a supervisor of OpenCode workers; the worker image and launcher are in the OpenCode Docker setup.

Why a container

A coding agent that can run commands is useful for the same reason it is dangerous: it can read any file the user can read and run any command the user can run. The container answers that with filesystem isolation, not with a permission prompt.

The isolation is filesystem and Docker control, not the network: the container keeps network access. A prompt injection in the project can still reach the network, and it can write anywhere in the mounted project. It cannot reach the host home, another checkout or the Docker socket.

What the container mounts:

MountModeWhat it gives Claude
$PWD at /workread-writeThe project, the only host directory it sees
aredel-claude-home-<uid> at /home/clauderead-writeThe login and settings, in a named volume, not the host ~/.claude
/tmptmpfsScratch space, gone with the container

Not mounted: the host home, ~/.ssh, ~/.config, other checkouts and /var/run/docker.sock.

The image

The image is Ubuntu 26.04, not Alpine, and it is separate from the OpenCode images: no image, home volume or config directory is shared. It carries the tools a coding agent needs to look around and run a project's checks, and the official native installer puts Claude into /opt/claude.

The installer goes into /opt/claude rather than into a home directory because /home/claude is a volume at run time: a binary installed underneath it would be hidden by the mount. The download is a separate step, so a failed curl fails the build instead of feeding an empty script to bash.

# extracted from container/containerfiles/Dockerfile.claude FROM ubuntu:26.04 ENV DEBIAN_FRONTEND=noninteractive \ LANG=C.UTF-8 \ LC_ALL=C.UTF-8 \ DISABLE_AUTOUPDATER=1 RUN apt-get update \ && apt-get install -y --no-install-recommends \ ca-certificates curl git less nano procps python3 ripgrep \ && rm -rf /var/lib/apt/lists/* RUN mkdir -p /opt/claude \ && curl -fsSL -o /tmp/install-claude.sh https://claude.ai/install.sh \ && HOME=/opt/claude bash /tmp/install-claude.sh stable \ && rm -f /tmp/install-claude.sh \ && ln -s /opt/claude/.local/bin/claude /usr/local/bin/claude ARG HOST_UID=1000 ARG HOST_GID=1000 RUN userdel -r ubuntu 2>/dev/null || true; \ groupdel ubuntu 2>/dev/null || true; \ groupadd -g "$HOST_GID" claude 2>/dev/null || true; \ useradd -m -u "$HOST_UID" -g "$HOST_GID" -s /bin/bash claude USER claude WORKDIR /work CMD ["claude"]

There is no sudo: a non-root user cannot install or change the system at run time. A task that needs another tool needs a change to the image; it cannot install it in the running container.

The launcher

start_claude.sh builds the image and starts the container; with --delegate it starts the installed queue watcher on the host. Delegation is off by default: without --delegate no watcher starts, and --no-delegate states that explicitly. The image is built on every run and cached; --rebuild adds --no-cache --pull.

The script takes Claude's own arguments after --, and has a few options of its own:

start_claude.sh [claude args...] start_claude.sh --shell # bash in the container start_claude.sh --rebuild # rebuild the image, no cache start_claude.sh --api-key-file FILE ... # ANTHROPIC_API_KEY from FILE start_claude.sh --delegate # host queue watcher (trusted sessions) start_claude.sh --no-delegate # explicit: no watcher (the default) start_claude.sh --install-tools # copy the launcher and host tools start_claude.sh -- --help # Claude's own option list

The launcher refuses to run from $HOME, from a directory above it, or from /: a mount of the home directory, or of a directory that contains it, would hand Claude everything the mount is meant to keep out. A project inside the home directory is fine.

The container name and hostname carry the project name, so two sandboxes are tellable apart in docker ps and in the shell prompt. The home volume is named after the host UID, because a volume from another UID would refuse the writes.

These are the flags that do the isolation:

# extracted from container/tools/ai/start_claude.sh docker run --rm -i "${tty[@]}" --name "$container" \ --hostname "$host" \ --label "start-claude.project=$PWD" \ --init \ --cap-drop ALL \ --security-opt no-new-privileges \ --read-only \ --tmpfs "/tmp:rw,exec,nosuid,nodev" \ -w /work \ -e HOME=/home/claude \ -e TERM \ "${env[@]}" \ -v "$PWD:/work" \ -v "$VOLUME:/home/claude" \ "$IMAGE" "${cmd[@]}" "$@"

Git commits made inside the container carry your identity without mounting ~/.gitconfig: the launcher reads user.name and user.email from the project and passes them as GIT_AUTHOR_NAME, GIT_COMMITTER_NAME and the matching email variables.

Install and sign in

Run the installer from the project root:

container/tools/ai/start_claude.sh --install-tools

It copies the launcher, Dockerfile.claude, delegate.py, delegate_queue.py, opencode-docker.sh, check_opencode_models.py, free-models.txt and docker/ into ~/.local/share/start-claude/, with the tools under tools/. The host runs the watcher from that copy, outside the mounted checkout, so a sandbox cannot change the code the host executes. Refresh it with the same command from a checkout you trust.

The launcher still finds Dockerfile.claude next to itself first, then in container/containerfiles of the checkout.

From the project directory, start the installed launcher:

~/.local/share/start-claude/start_claude.sh

If you put start_claude.sh on PATH, the short form works too.

The first run builds the image and asks for a Claude login. Open the printed URL on the host, authorize, and paste the code back. Later runs reuse the volume, so the login survives the container.

Two other routes:

export ANTHROPIC_API_KEY=sk-ant-...

~/.local/share/start-claude/start_claude.sh

~/.local/share/start-claude/start_claude.sh --api-key-file ~/.config/claude/api.key

The key file is read on the host; the container never sees the file itself. Keep it outside the project and readable only by you. The same rule and the same handling for the OpenCode key of a paid worker are in a key for a paid model.

The queue

The sandbox cannot run container/tools/ai/delegate.py: that script needs Docker and the opencode launcher. Mounting the Docker socket would give the container control of every container on the host and undo the isolation. Instead, with --delegate, the launcher starts a watcher on the host, and the sandbox writes requests into a folder that is already part of the mounted project.

The queue under the project:

container/reports/delegate/ .queue/ <timestamp>-<slug>-<8 hex>/ request.json brief.md <copied ro-ext folder>/ response.json taken.json

container/reports/ is git-ignored. The client writes request.json atomically, so the watcher never reads half a request. The watcher claims the folder by renaming request.json to taken.json, writes brief.md and response.json, and logs one line. The request folder stays because a running worker may still mount its read-only copies; the watcher removes old folders after seven days.

A request carries only this shape:

{ "version": 1, "action": "start", "slug": "my-task", "brief": "# Task: ...\n", "rw": ["container/tools"], "ro_ext": ["logs"], "image": "ruby" }

The answer has the same version and slug, the exit code and the two streams, clipped by the watcher at 200,000 characters:

{ "version": 1, "action": "start", "slug": "my-task", "returncode": 0, "stdout": "...", "stderr": "...", "started": "2026-12-01T10:00:00+02:00", "ended": "2026-12-01T10:00:01+02:00" }

A --ro-ext directory is copied into the request folder. The worker sees that copy read-only at /work/external/<name>, and the client prints each name; put that path in the brief. The brief itself may live in /tmp inside the sandbox.

The host watcher

With --delegate, start_claude.sh starts delegate_queue.py with the container, from the installed copy, and stops it when the container exits:

# extracted from container/tools/ai/start_claude.sh queue_tool="$INSTALL_DIR/tools/delegate_queue.py" log_file="$INSTALL_DIR/watcher.log" err_file="$INSTALL_DIR/watcher.err" python3 "$queue_tool" serve --repo "$pwd_real" --log "$log_file" \ >>"$err_file" 2>&1 &

INSTALL_DIR is ~/.local/share/start-claude. The log is ~/.local/share/start-claude/watcher.log; startup errors go to watcher.err beside it. The queue is still container/reports/delegate/.queue/, and that folder is still git-ignored.

The watcher runs as the host user with a fixed argv. Before any host command runs, it checks the request format: version, action, slug, brief, rw, ro_ext, image and timeout. It handles up to four requests at once, writes each answer atomically, and keeps one line per request in watcher.log. It prunes request folders older than seven days.

The checks are not only about fields. The watcher refuses a queue folder that resolves outside the project, skips a symlinked request folder, and creates the brief with O_EXCL and O_NOFOLLOW, so a symlink cannot redirect the write. If the queue folder disappears under a running watcher, it puts the folder back and logs that once.

On the host, the watcher is also a command of its own:

python3 container/tools/ai/delegate_queue.py serve --repo .

python3 container/tools/ai/delegate_queue.py serve --repo . --once

A watcher started by hand with --repo . writes watcher.log next to the queue, inside the mount, unless --log says otherwise. The launcher's installed copy is the normal case.

These commands run the checkout copy, not the installed one. --once handles the requests already queued and returns; --delegate-script points at another delegate.py. With --delegate the launcher stops with ERROR: --delegate needs python3 on PATH. when python3 is missing, and with ERROR: --delegate needs the installed tools in ... plus Install them from a checkout you trust: ... --install-tools when the tools are missing.

The client commands

Inside the container, from /work, the client is claude_delegate.py. It writes a request, waits for the watcher and prints the answer:

python3 container/tools/ai/claude_delegate.py start <slug> --brief <file>

python3 container/tools/ai/claude_delegate.py fix <slug> --brief <file>

python3 container/tools/ai/claude_delegate.py status

python3 container/tools/ai/claude_delegate.py wait <slug> --timeout 300

start and fix pass --detach to delegate.py: they return when the host has recorded the round, not when the round ends. wait blocks for the report; status is the quick look, for one task or for all of them. The default timeout of wait is 300 seconds, and the watcher accepts 1 to 3600.

A full start, with a writable path, a read-only copy and an image flag:

python3 container/tools/ai/claude_delegate.py start <slug> \ --brief <file> \ --rw container/tools \ --ro-ext /tmp/logs \ --image ruby

The client checks a writable path before it writes the request; the watcher checks it again before the host command:

# extracted from container/tools/ai/claude_delegate.py from pathlib import Path def check_rw(value: str) -> str: """The launcher rejects these too; refuse here to save a round trip.""" path = Path(value) if value.startswith("-") or path.is_absolute() or ".." in path.parts or not path.parts: raise SystemExit(f"ERROR: --rw {value}: use a relative path inside the worktree") return value

The response keeps the exit code of delegate.py. If no answer arrives, the client prints no answer from the host watcher, keeps the request and returns 4.

What the sandbox may ask for

The queue is a narrow command line, not a remote shell. Only four actions and these fields cross it:

FieldRule
actionstart, fix, status or wait
slug[a-z0-9][a-z0-9-]{0,47}, at most 48 characters
briefnon-empty, at most 100,000 bytes
--rwa relative path inside the worker worktree, no .., at most 20
--ro-exta folder the client copied into the request, at most 20; no symlinks or special files; the client refuses more than 200 MiB or 20,000 entries
--imageone flag from the image flags that delegate.py accepts
timeout1 to 3600 seconds, wait only

No key file, model, worktree, base branch or free-form argument crosses the queue. status may omit the slug and list every task; every other action needs one.

What delegation grants

Delegation is a capability, not a read-only view. The sandbox chooses the brief, and the OpenCode worker executes it with network access and the agent image's credentials. A sandbox that can delegate can spend the model budget and direct that worker. Treat the queue as a permission you grant, not as a window you watch.

Do not add the Docker socket, the host home or another checkout to make the setup simpler. Each of them removes the isolation this page is about.

What the sandbox cannot see

The worker gets its own worktree, ../site-agent-<slug>, next to the checkout on the host. The sandbox cannot see that worktree, its branch or its diff. It reads the report: the status, the worker's last answer and the changed paths.

That is enough to start a round and to decide whether it finished, but not to review the change. Review and merge happen on the host, in the main checkout, where the worktree and the diff are visible. The review loop, the fix rounds and the rule that Claude Code does not commit or merge are in the supervisor page.

The same limit applies to files that are not on main: uncommitted edits and /tmp of the sandbox are not in the worker. Copy what the worker needs into one folder and pass it with --ro-ext; the worker sees /work/external/<name>.

Troubleshooting

Article author: Arthur Isaev