Sub topic 2
Limiting CPU, Memory, and Process Count
Assume you are comfortable with Linux commands, Docker images, volumes, users, and file permissions. A coding agent can accidentally create unlimited processes, consume all memory, or run a CPU-heavy build. Resource limits reduce the damage from bugs and runaway commands.
Set resource limits
Docker limits apply to the container, not just the agent process:
docker run --rm -it \
--user 1000:1000 \
--cpus 2 \
--memory 1g \
--pids-limit 128 \
--network none \
--read-only \
--tmpfs /tmp \
--cap-drop=ALL \
--security-opt no-new-privileges \
-v "$PWD/project:/workspace" \
-w /workspace \
python:3.12-slim bash--cpus 2 limits the container to about two CPU cores. --memory 1g limits its memory. Docker can terminate a process when it exceeds this limit. --pids-limit 128 limits the total number of processes and threads.
Choose limits from the workload. A small test runner may need one CPU and 512 MB. A compiler may need more memory. Keep a timeout outside the container too, because resource limits do not guarantee that a command will finish.
Control network egress
Use --network none when the agent does not need the network. This blocks package downloads, remote APIs, and data exfiltration. If downloads are necessary, use a separate allowlisted proxy or an internal network. Do not give the container unrestricted internet access by default.
Keep secrets out of reach
Do not mount your home directory, SSH directory, cloud credential files, or Docker socket. Avoid passing secrets through environment variables unless the specific command needs them. Prefer short-lived credentials scoped to one service and one operation. Remember that a repository may already contain secrets, so scan generated changes before publishing them.
Use disposable sandboxes
Start a fresh container for each task. Mount only a temporary project copy or a narrowly scoped working directory. After the run, inspect the output and review git diff on the host. Copy approved changes back to the real repository, then remove the sandbox.
Exercise
Which Docker options should you use when an agent only edits a project and runs local tests?
Show answer
Use a non-root user, CPU, memory, and process limits, a read-only root filesystem with a temporary /tmp, --network none, dropped capabilities, and only the project directory as a writable mount.