Skip to content

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
IsolationContainer on the host kernelMicroVM with its own kernel
API keysIn the container environment or mounted filesStored on the host. A host proxy adds them to outgoing HTTPS requests
NetworkContainer network modeAllowlist managed with sbx policy
LifecycleDagu creates and removes the containerThe DAG or an operator creates and removes the sandbox
HostsAny host with Docker or PodmanmacOS 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.
  • sbx on the PATH of 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:

sh
brew trust docker/tap
brew install docker/tap/sbx
sbx login
sbx policy init balanced
echo "$ANTHROPIC_API_KEY" | sbx secret set anthropic
  • sbx login opens a browser. Without one, use a Docker personal access token: echo "$DOCKER_PAT" | sbx login --username <docker-id> --password-stdin.
  • sbx policy init balanced allows AI services and package registries and blocks other outbound traffic. Add hosts with sbx policy allow.
  • sbx secret set stores the key in the OS keychain for every local sandbox, including existing ones. Other services include openai, google, github, and copilot. Run sbx secret set --help for 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:

sh
sbx create --name dagu-claude claude /srv/repos/app

Then point a custom provider at it:

yaml
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:

text
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 exec starts there. Add -w <path> after exec to start elsewhere.
  • sbx exec starts 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 and with.fallback work as on the host.
  • Claude Code in -p mode cannot answer approval prompts. --dangerously-skip-permissions lets 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.

yaml
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 --clone adds a Git remote named sandbox-<name> to the host repository. The clone starts on the host's current branch.
  • collect copies the agent's commits to the local branch dagu-fix. sbx rm removes the remote, so fetch before the handler runs.
  • The first line of sandbox removes 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 exec into 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:

yaml
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.

Agentsbx create agentprefix_argsPromptsbx secret set service
Claude Codeclaude[exec, NAME, claude, --dangerously-skip-permissions]prompt_mode: flag, prompt_flag: -panthropic
Codexcodex[exec, NAME, codex, exec]prompt_mode: argopenai
Gemini CLIgemini[exec, NAME, gemini]prompt_mode: flag, prompt_flag: -pgoogle
GitHub Copilot CLIcopilot[exec, NAME, copilot]prompt_mode: flag, prompt_flag: -pcopilot
OpenCodeopencode[exec, NAME, opencode, run]prompt_mode: argThe 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 stop or sbx rm --force in an exit handler.
  • Do not combine these providers with a harness container:. Dagu would run sbx inside the container, where it is not installed.
  • sbx flags have changed between releases. Check sbx version on every worker after an upgrade.

Troubleshooting ​

MessageFix
error: not signed in to DockerRun sbx login as the user that runs Dagu
error: global network policy has not been initializedRun sbx policy init balanced
Not logged in · Please run /loginRun sbx secret set anthropic, or /login inside sbx run --name NAME claude
error: sandbox 'NAME' not foundCreate the sandbox, or check the name with sbx ls
error: sandbox 'NAME' already existsReuse it with sbx exec, or remove it with sbx rm --force NAME
A request from the agent is blockedAllow the host with sbx policy allow. sbx policy log shows blocked requests

Dagu is open source under the GNU General Public License v3.0.