{"slug": "running-an-ai-coding-agent-in-docker-isolation-permissions-safety", "title": "Running an AI coding agent in Docker: isolation, permissions, safety", "summary": "A developer published a recipe for running AI coding agents inside a hardened Docker container, using a non-root user, a read-only root filesystem, dropped Linux capabilities, memory/CPU/PID limits, and no network by default. Secrets are kept in an external .env file rather than baked into the image, and only a single bind-mounted workspace directory is writable. The writeup includes verification steps such as checking that `id` reports uid=1000 and that `touch /test` fails with a read-only filesystem error.", "body_md": "Agents do more than chat. They run shell commands, read files, and hit APIs — and if you start one from your home directory, it inherits your SSH keys, your cloud credentials, and your entire filesystem along with them. I still want a coding agent around; I just don't want it holding the keys to my machine.\n\nThe recipe below puts the agent inside a Docker container (the isolated, disposable box that packages an app together with its dependencies) and then tightens that box: a non-root user, a read-only root filesystem, every Linux capability dropped, and no network unless the job genuinely needs one. It applies to anything that ships a CLI or a Python entry point, on Docker Engine 24+ or Docker Desktop 4.x across Linux, macOS, and Windows with WSL2.\n\n`docker --version`.`.env` file rather than baked into the image.`main.py`, or a package you install with `pip`.` cd`, `ls`, `chmod`.\nStart with a sanity check: `docker run --rm hello-world` should print a message beginning with `Hello from Docker!` and exit with code 0. That proves the daemon is up and your user is allowed to talk to it.\n\nNext, create the project folder and the secrets file:\n\n```\nmkdir -p ~/agent-sandbox/workspace && cd ~/agent-sandbox\nprintf 'LLM_API_KEY=sk-your-key-here\\n' > .env\nchmod 600 .env\n```\n\n`ls -la` should now show `.env` as `-rw-------`. Only your user can read it, and since the file lives outside the image, the key never becomes a permanent build layer.\n\n```\nFROM python:3.12-slim\nRUN useradd -m -u 1000 agent && mkdir /workspace && chown agent /workspace\nWORKDIR /app\nCOPY --chown=agent requirements.txt .\nRUN pip install --no-cache-dir -r requirements.txt\nCOPY --chown=agent . .\nUSER agent\nCMD [\"python\", \"main.py\"]\n```\n\n`USER agent` is the line that matters: the process runs as UID 1000 instead of root, so if it slips out of its own code path it still can't rewrite system files. Also put `.env` and `*.key` into `.dockerignore` so secrets can't be copied in by accident.\n\nThen build it:\n\n```\ndocker build -t my-agent:0.1 .\n```\n\nThe final line should read `Successfully tagged my-agent:0.1`. The tag gives you a fixed name to reference later, and because `pip install` is cached, rebuilds are fast.\n\n```\ndocker run --rm -it --name agent \\\n  --user 1000:1000 \\\n  --read-only \\\n  --tmpfs /tmp:rw,noexec,nosuid,size=64m \\\n  --cap-drop=ALL \\\n  --security-opt no-new-privileges \\\n  --memory=2g --cpus=1.5 --pids-limit=256 \\\n  --network=none \\\n  -v \"$PWD/workspace:/workspace:rw\" \\\n  --env-file .env \\\n  my-agent:0.1\n```\n\nYou should see your agent's prompt or its first log line. What each flag buys you:\n\n`--read-only` makes the container's own filesystem unwritable, so the agent can't modify its code.`--tmpfs /tmp` hands it a small scratch area in RAM that disappears on exit, and `noexec` blocks running binaries from it.`--cap-drop=ALL` removes every Linux capability (a fine-grained root privilege such as `CAP_NET_RAW`), so even a compromised process can't sniff traffic.`--pids-limit=256` stops fork bombs — scripts that spawn processes until the host dies.`--memory` and `--cpus` cap resource use.`--network=none` cuts the container off from the internet entirely.\nDon't take the flags on faith. At the agent's prompt, type `id` and then `touch /test`.\n\n`id` should print `uid=1000(agent) gid=1000`, and `touch /test` should fail with `Read-only file system`. If that write succeeds, your `--read-only` flag didn't apply and something is wrong with the invocation.\n\nNow test that writes land only where you allowed them: `touch /workspace/hello.txt && ls -l /workspace`. The file appears inside, and on the host it shows up in `~/agent-sandbox/workspace`. That's the bind mount working — everything outside that one link stays invisible to the agent.\n\nWhile a task runs, watch it from a second terminal with `docker logs -f agent`, then shut it down. `docker rm -f agent` clears the rest; combined with `--rm` and a fresh image per run, nothing the agent did survives.\n\nIf the agent calls a cloud model, swap `--network=none` for `--network=bridge` and re-run; the API call should return a model response instead of timing out. The default bridge gives outbound access without publishing any ports, so nothing reaches in from outside.\n\nIf you run a local model instead, keep `none` and put both containers on a private user-defined network created with `docker network create agent-net`.\n\nReference material deserves the same treatment. Mount docs read-only:\n\n```\n-v \"$PWD/docs:/workspace/docs:ro\"\n```\n\nThe agent can `cat /workspace/docs/policy.txt`, but `rm` on it fails — and that restriction is enforced by the kernel, not by the agent's good behaviour.\n\n**Permission denied when writing to the mounted folder.** Your container UID (1000) doesn't own the host folder. Check the numeric owner with `ls -ln ~/agent-sandbox/workspace`, align it with `sudo chown -R 1000:1000 ~/agent-sandbox/workspace`, then restart with `--user $(id -u):$(id -g)` so the container matches your host user. If files suddenly appear owned by `root`, you dropped the `--user` flag — put it back.\n\n**Connection refused or `getaddrinfo EAI_AGAIN` after setting `--network=none`.** That's exactly what `none` means: no network stack at all. Decide what the agent actually needs. Cloud model? Switch to `--network=bridge` and keep the key in `.env`. Local model? Keep `none` and join both containers to the private `agent-net` network.\n\n**The container exits immediately with `exec format error` or a missing entrypoint.** Your `CMD` doesn't match the architecture or the installed command. Check with `docker run --rm my-agent:0.1 python --version`; if that fails, the base image or the `pip install` step is broken. Also confirm your `.py` file is copied into `/app` and that `WORKDIR /app` appears before `CMD`.\n\nBefore trusting the setup with anything real:\n\n`.dockerignore` and has mode `600`\n`id`)` touch /test` fails`no-new-privileges` are both present\nOne honest caveat: a container is isolation, not a virtual machine. Containers share the host kernel, so a kernel bug can still be an escape route. Treat Docker as strong friction — and for genuinely hostile code, add rootless mode or user namespaces on top.\n\nThat's the whole idea: an agent with just enough room to do its job and no room to do anything else. Start with `--network=none`, mount one folder, run as UID 1000, and only widen the box when a task fails without it. It takes two extra minutes per run and removes the scenario where your coding assistant quietly reads your SSH keys.\n\nThis article was originally published on AI Release — [https://ai-release.org](https://ai-release.org). More guides like this land in the channel at [https://t.me/ai_release1](https://t.me/ai_release1).", "url": "https://wpnews.pro/news/running-an-ai-coding-agent-in-docker-isolation-permissions-safety", "canonical_source": "https://dev.to/qunirice_verase_1ab0caeba/running-an-ai-coding-agent-in-docker-isolation-permissions-safety-3gbk", "published_at": "2026-11-30 10:09:02+00:00", "updated_at": "2026-11-30 10:14:42.035386+00:00", "lang": "en", "topics": ["ai-agents", "ai-safety", "ai-tools", "developer-tools"], "entities": ["Docker", "Docker Engine", "Docker Desktop", "WSL2", "Python"], "also_reported_by": [], "takeaway": "Run coding agents in a Docker container with --user 1000:1000, --read-only, --cap-drop=ALL, --pids-limit, and --network=none, mounting only a single workspace directory and passing secrets via --env-file so the key never enters an image layer.", "recommendation": {"advice": "Sandbox coding agents in Docker with a non-root user, read-only root filesystem, all capabilities dropped, and no network, then verify with `id` and a failed `touch /test` before trusting the setup.", "playbook": "agent-security", "playbook_url": "https://wpnews.pro/playbooks/agent-security"}, "alternates": {"html": "https://wpnews.pro/news/running-an-ai-coding-agent-in-docker-isolation-permissions-safety", "markdown": "https://wpnews.pro/news/running-an-ai-coding-agent-in-docker-isolation-permissions-safety.md", "text": "https://wpnews.pro/news/running-an-ai-coding-agent-in-docker-isolation-permissions-safety.txt", "jsonld": "https://wpnews.pro/news/running-an-ai-coding-agent-in-docker-isolation-permissions-safety.jsonld"}}