Sub topic 5
Giving the agent only the files it needs
This chapter assumes you know Docker bind mounts, Linux file permissions, and the hardened docker run flags from the previous chapter. Here you decide which host files the container can see, and whether it can change them.
Why the mount is the boundary
A hardened container still has full access to everything you mount into it. If you mount your home directory, the agent can read ~/.ssh and ~/.aws and change your shell startup files. The filesystem scope is therefore one of the most important settings in the sandbox.
The rule is to mount the smallest directory the task needs, and make as much of it read-only as you can.
Mount only the project
Mount the project directory, not its parent and not $HOME. Use an absolute path, and resolve symlinks first so you know what you are exposing.
docker run --rm -it \
--user 1000:1000 \
--read-only \
--tmpfs /tmp \
--cap-drop=ALL \
--security-opt no-new-privileges \
--network none \
-v "$PWD":/workspace \
-v "$PWD/.git":/workspace/.git:ro \
-v /dev/null:/workspace/.env:ro \
-w /workspace \
python:3.12-slim bashThe three mounts do different jobs:
- The first mount gives the agent a writable copy of the project's working files.
- The
:roflag on.gitlets the agent read history but not rewrite it or edit hooks. - Mounting
/dev/nullover.envmakes the file appear empty inside the container, so the agent never sees its contents.
If .env does not exist on the host, Docker creates an empty one in your project as a mount point. Add it to .gitignore or create it first.
Read-only by default
The --read-only flag makes the container's own filesystem read-only, so the agent cannot modify system binaries or install packages. Give it writable space only where needed:
| Need | Mechanism |
|---|---|
| Scratch files | `--tmpfs /tmp` (memory-backed, lost on exit) |
| Edits to the project | Writable bind mount of the project directory |
| Build output only | Read-only source mount plus a separate writable `out` directory |
If the agent only has to analyze code, mount the whole project with :ro and give it a single writable output directory.
Things never to mount
/var/run/docker.sock. Access to the Docker socket lets the agent start a new privileged container, which defeats every other control.$HOME,~/.ssh,~/.aws,~/.config, and browser profile directories./,/etc, or/procfrom the host.
Common errors
- Permission denied on writes. The
--user 1000:1000flag must match the owner of the project files on the host. Check withid -uandls -ln. - Files owned by root after a run. This happens when you omit
--user. The agent then writes as root and you must fix ownership. - Tools fail writing to
$HOME. With a read-only root, caches such as~/.cachefail. Set-e HOME=/tmpor mount a tmpfs. - Symlinks pointing outside the project. A symlink inside the mount that points to
/home/you/.sshresolves inside the container, so it does not expose host files. It can still mislead you into thinking a path is safe. Review the project for links when you did not create it.
Exercise
You run an agent with -v "$HOME":/workspace and --cap-drop=ALL. Name two things that can still go wrong, and how you would change the mount.
Show answer
The agent can read credentials such as ~/.ssh and ~/.aws, and it can modify shell startup files like ~/.bashrc that run on your host later. Capability dropping does not limit file access. Mount only the project directory, for example -v "$PWD":/workspace, and use :ro mounts or /dev/null overlays for sensitive files such as .git and .env.