Trying Claude Code in Docker sandboxes
5 min read
About 6 months ago I containerised my coding agent with a dev container, a hand-rolled firewall and a fair amount of sudo wrangling. This week I tried Docker Sandboxes, Docker’s sbx CLI, and had Claude Code running in a sandbox within a few minutes. By comparison, dev containers are useful when you’re in VS Code, but sbx doesn’t need it.
This is the short version I’ll follow myself. It’s mostly a quick start, so for anything beyond these steps the upstream docs are the source of truth. I used sbx v0.55.0, so check the docs if a command looks different for you.
What you need
- A supported machine. I’m on an Apple silicon Mac; Windows 11 and Ubuntu 24.04+ work too. The install page has the details.
- A Docker account.
sbx loginsigns you in through the browser. - No Docker Desktop. This surprised me the most.
sbxruns its own lightweight VM, so you don’t need Docker Desktop or Docker Engine. - An agent account. For Claude Code that’s either an Anthropic API key or a Claude subscription.
Running sandboxes locally is free. You only pay if you use Docker’s cloud sandboxes.
Questioning my workflow
By default sbx mounts your current directory into the sandbox, so the agent edits the same files you see on your machine. You can watch the changes land in VS Code, or any other editor, or even just git diff in another terminal. So you don’t need VS Code, since the agent itself runs in the terminal.
My dev container setup used VS Code to build and run the sandbox. But here, with sbx, the editor is just a viewer you can swap out, and primarily you’ll interact from within the terminal. To be honest, I’m still on the fence about whether I like working in a terminal this way…
Quick start
1. Install
brew trust docker/tap/sbx
brew install docker/tap/sbx
2. Turn off telemetry
sbx collects basic usage data by default: command names, timings, your OS version and your Docker username. It doesn’t collect your prompts or code. I’d rather opt out:
sbx settings set telemetry.disabled true
sbx daemon restart
sbx settings get telemetry.disabled # should print true
Don’t skip the restart, I got caught out by this! I set it, didn’t restart, and it still said false because the running daemon keeps its old value. Adding export SBX_NO_TELEMETRY=1 to your shell profile works too.
3. Sign in and pick a network policy
sbx login
sbx policy init balanced
balanced blocks outbound traffic by default and allows a baseline list: AI provider APIs, package managers, GitHub and container registries. Anything else gets blocked and you’re asked to approve it. That replaces my init-firewall.sh script with one command. The other presets are allow-all and deny-all.
4. Give it GitHub access
sbx secret set github --command 'gh auth token'
This reuses the token from the gh CLI on my Mac. The secret is stored in the macOS Keychain, and a proxy on the host adds it to requests, so the token itself never sits inside the sandbox. If you use an API key for Claude instead of a subscription, sbx secret set anthropic works the same way.
5. Branch, then run Claude
cd ~/dev/westernwilson.com
git checkout -b west/docs/my-new-post
sbx run claude
If you’re on a Claude subscription, run /login inside Claude Code the first time. From there it’s a normal Claude Code session, only it runs inside a sandbox.
6. Review and open the PR
Because the workspace is mounted, the changes are already in my working tree. I prefer to review them and open the PR from the host as usual:
git diff
git add -p
git commit
git push -u origin west/docs/my-new-post
gh pr create
Or let the agent do it. With the github secret from step 4, Claude can commit, push and run gh pr create from inside the sandbox. Two catches: the sandbox doesn’t see your host’s git config, so set user.name and user.email in the repo first, and it can only push to an HTTPS remote, because SSH to github.com is blocked under balanced.
There’s also a --clone mode, sbx run --clone claude ., where the agent works on its own copy of the repo inside the sandbox and you fetch its branches through a sandbox-<name> remote. That’s handy for running several agents at once. The Git workflows page walks through both modes.
7. Clean up
sbx ls
sbx stop claude-westernwilson.com
sbx rm claude-westernwilson.com
Sandboxes are named <agent>-<folder> unless you pass --name. Stopping keeps the sandbox’s state for next time, and removing deletes it.
Closing the firewall gap
In my dev container post I found a gap in the firewall: GitHub’s IP ranges also cover *.github.io, so an IP allowlist let the agent reach any GitHub Pages site. The first option I listed was to filter on hostname instead of IP, which is what sbx does. It’s not perfect: sbx policy --help admits that a denied host sharing an IP with an allowed one can still be reached. But it’s better.
For example
Testing from inside a sandbox, a plain visit to my site is blocked:
curl https://westernwilson.com # 403, asks for approval
But this gets through:
curl -H "Host: westernwilson.com" https://pages.github.com # 200, my site
An HTTPS request names the site twice: outside, where sbx checks it, and inside the encryption, where GitHub Pages picks which site to serve.
The fix
For some hosts, like api.github.com, sbx decrypts the traffic and checks that the two names match, so that request gets a 403. An HTTP rule forces that for other hosts too, because sbx can only check HTTP methods by reading the request:
sbx policy allow network "**.github.com,ruby-lang.org,rubyonrails.org" --method ANY
ANY allows every method, so the agent can’t do anything new, but the request above now gets a 403. Blocking pages.github.com alone isn’t enough: about a dozen other *.github.com sites, like cli.github.com, are on GitHub Pages too, and so are ruby-lang.org and rubyonrails.org from my allow list.
Am I switching?
I’m not deleting my .devcontainer/ yet. But for spinning up an agent on a repo with fewer guardrails, this is a lot less to maintain. Next, I’ll look at connecting VS Code to a sandbox over SSH, so expect a follow-up soon.
Further reading: the Docker Sandboxes get started guide, credentials, network policies and the FAQ.