Sandbox vs Docker: Key Differences Explained
“Isn’t Docker already a sandbox?”
This is one of the most common questions I get at meetups and in the Collabnix Slack. It is a fair question. Docker containers do isolate your code. So why do we now hear about “sandboxes” everywhere, and what does Docker have to do with them?
The short answer: a Docker container is built to package and ship your app. A Docker Sandbox is built to keep an AI agent away from your machine. Both give you isolation, but the wall sits in a very different place.
Let’s break it down with one picture.

Want a quick side-by-side view? The Docker team has put together a clear page that compares Docker Sandboxes with regular containers. It is a great companion to this post: Docker Sandboxes vs Containers.
First, what is a sandbox?
A sandbox is any closed space where code can run without touching the rest of your system. If the code breaks something, only the sandbox breaks. Your files, keys and other apps stay safe.
“Sandbox” is a general idea, not one product. Browsers use sandboxes for tabs. Phones use them for apps. Security teams use them to open suspicious files. A container is one kind of sandbox. A virtual machine is another, stronger kind.
So the real question is not “sandbox or Docker”. It is: how strong is the wall, and what is it made of?
How a Docker container isolates code
A container is a normal process on your machine with some limits around it. Linux gives Docker two main tools for this:
- Namespaces decide what the process can see (its own file system, network, process list).
- cgroups decide how much it can use (CPU, memory).
Here is the key point. Every container on your machine shares the same host kernel. The kernel is the core of the operating system. If code inside a container finds a bug in that kernel and escapes, it lands on your host with access to everything there.
For apps your team writes and reviews, this is a good trade. Containers start in milliseconds, use very little memory, and are the standard way to build, ship and run software. That is not changing.
Where containers fall short for AI agents
AI coding agents are different from your web app. An agent reads a prompt, decides what to do, and then runs real commands: installing packages, editing files, running tests, calling APIs. You did not write those commands, and you often do not read them before they run.
When people try to run agents in a plain container, three problems show up:
- Shared kernel. The agent runs on the same kernel as everything else on your laptop.
- The Docker socket. Agents often need to run
docker buildordocker compose. In a container, that usually means mounting/var/run/docker.sockor using Docker-in-Docker with elevated privileges. Access to the host socket is close to full control of the host. - Secrets inside the box. API keys are passed in as environment variables or mounted files. If the agent can see the key, a bad prompt or a poisoned dependency can leak it.
Telling the agent “please do not touch my home folder” in the prompt is a request, not a boundary.
How a Docker Sandbox isolates code
Docker Sandboxes (the sbx CLI) take a different approach. Each sandbox is a lightweight microVM, not just a process with limits.
Here is a closer look at what sits inside a sandbox, and what sits between the agent and your machine:

Let’s walk through each part:
- Its own kernel. Each sandbox boots a dedicated guest kernel. To reach your host, an escape would have to break the hypervisor, which is a much harder target than the shared kernel.
- sudo stays inside. The agent can have root inside the sandbox and install whatever it needs. That root power ends at the microVM wall. sudo inside the sandbox is not admin on your machine.
- Its own Docker daemon. There is a private Docker engine inside the microVM. The agent can run
docker buildanddocker composeas normal, with no host socket and no host privileges. - Only the folder you choose. The agent sees your project workspace, not your whole disk.
- A policy proxy at the edge. Network access goes through an allowlist you control. Credentials are added to outgoing requests as they leave the sandbox, so the agent never holds the real key.
- Short lived. One sandbox per agent session. When you are done, delete it and start clean.
These limits are set before the agent starts, so the agent cannot widen them on its own.
Side by side
| Docker container | Docker Sandbox | |
|---|---|---|
| Main job | Package and run your app | Run an AI agent safely |
| Isolation wall | Namespaces and cgroups | Hypervisor (microVM) |
| Kernel | Shared with the host and all containers | Its own kernel per sandbox |
| Docker inside | Needs host socket or privileged Docker-in-Docker | Private daemon, no host privileges |
| Secrets | Env vars or mounts inside the container | Injected at the network edge, outside the agent |
| Network | Whatever your run flags allow | Allowlist policy set before the session |
| Lifetime | As long as the service runs | One agent session |
| Start time and memory | Very fast, very light | A bit slower, a bit more memory (it boots a kernel) |
So which one should you use?
Use both. They solve different problems.
Use a container when you are packaging, testing and shipping code your team wrote. Your services, databases and CI jobs belong in containers.
Use a sandbox when something you do not fully control is running commands for you. Claude Code, Codex, Gemini CLI, OpenCode and other coding agents are the obvious case. Running untrusted scripts or AI-generated code is another.
A simple rule to remember:
Containers ship your app. Sandboxes hold your agent.
In fact, a common pattern is an agent inside a sandbox, building and running containers with the sandbox’s own private Docker engine. You get the agent’s speed without handing it your laptop.
Try it in two minutes
Docker Sandboxes ship as a standalone CLI called sbx. It does not need Docker Desktop.
macOS:
brew install docker/tap/sbx
Windows:
winget install Docker.sbx
Then, from your project folder, start an agent inside a sandbox:
cd my-project
sbx run claude
The agent now works on my-project inside its own microVM, with its own kernel and its own Docker engine. Your other folders, SSH keys and host Docker socket stay out of reach.
Wrapping up
Docker containers are still the right way to build and ship software. But a container was never designed to contain an AI agent that writes and runs its own commands. A sandbox adds the stronger wall that job needs: a separate kernel, a private Docker engine, and secrets and network rules that the agent cannot change.
If you have questions or want to share how you run agents today, come say hi in the Collabnix Slack or Discord.
Further reading
- Hand Off Local Agent Work to a Docker Cloud Sandbox: Mountless Prep, sbx move, Disconnected Runs and Template Cleanup
- Understanding AI Embeddings and Vector Search for Beginners
- Share a Live Preview from a Docker Cloud Sandbox: Balanced Egress, Public HTTPS Ports and Clean Withdrawal
- Run OpenAI Codex in Docker Sandboxes: Host-Side OAuth, Bypass Mode Arguments, Pinned v3 Workloads and Session History
- Governing Cross-Platform UI: Managing Native Icon Paradigms Across 30 Internal Tools