Join our Discord Server
Collabnix Team The Collabnix Team is a diverse collective of Docker, Kubernetes, and IoT experts united by a passion for cloud-native technologies. With backgrounds spanning across DevOps, platform engineering, cloud architecture, and container orchestration, our contributors bring together decades of combined experience from various industries and technical domains.

Running Docker Sandboxes in CI: Headless sbx Login, Exit Codes and JSON Output

6 min read

Terminal-style banner showing the command sbx create --name ci-task --clone claude . with the title Docker Sandboxes in CI: headless login, exit codes and JSON output

Running an AI coding agent on your laptop is one thing. Running the same agent inside a pipeline, with no browser, no keyboard and no human to answer a prompt, is a different problem. The Docker docs now have a dedicated page for exactly this case: Run sandboxes in CI. It explains how to sign in to sbx without a browser, how to drive an agent with sbx create and sbx exec, how to read exit codes, and how to parse output in a way that will not break on the next release.

This post walks through that guide step by step and adds context on how each piece behaves in a script. Every command below comes from the official docs and was checked against the sbx CLI reference and Docker’s Gordon assistant.

Prerequisites

The CI page is about local sandboxes, meaning microVMs that run on the CI runner itself. That puts a few hard requirements on the runner, taken from the install guide:

  • The sbx CLI. You do not need Docker Desktop or Docker Engine to use it.
  • Linux runners: Ubuntu 24.04 or later on 64-bit Intel/AMD or Arm, with KVM hardware virtualization turned on and the runner user in the kvm group. If the runner is itself a VM, it must support nested virtualization.
  • macOS runners: macOS Sonoma 14 or later on Apple silicon.
  • Windows runners: Windows 11 on 64-bit Intel or AMD with Windows Hypervisor Platform enabled.
  • A Docker account and a Docker Personal Access Token (PAT) with at least Read scope.
  • API keys or tokens for the agent you plan to run, for example an Anthropic key for Claude Code.

On a Linux runner you can confirm KVM is present and give the runner user access like this:

lsmod | grep kvm
sudo usermod -aG kvm $USER

The docs note that the output should list kvm_intel, kvm_amd, kvm_arm64 or kvm. The group change only applies after signing out and back in, or after running newgrp kvm. On Ubuntu you can install only the sbx package from Docker’s apt repository:

curl -fsSL https://get.docker.com | sudo REPO_ONLY=1 sh
sudo apt install docker-sbx

Step 1: Sign in without a browser

On a workstation, sbx login opens a browser for Docker OAuth. A CI job cannot do that, so the guide uses a PAT piped through standard input:

echo "$DOCKER_PAT" | sbx login --username <your-docker-id> --password-stdin

Store the PAT in your CI provider’s secret store and expose it as the DOCKER_PAT environment variable for the job. Generate the token from your Docker account settings with at least Read scope.

Two details from the docs matter here. First, sign in before any command that needs authentication. Read-only commands and commands run with --force will not start an interactive sign-in, so a missing login shows up as a failure rather than a hang. Second, create the workspace directory before calling sbx create or sbx run. Without a terminal, these commands fail if the directory does not exist.

Step 2: Preload agent credentials as secrets

Agents need credentials such as model API keys and a GitHub token. The guide suggests storing them as global secrets so every sandbox the runner creates can use them. If your CI environment already exports the variables that each service reads (listed in the built-in services table), import them in one command:

sbx secret import --all

Add --force to overwrite an entry that is already stored, which is useful on long-lived self-hosted runners where a key may have been rotated. To push a single value from your CI secret store, use sbx secret set with -t. In GitHub Actions the step looks like this:

- run: sbx secret set anthropic --force -t "${{ secrets.ANTHROPIC_API_KEY }}"

Step 3: Create the sandbox, run the agent, collect the result

The core of the CI flow is four commands. Create the sandbox in the background, run the agent non-interactively, pull its work back, and clean up:

sbx create --name ci-task --clone claude .
sbx exec ci-task -- claude --dangerously-skip-permissions -p "Review the code and commit your changes"
git fetch sandbox-ci-task
sbx rm --force ci-task

Here is what each part does, based on the sbx create reference:

  • --name ci-task gives the sandbox a fixed name so later steps can address it. Without it, the name defaults to the agent plus the working directory.
  • --clone changes how the workspace is shared. Instead of bind-mounting the checkout, the agent works on a private clone inside the sandbox, and the host repository is mounted read-only. The agent’s commits are exposed to the host through a git remote named sandbox-<name>.
  • claude is the built-in agent, and . is the workspace path, which in CI is usually the job’s checkout directory.
  • sbx exec ci-task -- ... runs a command inside the existing sandbox. Everything after -- is passed to the agent CLI. Here Claude Code runs a single prompt with -p and skips its own permission prompts, which is acceptable because the sandbox is the security boundary.
  • git fetch sandbox-ci-task pulls the agent’s commits from that remote into the runner’s repository, where your pipeline can diff them, run tests or open a pull request.
  • sbx rm --force ci-task removes the sandbox without asking for confirmation.

The --clone mode is a good fit for CI. The checkout on the runner stays untouched, and the agent’s output arrives as normal git commits that you can review before anything is pushed.

Step 4: Handle exit codes and confirmations

Scripts live or die by exit codes, and the guide is precise about them. sbx exec returns the exit status of the command it ran, so a failing agent or test inside the sandbox fails your step. Two special values point at sbx itself rather than your command, so check stderr before retrying:

  • 125: sbx could not run the command at all, for example because the sandbox does not exist.
  • 127: the executable was not found inside a local sandbox.

Confirmations are the other common trap. Prompts need real terminal input, and piping echo y into a command does not confirm it. When stdin is not a terminal, a command that wants confirmation exits with status 1. Declining also returns non-zero. Treat both as an incomplete operation. Use --force on commands that support it. The one exception called out in the docs is sbx kit builder history rm, which uses --yes or -y, because its --force flag is passed through to Buildx.

For idempotent cleanup, check before you remove. sbx mcp rm fails for a server that is not registered, even with --force, and sbx secret rm fails if no secret matches. Run sbx mcp ls or sbx secret ls first and only remove what exists.

Step 5: Parse structured output, not tables

Human-readable tables can change between releases. For scripts, request --format json or --format yaml. Results go to stdout, while errors, warnings, hints and progress go to stderr, so redirecting stderr will not corrupt your JSON.

sbx ls --json | jq -r '.[].name'
sbx prune --dry-run --format yaml

--json is an alias for --format json. On commands that share output flags, --format, --json and --quiet are mutually exclusive, even when two of them ask for the same format. When you only need identifiers, -q drops the table headings: sbx policy ls -q prints local rule IDs and sbx settings ls -q prints setting keys.

Updating older scripts for v0.48.0

Docker Sandboxes v0.48.0 changed several flags and output shapes, and the guide lists them. The ones most likely to break a pipeline:

  • sbx ls now returns a plain array of sandboxes instead of an object with a sandboxes key. Replace the jq filter .sandboxes[].name with .[].name.
  • JSON field names use snake_case, for example created_at and last_used_at, which are RFC 3339 timestamps.
  • --detached on sbx run and sbx env run becomes --detach or -d.
  • sbx logout --yes becomes --force, and --auto-approve on the sbx env commands becomes --force.
  • sbx rm and sbx stop return an array of results with lowercase actions such as removed or failed, including entries for missing targets.

Local sandboxes vs Cloud Sandboxes in CI

Everything above runs a local sandbox on the runner, which means the runner must support hardware virtualization. The guide points to a second option for runners that cannot: cloud sandboxes run without attaching. With the --cloud flag, the sandbox runs on Docker-managed infrastructure, so the runner only needs the CLI and network access.

sbx --cloud run --detach claude --name cloud-task
sbx --cloud exec cloud-task pwd
sbx --cloud rm --force cloud-task

A detached run with --name reuses the named sandbox and starts it if it is stopped, or creates it if it does not exist. Keep these differences in mind when choosing between the two modes, all taken from the cloud docs:

  • Cloud creation does not accept workspace paths, so . is rejected. There is no --clone remote back to the runner. Use sbx --cloud cp or have the agent push to a remote repository.
  • Credentials saved for local sandboxes are not available in the cloud. Configure a cloud credential before launching an agent.
  • Cloud sandboxes expire after one hour by default. The expiration settings let you choose a different time-to-live and timeout action at creation.
  • Cloud sandbox names cannot contain periods, which local names allow. Stick to letters, numbers and hyphens if a script targets both modes.

Troubleshooting tips

  • Create or run fails immediately in CI: make sure the workspace directory exists. Without a terminal, sbx will not create it for you.
  • A command fails with an authentication error: run the PAT login step before anything else in the job.
  • Exit status 1 on a destructive command: it probably asked for confirmation. Add --force (or -y for sbx kit builder history rm).
  • Exit status 125 from sbx exec: the sandbox is missing or the command could not start. Read stderr, then check the create step.
  • jq returns null after an upgrade: you are still using a filter written before v0.48.0, such as .sandboxes[].name. Switch to .[].name.

Key takeaways

  • Use sbx login --username ... --password-stdin with a Read scoped PAT for headless sign-in.
  • Preload agent keys with sbx secret import --all or sbx secret set ... --force -t.
  • sbx create --clone plus git fetch sandbox-<name> gives you the agent’s work as reviewable commits without touching the checkout.
  • Treat sbx exec exit codes 125 and 127 as infrastructure problems, not test failures.
  • Piping echo y never confirms. Use --force.
  • Parse --format json or yaml from stdout, and update filters for the v0.48.0 array output.

References

Source guide: Run sandboxes in CI. Related pages: Install Docker Sandboxes, Use cloud sandboxes, Credentials, and the CLI references for sbx create, sbx exec, sbx login and sbx secret import.

Have Queries? Join https://launchpass.com/collabnix

Collabnix Team The Collabnix Team is a diverse collective of Docker, Kubernetes, and IoT experts united by a passion for cloud-native technologies. With backgrounds spanning across DevOps, platform engineering, cloud architecture, and container orchestration, our contributors bring together decades of combined experience from various industries and technical domains.

Understanding AI Embeddings and Vector Search for Beginners

Explore the world of AI embeddings and vector search, learning how they transform massive data into actionable insights, driving intelligent systems.
Collabnix Team
9 min read
Join our Discord Server