Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 153/ 160
08. Container Orchestration & Ecosystem

Sandboxed Code Execution with Docker (for AI Agents & Online Judges)

Sandbox untrusted code execution for agents and judges using hardened containers.

AdvancedSandbox · Untrusted Code3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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:exec for 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

Loading interactive 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.

Concrete Sandbox setup

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

Key takeaway: Prove Untrusted Code 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, 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.

Deliberate failure test

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.

Key takeaway: Layered Docker hardening suits lower-risk jobs; microVMs/gVisor provide stronger kernel isolation for hostile multi-tenant execution.

How it works, step by step

Step 1 of 4
Design

1. 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.

Treat attacker code as malicious by design: it may fork, fill disk, contact metadata endpoints and try container escape.
Key numbers at this step
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 changed in 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

Explicit Sandbox
Best for: Sandbox untrusted code execution for agents and judges using hardened containers, with a reviewed runbook.

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.

Ambient default
Best for: Short-lived Untrusted Code experiments with low blast radius.

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.

Managed Limits
Best for: Teams that centralize Limits policy across many workloads.

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:exec as proof and skipping run 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:exec address 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?
Interview tip: Give one concrete operation-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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Development Containers (devcontainer.json) & GitHub Codespaces
Next topic
Lightweight MicroVMs & Stronger Isolation (Kata Containers, Firecracker, gVisor)