Topic 3 of 5 · 5 min read
Running agents in containers and OS sandboxes
Goal: Pick a sandbox for an agent workload and state which files, processes and credentials it protects and which it leaves exposed.
A sandbox limits what an agent's commands can touch no matter what the agent decides, so it protects you even when a prompt is missed or the model is fooled. Permission rules are a polite request to the tool. A sandbox is a wall enforced by the operating system.
Check yourself · 1 of 3
What does an OS-level sandbox such as bubblewrap or Seatbelt do?
Why it matters
Rules are written for commands you foresee. An agent can reach the same file through a script, a package install hook or a shell trick you did not foresee. A sandbox enforces the boundary below the agent, on every process it starts.
The options, from light to heavy
- OS-level sandbox (Linux bubblewrap, macOS Seatbelt; Claude Code and Codex both use this kind). Commands run on your machine but can write only inside the project folder and reach only approved network hosts. Light, fast, few prompts.
- Dev container. The agent runs inside a container with the repo mounted. It sees the container's files, not your home folder. Weak point: whatever you mount or pass in (a Docker socket, cloud credentials, SSH agent) is exposed.
- Virtual machine. A separate kernel. Stronger separation than a container; heavier to start.
- Cloud agent. A remote disposable machine. The agent never touches your laptop; check what credentials and repositories you grant it.
How to choose
- List what you want to protect: files, running processes, credentials, other networks.
- For each option, ask what is inside the wall. Anything mounted or inherited is inside.
- Match strength to risk. Everyday edits: OS sandbox. Unattended or untrusted code: container or VM with no secrets.
Before and after
Before: a container started with docker run -v $HOME:/work and the host Docker socket. The agent can read every personal file and start any container on the host. It is a container in name only.
After:
docker run --rm -v "$PWD":/work -w /work \
--cap-drop ALL --network none agent-image
Only the project is mounted, no extra privileges, no network until you decide to allow some.
What goes wrong
- Mounting the home folder or credentials into the container "for convenience".
- Trusting a sandbox for the wrong threat. A sandbox that blocks file writes does not stop the agent leaking what it can read, unless the network is limited too.
- Sandbox escapes by retry. Some tools offer to re-run a blocked command outside the sandbox. Treat that prompt as a high-risk approval.
Examples
Naming what is protected
For an OS sandbox on a laptop: project files are writable, the rest of the disk is read-only or hidden, the network is limited to approved domains. Not protected: anything inside the project folder, such as a committed .env file.
Container with a leak
A dev container mounts ~/.aws so the agent can run a deploy script. The wall is now decorative: the agent has the same cloud access you do. Use a short-lived, narrow role instead, or leave deploys out of the sandbox.
Key points
- A sandbox is an operating-system boundary, so it holds even when the agent or its prompts go wrong.
- OS sandboxes are light, containers add a separate file view, virtual machines add a separate kernel.
- Whatever you mount, pass in or leave reachable is inside the wall and exposed.
- File isolation alone is not enough; pair it with network limits so reads cannot leak out.
- A prompt to retry a command outside the sandbox is a serious approval, not a routine one.
Common mistakes
- Calling a container safe while mounting the home folder, a Docker socket or cloud credentials into it.
- Assuming a sandbox that restricts writes also stops data leaving over the network.
- Approving "run outside the sandbox" prompts as routine.
- Running unattended agents on a laptop because setting up a container felt like too much work.
Check yourself · 2 of 3
A dev container is started with the home directory mounted inside. What is the main problem?
Check yourself · 3 of 3
A sandboxed command is blocked and the tool offers to retry it outside the sandbox. How should you treat this?
Practice items, not counted towards your mastery.
Start here
- Configure permissions (Claude Code) (opens in a new tab)DocsFree
Rule syntax, allow/ask/deny precedence, permission modes and settings file locations.
- Sandboxing (Claude Code docs) (opens in a new tab)DocsFree
How OS-level sandboxing, the network proxy and permissions work together.
- Making Claude Code more secure and autonomous with sandboxing (Anthropic) (opens in a new tab)ArticleFree
Why filesystem and network isolation are both needed, and how sandboxing cuts approval prompts.
More for this topic
- Development container reference (Claude Code docs) (opens in a new tab)DocsFree
A working dev container setup for running an agent with limited blast radius.
- sandbox-runtime (Anthropic, GitHub) (opens in a new tab)ToolFree
Open-source sandbox with filesystem and domain rules you can read and reuse.