Explore the Claude Code Docker Sandbox Today
Running an AI coding agent usually means two trade-offs. You give the agent access to your machine, and you send your code and prompts to a hosted model. Docker’s guide Run Claude Code in a Docker Sandbox with Docker Model Runner removes both. Claude Code runs inside a Docker Sandbox, which is an isolated microVM, and its model calls go to Docker Model Runner on your own host. The agent stays contained, and no model traffic leaves your machine.
This post from the Collabnix Team walks through the full setup, explains the networking trick that makes it work, and covers how to debug it. Every command below comes from the official guide and was checked against the Docker CLI reference.
How the pieces fit together
Three components cooperate at runtime, and understanding them makes every later step obvious:
- Docker Model Runner runs on your host and serves an Anthropic-compatible API at
http://localhost:12434. Claude Code already speaks the Anthropic API, so it can talk to Model Runner without any adapter. - The Docker Sandbox runs Claude Code inside a microVM. The microVM has its own network stack, so it cannot reach your host’s
localhostdirectly. - The sandbox proxy runs on your host and brokers every outbound request from the sandbox. It enforces network policy and translates the special hostname
host.docker.internaltolocalhost.
So Claude Code sends its requests to http://host.docker.internal:12434. The proxy rewrites the destination to localhost:12434, and Docker Model Runner answers. The whole round trip stays on your machine.
Prerequisites
- Docker Desktop or Docker Engine installed.
- Docker Model Runner enabled.
- Docker Sandboxes (
sbx) version 0.39.0 or later, installed and signed in.
Model Runner must accept TCP connections, because the sandbox reaches it over the network rather than through a socket. On Docker Desktop you can turn this on under Settings > AI, or from the terminal:
docker desktop enable model-runner --tcp 12434
The --tcp flag sets the TCP port Model Runner listens on. Port 12434 is the default and the one the rest of the guide assumes.
Step 1: Pull a coding model on the host
Models live on the host, not in the sandbox, so pull one before you create the sandbox:
docker model pull ai/devstral-small-2
The guide also suggests ai/qwen3-coder if you want another coding-focused model with a large context window. Either works with the steps below; just swap the model name.
Step 2: Allow the sandbox to reach Model Runner
Sandboxes are network-isolated by default, so you need an explicit policy rule. This is the step most people get wrong. The rule is matched against the destination the proxy forwards to, not the hostname the sandbox uses. Since the proxy rewrites host.docker.internal to localhost before forwarding, you allow localhost:12434:
sbx policy allow network localhost:12434
If you write the rule for host.docker.internal:12434 instead, requests from Claude Code will still be blocked. The sbx policy allow network reference also documents a --sandbox option if you prefer to scope a rule to a single sandbox. For more background, see Accessing host services from a sandbox.
Step 3: Create the sandbox with a persistent base URL
Instead of launching Claude Code right away, create the sandbox first. This lets you check the configuration before the agent starts. From your project directory:
cd ~/my-project
sbx create --name claude-dmr \
-e ANTHROPIC_BASE_URL=http://host.docker.internal:12434 \
claude .
Here claude is the built-in agent, . mounts the current directory as the workspace, and -e sets an environment variable. Because the variable is set at creation time, it is stored with the sandbox and applies to every later session. ANTHROPIC_BASE_URL is the setting that tells Claude Code to send requests to Model Runner instead of api.anthropic.com.
Notice what you do not need: no Anthropic API key and no sbx secret set anthropic. Model Runner does not authenticate its local endpoint, and the sandbox proxy only injects credentials for requests bound for api.anthropic.com. Nothing secret is involved in this setup at all.
To confirm the variable, open a shell in the sandbox:
sbx exec -it claude-dmr bash
echo $ANTHROPIC_BASE_URL
The guide shows this printing http://host.docker.internal:12434.
Step 4: Test connectivity before starting the agent
Still inside the sandbox shell, send a minimal request to the Anthropic-compatible Messages endpoint:
curl http://host.docker.internal:12434/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "ai/devstral-small-2",
"max_tokens": 32,
"messages": [{"role": "user", "content": "Say hello"}]
}'
A successful response proves three things at once: the policy rule is correct, the proxy rewrite works, and the model is loaded. Type exit to leave the shell. Testing with curl first is worth the extra minute, because if Claude Code fails to connect later, you already know whether the problem is the network path or the agent configuration.
Step 5: Launch Claude Code against the local model
Now start Claude Code in the sandbox you created and pass the model name through to the agent:
sbx run --name claude-dmr -- --model ai/devstral-small-2
--name re-attaches to the existing sandbox, and everything after -- is forwarded to the Claude Code CLI. Since ANTHROPIC_BASE_URL is stored with the sandbox, Claude Code routes its requests to Model Runner on your host.
Step 6: Inspect requests and tune the context window
To see exactly what Claude Code is sending to the model, run this on the host, not in the sandbox:
docker model requests --model ai/devstral-small-2 | jq .
docker model requests fetches the requests and responses Model Runner has handled, and --model filters them to one model. This is the fastest way to debug prompts, context usage, and compatibility issues without attaching to the sandbox.
Context size matters a lot for coding agents, because Claude Code sends large prompts with file contents and tool definitions. ai/gpt-oss defaults to a smaller context window than the coding-focused models. To use it on repository-scale work, package a variant with a bigger window on the host:
docker model pull ai/gpt-oss
docker model package --from ai/gpt-oss --context-size 32000 gpt-oss:32k
--from takes an existing model as the source, --context-size sets the window, and the last argument names the new model. Then point Claude Code at it the next time you run the sandbox:
sbx run --name claude-dmr -- --model gpt-oss:32k
Local Sandboxes vs Cloud Sandboxes
This guide is a local sandboxes workflow from end to end. It depends on two things that only exist on your machine: Docker Model Runner serving on your host, and the host-side sandbox proxy that rewrites host.docker.internal to your localhost. That is what keeps all inference on-device.
Docker Cloud Sandboxes are driven by the same sbx CLI through the global --cloud flag. The sbx run reference notes that cloud sandboxes have no host workspace, and the credentials docs say cloud credentials need separate setup. The guide does not describe a cloud variant of this Model Runner workflow, so if keeping model traffic on your own hardware is the goal, local sandboxes are the documented path.
Troubleshooting
- Requests from the sandbox are blocked: check that the policy rule allows
localhost:12434, nothost.docker.internal:12434. - Connection refused or no response: make sure Model Runner TCP access is on, with
docker desktop enable model-runner --tcp 12434or the Docker Desktop setting. - Claude Code still calls Anthropic: open a shell with
sbx exec -it claude-dmr bashand confirmANTHROPIC_BASE_URLis set. - Model not found: pull the model on the host, not in the sandbox, and pass the same name after
--model. - Long prompts fail or get cut off: package a model with a larger context window using
docker model package --context-size. - Unsure what the agent sends: run
docker model requests --model <name> | jq .on the host.
When you are finished, remember that sandboxes persist after Claude Code exits. Stop the sandbox to keep it for later, or remove it entirely. Files in your workspace are not affected either way:
sbx stop claude-dmr
sbx rm claude-dmr
Key takeaways
- Docker Sandboxes isolate Claude Code in a microVM, and Docker Model Runner keeps inference on your host, so neither your machine nor your prompts are exposed.
- Claude Code talks to Model Runner through its Anthropic-compatible API by setting
ANTHROPIC_BASE_URLtohttp://host.docker.internal:12434. - Network rules match the proxy’s forwarded destination, so allow
localhost:12434. - Setting the variable with
sbx create -estores it with the sandbox for every session. - No Anthropic API key or stored secret is needed for this setup.
- Use
docker model requeststo debug anddocker model package --context-sizeto give models room for repository-scale prompts.
References
Source guide: Run Claude Code in a Docker Sandbox with Docker Model Runner. Related Docker docs: Use Claude Code with Docker Model Runner, Get started with Docker Sandboxes, Claude Code in Docker Sandboxes, Docker Sandboxes usage, Docker Model Runner overview, Docker Model Runner API reference, and the CLI references for sbx create, sbx policy allow network, and docker model package.