Sub topic 4
Running an agent in a hardened container
This chapter shows how to run a shell for a coding agent in a Docker container with the default privileges removed. It assumes you know the Linux command line, basic Docker (images, containers, volumes), and how Linux users and file permissions work. The chapter before this one covered namespaces, cgroups, capabilities, and seccomp. Here you apply them.
Start from the defaults you want to remove
A plain docker run gives the container more than an agent needs. By default the process runs as root inside the container, the root filesystem is writable, the container keeps a default set of Linux capabilities, and it has network access. Each default widens what a mistaken or manipulated agent can do.
Hardening means removing each of these unless the task requires it.
A hardened shell
The command below starts an interactive shell with the main restrictions in place.
docker run --rm -it \
--user 1000:1000 \
--read-only \
--tmpfs /tmp \
--cap-drop=ALL \
--security-opt no-new-privileges \
--pids-limit 256 \
--memory 2g \
--cpus 2 \
--network none \
-v "$PWD":/workspace \
-w /workspace \
python:3.12-slim bashEach flag has one job:
| Flag | Effect |
|---|---|
| `--user 1000:1000` | Runs as an unprivileged user, not root |
| `--read-only` | Makes the container's root filesystem read-only |
| `--tmpfs /tmp` | Gives the agent a writable scratch space in memory |
| `--cap-drop=ALL` | Removes all Linux capabilities |
| `--security-opt no-new-privileges` | Blocks privilege gain through setuid binaries |
| `--pids-limit 256` | Caps the number of processes, which stops fork bombs |
| `--memory 2g`, `--cpus 2` | Caps memory and CPU use |
| `--network none` | Removes network access |
| `-v "$PWD":/workspace` | Mounts only the current project, writable |
The agent can now change files only in /workspace and /tmp. Everything else in the image is read-only.
The same setup in Compose
If you manage the sandbox with Docker Compose, the same settings look like this. The agent joins an internal network, which has no route to the outside world.
services:
agent:
image: python:3.12-slim
user: "1000:1000"
working_dir: /workspace
command: ["sleep", "infinity"]
read_only: true
tmpfs:
- /tmp
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
pids_limit: 256
mem_limit: 2g
volumes:
- ./project:/workspace
networks:
- sandbox
networks:
sandbox:
internal: trueCalling the sandbox from code
Agent frameworks usually launch the container from code. Pass the command as a list, not as one string. With a list there is no shell parsing, so a crafted argument cannot inject extra commands into the docker call.
import subprocess
from pathlib import Path
def run_in_sandbox(project_dir: str, command: list[str], timeout: int = 60) -> subprocess.CompletedProcess:
project = Path(project_dir).resolve()
docker_cmd = [
"docker", "run", "--rm",
"--user", "1000:1000",
"--read-only",
"--tmpfs", "/tmp",
"--cap-drop=ALL",
"--security-opt", "no-new-privileges",
"--pids-limit", "128",
"--memory", "1g",
"--network", "none",
"-v", f"{project}:/workspace",
"-w", "/workspace",
"python:3.12-slim",
*command,
]
return subprocess.run(docker_cmd, capture_output=True, text=True, timeout=timeout)
result = run_in_sandbox(".", ["python", "-c", "print('hello from the sandbox')"])
print(result.stdout)The timeout argument limits the whole run. Note one gap: if the timeout fires, Python stops waiting and raises TimeoutExpired, but the container may keep running. In production, give the container a name with --name and run docker kill on it in an exception handler.
Common errors
- Permission denied in
/workspace. The mounted files belong to a different user ID than 1000. Match--userto the owner of the project directory. - Tools fail to write a cache or config file. The root filesystem is read-only. Point the cache to
/tmpwith an environment variable, or mount a dedicated writable volume. pip installfails. With--network nonethere is no network. Install dependencies into the image beforehand.
Exercise
An agent in your hardened container needs to run pytest, which writes a .pytest_cache directory to the current directory. The project mount is /workspace. Will this work, and why?
Show answer
Yes. The project mount at /workspace is writable, and the working directory is /workspace, so pytest can create .pytest_cache there. The read-only root filesystem only affects paths outside the mounts and the tmpfs. It works as long as user 1000 can write to the host directory.