Sandboxed Code Execution with Docker (for AI Agents & Online Judges)
Sandbox untrusted code execution for agents and judges using hardened containers.
In plain words
Treat attacker code as malicious by design: it may fork, fill disk, contact metadata endpoints and try container escape.
Think of it like…
A sandbox is a sealed exam booth with no network, a shredder, a timer and one blank notepad; the candidate receives only the assignment and loses the room after submission.
Where you’ve seen it
In practice, docker run --rm --network none --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m --pids-limit 64 --memory 256m --cpus .5 --security-opt no-new-privileges -u 65534 judge:exec is the concrete move for this job: sandbox untrusted code execution for agents and judges using hardened containers. The release is not accepted until run a fork bomb, 1 GiB allocation, outbound curl and /proc scan under timeout, verify each is blocked/denied and no host files changed confirms the result.
Words you’ll hear
- Sandbox
- The implementation control in
docker run --rm --network none --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m --pids-limit 64 --memory 256m --cpus .5 --security-opt no-new-privileges -u 65534 judge:execfor sandbox untrusted code execution for agents and judges using hardened containers. - Untrusted Code
- The result to observe with
run a fork bomb, 1 GiB allocation, outbound curl and /proc scan under timeout, verify each is blocked/denied and no host files changed, rather than infer from a successful command alone. - Limits
- The production boundary exposed when reusing a named volume for source/output without cleaning ownership, or promising secure multi-tenant isolation on an ordinary shared-kernel Docker daemon.
Visual diagram
Theory & deep dive
Sandbox control path
The concrete mechanism is docker run --rm --network none --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m --pids-limit 64 --memory 256m --cpus .5 --security-opt no-new-privileges -u 65534 judge:exec. The observable result is run a fork bomb, 1 GiB allocation, outbound curl and /proc scan under timeout, verify each is blocked/denied and no host files changed; if those disagree, inspect the runtime evidence before changing unrelated settings.
Sandbox untrusted code execution for agents and judges using hardened containers. Keep Sandbox, Untrusted Code and Limits in the same deployment record so the intent and the observed state cannot drift apart.
docker run --rm --network none --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m --pids-limit 64 --memory 256m --cpus .5 --security-opt no-new-privileges -u 65534 judge:exec
run a fork bomb, 1 GiB allocation, outbound curl and /proc scan under timeout, verify each is blocked/denied and no host files changed, not with command success alone.Limits failure boundary
A normal Docker container shares the host kernel, so rootless/seccomp/no-new-privileges are defenses, not proof. Mounting a writable judge directory lets code poison later runs, and no-network plus a writable Docker socket still gives a host-control channel. The narrow failure to design for is reusing a named volume for source/output without cleaning ownership, or promising secure multi-tenant isolation on an ordinary shared-kernel Docker daemon.
Accept this compromise explicitly: Layered Docker hardening suits lower-risk jobs; microVMs/gVisor provide stronger kernel isolation for hostile multi-tenant execution. The controlled check-run a fork bomb, 1 GiB allocation, outbound curl and /proc scan under timeout, verify each is blocked/denied and no host files changed-also identifies the point where a different design is safer.
Exercise this failure safely: reusing a named volume for source/output without cleaning ownership, or promising secure multi-tenant isolation on an ordinary shared-kernel Docker daemon.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: A normal Docker container shares the host kernel, so rootless/seccomp/no-new-privileges are defenses, not proof. Mounting a writable judge directory lets code poison later runs, and no-network plus a writable Docker socket still gives a host-control channel.
- Intent
- Sandbox
When to use it & trade-offs
What it is
A code-execution sandbox needs a disposable root filesystem, short-lived network policy, no daemon socket, dropped privileges/capabilities, strict CPU/PID/memory quotas, read-only mounts and a timeout. It should be isolated per untrusted submission and collect only bounded output.
Why it matters: A normal Docker container shares the host kernel, so rootless/seccomp/no-new-privileges are defenses, not proof. Mounting a writable judge directory lets code poison later runs, and no-network plus a writable Docker socket still gives a host-control channel.
When to reach for it
- The fit is strongest for this job: sandbox untrusted code execution for agents and judges using hardened containers. Pair it with
run a fork bomb, 1 GiB allocation, outbound curl and /proc scan under timeout, verify each is blocked/denied and no host files changedin the runbook. - Production Sandbox changes should make Untrusted Code observable through
docker run --rm --network none --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m --pids-limit 64 --memory 256m --cpus .5 --security-opt no-new-privileges -u 65534 judge:exec, rather than trusting the runtime default. - Keep a rollback example for the case where reusing a named volume for source/output without cleaning ownership, or promising secure multi-tenant isolation on an ordinary shared-kernel Docker daemon.
When to avoid or be careful
- Do not accept command success while the system can still fail by reusing a named volume for source/output without cleaning ownership, or promising secure multi-tenant isolation on an ordinary shared-kernel Docker daemon.
- Avoid erasing Limits protection merely to dodge this cost: “Layered Docker hardening suits lower-risk jobs; microVMs/gVisor provide stronger kernel isolation for hostile multi-tenant execution”.
- Do not replace the evidence path-
run a fork bomb, 1 GiB allocation, outbound curl and /proc scan under timeout, verify each is blocked/denied and no host files changed-with a screenshot or a mutable tag.
docker run --rm --network none --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m --pids-limit 64 --memory 256m --cpus .5 --security-opt no-new-privileges -u 65534 judge:exec: practical choices
Strengths: A code-execution sandbox needs a disposable root filesystem, short-lived network policy, no daemon socket, dropped privileges/capabilities, strict CPU/PID/memory quotas, read-only mounts and a timeout. It should be isolated per untrusted submission and collect only bounded output.
Watch out: Layered Docker hardening suits lower-risk jobs; microVMs/gVisor provide stronger kernel isolation for hostile multi-tenant execution.
Strengths: It can begin with docker run --rm --network none --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m --pids-limit 64 --memory 256m --cpus .5 --security-opt no-new-privileges -u 65534 judge:exec and fewer surrounding controls.
Watch out: It silently accepts reusing a named volume for source/output without cleaning ownership, or promising secure multi-tenant isolation on an ordinary shared-kernel Docker daemon, which makes the result hard to reproduce after promotion.
Strengths: A normal Docker container shares the host kernel, so rootless/seccomp/no-new-privileges are defenses, not proof. Mounting a writable judge directory lets code poison later runs, and no-network plus a writable Docker socket still gives a host-control channel.
Watch out: The platform adds an ownership and debugging layer; retain run a fork bomb, 1 GiB allocation, outbound curl and /proc scan under timeout, verify each is blocked/denied and no host files changed as the direct check.
Interview signals & numbers to know
When this topic comes up
- docker run --rm --network none --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m --pids-limit 64 --memory 256m --cpus .5 --security-opt no-new-privileges -u 65534 judge:exec
- run a fork bomb, 1 GiB allocation, outbound curl and /proc scan under timeout, verify each is blocked/denied and no host files changed
- Failure signature: reusing a named volume for source/output without cleaning ownership, or promising secure multi-tenant isolation on an ordinary shared-kernel Docker daemon
- Trade-off: Layered Docker hardening suits lower-risk jobs; microVMs/gVisor provide stronger kernel isolation for hostile multi-tenant execution.
Benchmarks & rules of thumb
- 64 PIDs
- bounded task count
- 256 MiB
- hard memory quota
- 0 network routes
- for the execution container
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
docker run --rm --network none --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m --pids-limit 64 --memory 256m --cpus .5 --security-opt no-new-privileges -u 65534 judge:execas proof and skippingrun a fork bomb, 1 GiB allocation, outbound curl and /proc scan under timeout, verify each is blocked/denied and no host files changed. - Reusing a named volume for source/output without cleaning ownership, or promising secure multi-tenant isolation on an ordinary shared-kernel Docker daemon
- Optimizing Sandbox without accepting that layered Docker hardening suits lower-risk jobs; microVMs/gVisor provide stronger kernel isolation for hostile multi-tenant execution.
Likely interviewer follow-ups
- Why does
docker run --rm --network none --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m --pids-limit 64 --memory 256m --cpus .5 --security-opt no-new-privileges -u 65534 judge:execaddress Sandbox here rather than through application code? - How would you distinguish healthy Untrusted Code from the failure “reusing a named volume for source/output without cleaning ownership, or promising secure multi-tenant isolation on an ordinary shared-kernel Docker daemon” using
run a fork bomb, 1 GiB allocation, outbound curl and /proc scan under timeout, verify each is blocked/denied and no host files changed? - When would this trade-off-“Layered Docker hardening suits lower-risk jobs; microVMs/gVisor provide stronger kernel isolation for hostile multi-tenant execution”-justify a different Limits design?
docker run --rm --network none --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m --pids-limit 64 --memory 256m --cpus .5 --security-opt no-new-privileges -u 65534 judge:exec-and one proof-run a fork bomb, 1 GiB allocation, outbound curl and /proc scan under timeout, verify each is blocked/denied and no host files changed. Then name the boundary: reusing a named volume for source/output without cleaning ownership, or promising secure multi-tenant isolation on an ordinary shared-kernel Docker daemon.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Sandboxed Code Execution with Docker (for AI Agents & Online Judges) with generative AI interview Q&A and hands-on practice.