AI coding agents are now writing Helm charts, fixing YAML, and running kubectl for us. That is useful, but it also raises a simple question: where should the agent run, and what should it be allowed to touch? An agent with your kubeconfig and full access to your laptop can do a lot of damage in a few seconds.
Docker Sandboxes (the sbx CLI) gives every agent its own microVM. Kubernetes gives us a standard way to run workloads. In this post we look at how the two fit together, with three practical patterns you can try today.
What is Docker Sandboxes (sbx)?
Docker Sandboxes runs AI coding agents like Claude Code, Codex, Copilot CLI, OpenCode and Kiro in isolated microVMs. Each sandbox gets its own kernel, filesystem, network and its own private Docker daemon. If the agent breaks something, it breaks it inside a disposable VM, not on your machine.
A few things make it a good fit for Kubernetes work:
- Private Docker Engine. The agent can build images and run containers without touching your host Docker.
- Network policy. You decide which hosts the sandbox can reach with
sbx policy. Blocked requests show up insbx policy log. - Secrets stay on the host. API keys set with
sbx secret setare injected by a proxy into outbound requests, so the agent never sees the real value. - Local or cloud, one CLI. The same commands work for local sandboxes and Docker Cloud Sandboxes (
sbx --cloud run claude).
Install it with one command:
# macOS
brew trust docker/tap && brew install docker/tap/sbx
# Windows
winget install Docker.sbx
# Linux (Ubuntu)
curl -fsSL https://get.docker.com | sudo SBX=1 sh
sbx login
You do not need Docker Desktop to use sbx.
Pattern 1: Give the agent its own throwaway cluster
The safest cluster to give an agent is one that nobody else uses. Since every sandbox has its own Docker daemon, the agent can spin up a small cluster with kind or k3d inside the sandbox. It can apply manifests, break things, and try again. When you are done, you remove the sandbox and the cluster goes with it.
Start a sandbox for your project:
cd ~/my-k8s-app
sbx policy init balanced
sbx run claude --name k8s-lab
Your project folder is mounted into the sandbox, so the agent and your host see the same files. Then ask the agent to install kind and kubectl and create a cluster, or do it yourself from another terminal:
# Run commands inside the sandbox
sbx exec k8s-lab -- kind create cluster --name dev
sbx exec k8s-lab -- kubectl get nodes
sbx exec k8s-lab -- kubectl apply -f k8s/
A good prompt for the agent looks like this:
“Install kind and kubectl. Create a local cluster called dev. Deploy the manifests in ./k8s, then fix anything that fails until all pods are Running. Do not change files outside ./k8s.”
The kind node image comes from Docker Hub and the binaries come from GitHub. The Balanced policy preset allows common development services. If a download is blocked, check the log and allow the host you need:
sbx policy log
sbx policy allow network <host>
When you are finished:
sbx stop k8s-lab # keep packages, images and the cluster for later
sbx rm k8s-lab # delete everything inside the sandbox
Your host files are not deleted when you remove the sandbox. This pattern is great for learning, testing Helm charts, and reviewing pull requests that change manifests.
Pattern 2: Let the agent talk to a real cluster, with limits
Sometimes the agent needs to look at a real dev or staging cluster, for example to read logs or explain why a deployment is stuck. Here the rule is simple: give the agent the smallest access that does the job.
Step 1: Create a read-only identity. Do not hand over your admin kubeconfig. Create a ServiceAccount that can only read:
kubectl create namespace agent-access
kubectl -n agent-access create serviceaccount ai-agent
kubectl create clusterrolebinding ai-agent-view \
--clusterrole=view \
--serviceaccount=agent-access:ai-agent
# Short-lived token, valid for 1 hour
kubectl -n agent-access create token ai-agent --duration=1h
Build a small kubeconfig from that token and place it in the project folder the sandbox can see. The built-in view role cannot read Secrets, and the token expires on its own.
Step 2: Allow only the API server. Start from a strict policy and open just the cluster endpoint:
sbx policy allow network <your-api-server-host>:6443
sbx policy ls
Now even if the agent gets tricked by a prompt injection in a log line or a README, it cannot send your data to some random server, and it cannot change anything in the cluster.
Step 3: Watch what it does. sbx policy log shows blocked requests, and the Kubernetes audit log shows every call made with the ai-agent ServiceAccount. Two layers of visibility, one on each side.
Pattern 3: Run Docker Sandboxes on Kubernetes
The first two patterns put Kubernetes inside or next to a sandbox. This one flips it around: run sbx itself on a Kubernetes cluster, so a platform team can offer sandboxed agents to many developers.
The Kubernetes SIG Apps Agent Sandbox project now ships a docker-sbx example. A Sandbox pod hosts the sbx daemon, and each agent you start runs in its own microVM with its own kernel, network and Docker daemon. Think of it as a sandbox inside a Sandbox.
What you need:
- A cluster with the agent-sandbox controller installed.
- At least one node with
/dev/kvm(bare metal or nested virtualization) and theerofsfilesystem. This usually does not work on kind. - A namespace that allows privileged pods.
- A Docker account with a personal access token, and an API key for your agent.
The flow looks like this:
# Prepare the KVM node
ls -l /dev/kvm && { grep -w erofs /proc/filesystems || sudo modprobe erofs; }
kubectl label node <node> kvm=true
# Docker credentials for sbx
kubectl create secret generic docker-sbx-credentials \
--from-literal=DOCKER_USERNAME="your_docker_username" \
--from-literal=DOCKER_PAT="your_docker_access_token"
# Deploy the Sandbox and wait for it
kubectl apply -f sandbox.yaml
kubectl wait --for=condition=Ready sandbox/docker-sbx --timeout=5m
# Check it and set a network policy
kubectl exec docker-sbx -- sbx diagnose
kubectl exec docker-sbx -- sbx policy init balanced
# Add the API key through stdin, then run an agent
kubectl exec -i docker-sbx -- sbx secret set anthropic <<< "$ANTHROPIC_API_KEY"
kubectl exec -it docker-sbx -- sbx run --cpus 2 --memory 4g claude /workspace
A nice check: run uname -r inside a microVM and on the pod. The kernel versions are different, which proves the agent is running on its own guest kernel and not on the node.
Keep these limits in mind before you take this to production:
- The pod runs privileged, so put it on a dedicated node pool. Clusters that enforce baseline or restricted Pod Security, or policy engines like Kyverno or Gatekeeper, will block it unless you make an exception.
- Without an OS keychain,
sbxkeeps the Docker token and API keys on the data PVC, protected by file permissions only. Use an encrypted StorageClass and limit who cankubectl execinto the pod. - Pass
--cpusand--memoryto keep each microVM inside the pod limits.
Which pattern should you pick?
| Pattern | Best for | Risk level |
|---|---|---|
| 1. Throwaway cluster inside the sandbox | Learning, testing manifests and Helm charts, PR reviews | Lowest. Nothing real is touched. |
| 2. Sandbox talks to a real cluster | Debugging dev or staging, reading logs, explaining failures | Low, if you use read-only RBAC, short tokens and a strict network policy |
| 3. sbx running on Kubernetes | Platform teams offering sandboxed agents to many developers | Needs care: privileged pods and KVM nodes |
Wrapping up
Kubernetes taught us to put workloads in boxes with clear limits. AI agents need the same treatment. Docker Sandboxes gives each agent a microVM with its own Docker daemon, a network policy and host-side secrets. Pair that with Kubernetes RBAC and you get an agent that can do real work on your clusters without holding the keys to everything.
Start with Pattern 1. It costs nothing, runs on your laptop, and shows you how your agent behaves before you let it near a shared cluster.
Useful links:
