Topic 2 of 5 · 4 min read
Container sandboxes for coding agents
Goal: Review a container setup for an agent and tighten its mounts, user and capabilities.
A container gives an agent its own filesystem and process view, but it shares the host's kernel, so it is only as safe as the mounts, user and privileges you give it.
Check yourself · 1 of 3
What does a Docker container NOT isolate from the host?
Why it matters
Running an agent in a Docker container or a dev container is the most common way to keep it away from your laptop or server. It works well when set up with care. It fails when the container is handed the same access as the host, which happens often because it makes things "just work".
What a container is and is not
A container is a normal process with namespaces and resource limits. It sees its own files, its own process list and its own network. It does not run its own kernel. A kernel bug, or a dangerous setting, can let code reach the host. For code you wrote or trust, that is usually an acceptable risk. For unknown code, see the next topic on VMs.
A tightening checklist
- Run as a non-root user. Set a normal
USERin the image. Root inside the container is a much bigger step toward the host. - Mount only the project. Mount the one working folder, not your home folder or
/. Use read-only mounts for anything the agent only needs to read. - Drop capabilities. Start from
--cap-drop=ALLand add back only what is proven necessary. - Block privilege gain. Use
--security-opt no-new-privilegesand keep the default seccomp profile on. - Limit resources. Set memory, CPU and process limits so a runaway loop cannot take the machine down.
- Keep secrets out. Do not bake keys into the image or pass your whole environment. Give the agent a short-lived, narrow token.
Before and after
# Weak
docker run -it --privileged -v "$HOME":/work agent-image
--privileged switches off most isolation, and the mount exposes every file in your home folder. A tighter run:
docker run -it --user 1000:1000 --cap-drop=ALL \
--security-opt no-new-privileges --memory 4g --pids-limit 256 \
-v "$PWD":/work agent-image
What goes wrong
- Convenience flags.
--privileged,--network hostand extra capabilities are added to fix one error and never removed. - Mounting too much. One mounted home folder gives the agent your keys, history and other projects.
- Assuming the container equals a VM. It shares the kernel with the host.
- Open network. Isolation of files does nothing about outbound traffic; see the egress topic.
Examples
Reviewing a compose file
services:
agent:
image: agent-image
privileged: true
volumes:
- ~/:/work
Two problems: privileged: true removes most isolation, and the mount gives the agent the whole home folder. Replace it with a non-root user, cap_drop: [ALL] and a mount of the project only.
A read-only root
Adding --read-only with a small writable tmpfs for /tmp stops an agent from altering tools and config inside the image, and any attempt shows up as an error you can read.
Key points
- A container shares the host kernel, so it is weaker isolation than a virtual machine.
- Run as a non-root user and mount only the project folder, read-only where possible.
- Start from dropped capabilities and no-new-privileges, then add back only what is needed.
- Set memory, CPU and process limits so one runaway task cannot harm the host.
- A container with an open network and mounted secrets is not a sandbox.
Common mistakes
- Using --privileged to get past a permission error.
- Mounting the home directory or the whole repository parent instead of one project.
- Leaving the container as root.
- Treating a container as strong enough for completely untrusted code.
Check yourself · 2 of 3
Which setting most weakens an agent container's isolation?
Check yourself · 3 of 3
What is the best mount for an agent working on one project?
Practice items, not counted towards your mastery.
Start here
- Sandboxing (Claude Code docs) (opens in a new tab)DocsFree
How a coding agent's sandbox limits files and network, and how it pairs with permission prompts.
- Making Claude Code more secure and autonomous with sandboxing (Anthropic) (opens in a new tab)ArticleFree
Why filesystem and network isolation must work together, explained by the team that built it.
- Docker Engine security (opens in a new tab)DocsFree
What containers isolate, what they share with the host, and how capabilities and the daemon socket matter.