Run Harness Agents in Docker Sandboxes (sbx)
Docker Sandboxes (sbx) runs an agent CLI inside a microVM. A custom harness provider whose binary is sbx makes Dagu start sbx exec on the worker, and the agent runs inside the sandbox.
Choose it over a harness container: when the agent should have its own kernel and never see a real API key.
Harness container: | Docker Sandboxes | |
|---|---|---|
| Isolation | Container on the host kernel | MicroVM with its own kernel |
| API keys | In the container environment or mounted files | Stored on the host. A host proxy adds them to outgoing HTTPS requests |
| Network | Container network mode | Allowlist managed with sbx policy |
| Lifecycle | Dagu creates and removes the container | The DAG or an operator creates and removes the sandbox |
| Hosts | Any host with Docker or Podman | macOS on Apple silicon, Windows 11, Ubuntu 24.04 or later with KVM |
The examples on this page were checked with sbx 0.49.
Requirements
- A host that supports local sandboxes: macOS 14 or later on Apple silicon, Windows 11 on x86-64 with Windows Hypervisor Platform, or Ubuntu 24.04 or later with KVM. See Install Docker Sandboxes.
- A Docker account.
sbxon thePATHof the Dagu process that runs the step: the server, scheduler, or worker, not your shell.
A Dagu process inside a container or a Kubernetes pod can start sandboxes only where nested virtualization works. With distributed execution, install sbx and sign in on each worker that runs these steps, and route the steps there with worker_selector.
One-Time Setup
Run these as the user that runs Dagu. On macOS:
brew trust docker/tap
brew install docker/tap/sbx
sbx login
sbx policy init balanced
echo "$ANTHROPIC_API_KEY" | sbx secret set anthropicsbx loginopens a browser. Without one, use a Docker personal access token:echo "$DOCKER_PAT" | sbx login --username <docker-id> --password-stdin.sbx policy init balancedallows AI services and package registries and blocks other outbound traffic. Add hosts withsbx policy allow.sbx secret setstores the key in the OS keychain for every local sandbox, including existing ones. Other services includeopenai,google,github, andcopilot. Runsbx secret set --helpfor the list.
To use a Claude subscription instead of an API key, sign in once inside a long-lived sandbox: run sbx run --name dagu-claude claude, enter /login, then exit. The login stays in that sandbox until it is removed.
Long-Lived Sandbox
Create the sandbox once, with the repository as its workspace:
sbx create --name dagu-claude claude /srv/repos/appThen point a custom provider at it:
working_dir: /srv/repos/app
harnesses:
claude_sbx:
binary: sbx
prefix_args: [exec, dagu-claude, claude, --dangerously-skip-permissions]
prompt_mode: flag
prompt_flag: -p
handler_on:
exit:
run: sbx stop dagu-claude
steps:
- id: review
action: harness.run
output: REVIEW
with:
provider: claude_sbx
model: sonnet
prompt: Review the uncommitted changes and list concrete bugs.Dagu runs this on the worker:
sbx exec dagu-claude claude --dangerously-skip-permissions -p "Review the uncommitted changes and list concrete bugs." --model sonnet- The workspace has the same absolute path inside the sandbox as on the host, and
sbx execstarts there. Add-w <path>afterexecto start elsewhere. sbx execstarts a stopped sandbox first. Installed tools, caches, and agent logins persist.- The agent's stdout is the step's stdout, so
output:captures it. Its exit code is the step's exit code, so retries andwith.fallbackwork as on the host. - Claude Code in
-pmode cannot answer approval prompts.--dangerously-skip-permissionslets it edit and run commands, and the sandbox limits what those commands reach.
The exit handler matters. Stopping or timing out the step ends sbx exec on the worker, but the agent inside the sandbox keeps running until sbx stop. Give each DAG its own sandbox, so one DAG's handler does not stop another DAG's agent.
Fresh Sandbox per Run
Create the sandbox in a step and remove it in the exit handler. With --clone, the agent works on a private Git clone inside the sandbox, and the host repository is mounted read-only.
type: chain
working_dir: /srv/repos/app
harnesses:
claude_sbx:
binary: sbx
prefix_args: [exec, dagu-fix, claude, --dangerously-skip-permissions]
prompt_mode: flag
prompt_flag: -p
handler_on:
exit:
run: sbx rm --force dagu-fix
steps:
- id: sandbox
run: |
sbx rm --force dagu-fix >/dev/null 2>&1 || true
sbx create --name dagu-fix --clone --no-progress claude .
- id: fix
action: harness.run
with:
provider: claude_sbx
prompt: Fix the failing unit tests and commit the change.
- id: collect
run: |
git fetch sandbox-dagu-fix
git branch -f dagu-fix "sandbox-dagu-fix/$(git branch --show-current)"sbx create --cloneadds a Git remote namedsandbox-<name>to the host repository. The clone starts on the host's current branch.collectcopies the agent's commits to the local branchdagu-fix.sbx rmremoves the remote, so fetch before the handler runs.- The first line of
sandboxremoves a sandbox left by a run that ended without its handler, such as a host restart. - Creating a sandbox boots a VM and may pull the agent image, so it is slower than
sbx execinto an existing one.
Drop --clone to mount the repository read-write. The agent's edits then appear in the host working tree immediately.
Pass Environment Variables
DAG env:, params, and step outputs reach the sbx exec process on the worker, not the agent inside the sandbox. Add -e NAME after exec to pass one through. A bare name takes its value from the step's environment:
working_dir: /srv/repos/app
env:
- TICKET_ID: PROJ-123
harnesses:
claude_sbx:
binary: sbx
prefix_args: [exec, -e, TICKET_ID, dagu-claude, claude, --dangerously-skip-permissions]
prompt_mode: flag
prompt_flag: -p
steps:
- id: triage
action: harness.run
with:
provider: claude_sbx
prompt: |
Run ./scripts/fetch-ticket.sh, which reads TICKET_ID,
and summarize the root cause.Do not pass API keys with -e. The value would enter the sandbox, where the agent can read it. Store keys with sbx secret set.
Other Agents
Every agent uses the same shape: exec, the sandbox name, then the agent's non-interactive command.
| Agent | sbx create agent | prefix_args | Prompt | sbx secret set service |
|---|---|---|---|---|
| Claude Code | claude | [exec, NAME, claude, --dangerously-skip-permissions] | prompt_mode: flag, prompt_flag: -p | anthropic |
| Codex | codex | [exec, NAME, codex, exec] | prompt_mode: arg | openai |
| Gemini CLI | gemini | [exec, NAME, gemini] | prompt_mode: flag, prompt_flag: -p | google |
| GitHub Copilot CLI | copilot | [exec, NAME, copilot] | prompt_mode: flag, prompt_flag: -p | copilot |
| OpenCode | opencode | [exec, NAME, opencode, run] | prompt_mode: arg | The model provider's service |
Add each agent's flag for running without approval prompts. Run sbx create --help for every supported agent.
Limits
- Stopping or timing out a step does not stop the agent inside the sandbox. Use
sbx stoporsbx rm --forcein anexithandler. - Do not combine these providers with a harness
container:. Dagu would runsbxinside the container, where it is not installed. sbxflags have changed between releases. Checksbx versionon every worker after an upgrade.
Troubleshooting
| Message | Fix |
|---|---|
error: not signed in to Docker | Run sbx login as the user that runs Dagu |
error: global network policy has not been initialized | Run sbx policy init balanced |
Not logged in · Please run /login | Run sbx secret set anthropic, or /login inside sbx run --name NAME claude |
error: sandbox 'NAME' not found | Create the sandbox, or check the name with sbx ls |
error: sandbox 'NAME' already exists | Reuse it with sbx exec, or remove it with sbx rm --force NAME |
| A request from the agent is blocked | Allow the host with sbx policy allow. sbx policy log shows blocked requests |

