An autonomous coding agent can turn “clean up the build” into an rm -rf command aimed at the wrong directory. If it runs with your host account’s permissions, it can delete files you can delete, including files in synced folders. It can also read credentials that your shell can access. Running the model locally changes where inference happens. The agent’s shell and tools still inherit the permissions you give them.

Untrusted content adds another risk. A README, issue, webpage, or tool response can contain instructions planted for an agent to follow. An agent with file access and outbound networking could read a project’s .env file and transmit its contents. The OWASP Top 10 for Large Language Model Applications covers prompt injection and other risks that matter when a model can call tools.
Start with four guardrails. Share one disposable workspace. Run as non-root, drop capabilities, and keep syscall filtering enabled. Deny outbound networking by default. Keep durable credentials outside the sandbox. Apply the same restrictions to subprocesses and integrations that act on the agent’s behalf.
Choosing Your Isolation Layer
OS-level process sandboxes
A process sandbox suits short tasks that need native tools. Anthropic’s Sandbox Runtime (srt) uses Seatbelt through sandbox-exec on macOS and bubblewrap on Linux. Codex uses bubblewrap and seccomp on Linux, with Landlock available through its legacy implementation. On Windows, AppContainer restricts access for applications launched under a supported AppContainer configuration.
Check what your chosen policy allows the agent to read, as well as what it allows the agent to write. For example, srt can allow unrestricted reads under its built-in defaults. Test with a harmless file outside the intended workspace before exposing a real project.
Hardened rootless containers
Rootless Docker and Podman suit repeatable coding, testing, and automation jobs. Rootless mode reduces the runtime’s privileges on the host. The --user 1000:1000 flag independently sets a non-root identity for the workload inside the container. The workload still shares the underlying Linux kernel, so keep the default seccomp profile enabled. See the Docker Engine security guide for the roles of namespaces, capabilities, and the daemon.
MicroVM-backed sandboxes
Choose a microVM for jobs that warrant a separate guest kernel, especially when the agent needs to build container images. The Docker Sandboxes overview describes how sbx gives the agent a private Docker engine inside its VM. Kata is another option for teams that manage their own runtime. A directly mounted workspace can still be changed from inside the guest. Share a disposable copy or use a workspace mode that keeps edits inside the VM.
Step-by-Step Guide to Locking Down a Container for AI Agents
⚠️ Warning: Never mount /var/run/docker.sock or run an agent container with --privileged. Access to a rootful Docker socket effectively hands the agent root control of the host. The agent can ask the daemon to start another container with the host filesystem mounted. For image builds, use rootless Podman in a dedicated build environment or Docker sbx with its private VM engine. Keep host engine sockets outside the agent’s reach.
Prepare a disposable workspace
💡 Pro-Tip: Create a dedicated Git worktree or disposable copy before bind-mounting it. For example, -v /path/to/agent-worktree:/workspace shares that folder for edits. Review runaway refactoring with git diff on the host, then discard the workspace without touching your main branch. Keep the main checkout and shared Git metadata outside the mount.
Place the reviewed copy in ./workspace. Check it for tracked secrets, local configuration, and files the agent has no reason to read. Preload a trusted image containing /bin/sh and the tools needed for the task. The examples use local/agent-tools:reviewed as the tag for that preloaded image.
Launch an offline, read-only test
Run docker context ls and docker --context rootless info to confirm that the rootless context points to the intended engine. Start with a shell so you can test the boundary before running the agent.
docker --context rootless run --rm -it --pull=never \
--user 1000:1000 \
--cap-drop=ALL \
--security-opt=no-new-privileges \
--read-only \
--network none \
--memory="4g" --cpus="2.0" --pids-limit=256 \
--tmpfs /tmp:rw,nosuid,nodev,size=128m,mode=1777 \
--mount "type=bind,source=$PWD/workspace,target=/workspace,readonly" \
--workdir /workspace --env HOME=/tmp \
--entrypoint /bin/sh local/agent-tools:reviewed
Inside the shell, id -u should return 1000. Confirm that touch /workspace/.write-check fails and touch /tmp/.write-check succeeds. To enable editing, remove readonly from the workspace bind mount while keeping --read-only on the container filesystem. Check the rootless UID mapping first. Grant the mapped identity access only to the disposable tree, then test whether you can read its output from the host.
For Podman, confirm rootless mode with podman info --format '{{.Host.Security.Rootless}}'. A rootless edit session can use --userns=keep-id:uid=1000,gid=1000 to map your host identity to the container user. On an SELinux host, use -v "$PWD/workspace:/workspace:ro,Z" for inspection or :rw,Z for editing. Relabel only the disposable folder. The Podman run documentation covers these user namespace and volume options.
Save the same baseline in Compose
Put the configuration in docker-compose.yml. The long-form bind mount uses create_host_path: false so a missing source directory causes the launch to fail. See the Docker Compose service reference for the settings below.
services:
agent:
image: local/agent-tools:reviewed
pull_policy: never
user: "1000:1000"
working_dir: /workspace
environment:
HOME: /tmp
read_only: true
cap_drop: [ALL]
security_opt: ["no-new-privileges:true"]
network_mode: none
cpus: 2.0
mem_limit: 4g
pids_limit: 256
tmpfs: ["/tmp:rw,nosuid,nodev,size=128m,mode=1777"]
volumes:
- type: bind
source: ./workspace
target: /workspace
read_only: true
bind:
create_host_path: false
Validate the configuration and repeat the shell checks.
docker --context rootless compose config
docker --context rootless compose run --rm --entrypoint /bin/sh agent
Check that the runtime actually enforces the CPU, memory, and PID caps. Rootless resource limits depend on the host’s cgroup configuration. The caps limit how much an infinite loop or fork bomb can consume, while a job timeout limits how long it can run. After validation, use the reviewed agent command as the entrypoint for an actual task.
Restricting Network Egress, API Keys, and File Permissions
Start offline and allow only what the task needs
Keep --network none for static analysis and tests that already have their dependencies. Bake approved tools into the image so the agent can work offline. If the job needs a package registry or hosted inference, create a separate connected profile.
Attach the agent to an internal network with a trusted, domain-allowlisted HTTP proxy. Give only the proxy an upstream route. Set HTTP_PROXY and HTTPS_PROXY for clients that honor those variables. Enforce the policy at the network boundary so subprocesses cannot make direct connections around the proxy.
- Allow exact destinations and ports. Start with HTTPS to
pypi.organdfiles.pythonhosted.orgfor PyPI, orregistry.npmjs.orgfor npm. Add only the LLM API hostnames that the job requires. - Deny other traffic. Have the proxy resolve allowed names and reject direct IP destinations, private or link-local addresses, and unexpected ports. Block direct outbound connections and unapproved DNS from the agent.
- Test both paths. Verify that a request to an approved destination succeeds through the proxy. Verify that an unapproved request fails and that a direct request fails even without proxy settings.
An approved LLM endpoint can still receive source code in a prompt. A proxy that sees only the destination of an HTTPS tunnel cannot enforce request paths or methods. Use a broker that understands the service API when you need to constrain operations. For dependencies, a read-only package mirror can further narrow what the agent can do.
⚠️ Warning: Indirect prompt injection can start even if you never type a malicious prompt. A compromised webpage, GitHub issue, or third-party package README can contain hidden instructions the agent acts on. Those instructions can trigger shell commands, file changes, or data transfers. Enforce strict egress filtering and require human approval before git push or any destructive action.
Keep credentials and sensitive files outside
Never mount .env, ~/.aws, ~/.config/gcloud, or ~/.ssh into the sandbox. These paths can contain API keys, cached cloud tokens, or private SSH keys. Read-only mounts still expose their contents. Keep SSH_AUTH_SOCK outside, too. A process with access to that socket can ask the host’s SSH agent to authenticate on its behalf.
For hosted inference, have a credential broker hold the provider key and inject it into approved upstream requests. If the client requires a key inside the sandbox, give it a dedicated, short-lived key with the narrowest available scope. Enforce a hard spending cap where the provider supports one, or set a quota at the broker. Revoke the key when the job ends.
Agent Sandbox Comparison Matrix & Security Checklist
| Isolation approach | Kernel isolation | Setup complexity | Performance overhead | Recommended use case |
|---|---|---|---|---|
| Host OS direct execution | Uses the host kernel without a sandbox boundary | Minimal | Minimal | Trusted, supervised commands |
| OS-level sandbox such as bubblewrap or Seatbelt | Shared host kernel | Low with a maintained policy | Minimal | Short tasks that need native tools |
| Hardened rootless container | Shared Linux kernel | Moderate | Low on Linux | Repeatable coding, testing, and automation |
MicroVM sandbox such as Docker sbx or Kata | Separate guest kernel | Moderate with sbx, higher with self-managed Kata | Guest startup and memory overhead | Image builds and workloads that need stronger host isolation |
Run this checklist against a throwaway repository before giving an agent a real task.
- Workspace. Confirm an outside test file is inaccessible. Verify that the workspace permits writes only when you intentionally enable editing.
- Privileges. Run
idand checkCapEff,NoNewPrivs, andSeccompin/proc/self/statuson Linux. Confirm which seccomp profile the runtime applies. - Network and secrets. Verify the configured egress policy and check that credential files, environment variables containing secrets, and host sockets are absent.
- Resources. Confirm the effective memory, CPU, and process limits. Check that the job timeout stops the run.
- Review and recovery. Inspect
git statusandgit diffon the host. Confirm you can discard the workspace without affecting the main checkout.
Conclusion
Run one bounded task in a disposable workspace before handing an agent a real project. Verify what it can read, write, and reach, then review the diff before accepting any changes. Repeat those checks whenever you add a mount, credential, integration, or network destination.