The 5-Minute Guide: How to Run Claude Code & Cursor Inside a Disposable Docker Sandbox
π Published: November 2026 | ✍️ By MyTechDiary Editorial Team | ⏱️ 8 min read | π·️ Category: Docker, DevSecOps & AI Agents
By the 20th confirmation popup of the hour, nobody reads the prompt anymore—your brain goes on autopilot and clicks "Yes, don't ask again" just to ship code.
In our previous security breakdown, we saw how this exact approval fatigue caused thousands of live AWS, Supabase, and database credentials to leak via .claude/settings.local.json.
The real cure isn't superhuman willpower. It's architectural isolation. If your AI agent lives inside a disposable Docker sandbox, your host ~/.ssh, ~/.aws, and personal files simply do not exist to it. You can let Claude Code, OpenAI Codex, or Cursor Agent run at full speed—and destroy the entire container the second the task finishes.
Here is the exact 5-minute copy-paste setup for both CLI purists (Docker one-liner) and IDE users (VS Code / Cursor Devcontainers).
1. Option A: The 30-Second Disposable CLI Sandbox (Claude Code / Codex)
You don't need Kubernetes or complex VM hypervisors. You can lock Claude Code inside an ephemeral Node.js container that mounts only your current working repository and deletes itself automatically on exit (--rm).
Step 1: Create a Reusable Dockerfile.agent
Save this minimal, hardened Dockerfile anywhere in your home directory (e.g., ~/docker-agent/Dockerfile):
FROM node:22-bookworm-slim
# Install essential CLI tools (git, ripgrep, curl, python3)
RUN apt-get update && apt-get install -y --no-install-recommends \
git ripgrep curl ca-certificates python3 python3-pip \
&& rm -rf /var/lib/apt/lists/*
# Install Claude Code CLI globally
RUN npm install -g @anthropic-ai/claude-code
# Run as non-root user for extra container safety
USER node
WORKDIR /workspace
Step 2: Build Once & Add a Shell Alias
Build the image once, then add a clean ai-sandbox alias to your ~/.zshrc or ~/.bashrc:
# 1. Build the reusable sandbox image
docker build -t claude-sandbox ~/docker-agent
# 2. Add this function to ~/.zshrc or ~/.bashrc
ai-sandbox() {
docker run --rm -it \
--name "agent-$(basename $(pwd))" \
-v "$(pwd):/workspace" \
-e ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY" \
--cap-drop=ALL \
--security-opt=no-new-privileges \
claude-sandbox claude
}
π‘️ Why Those Security Flags Matter:
--rm: Destroys the container filesystem the instant you exit—no lingering temp caches or stray background daemons.-v "$(pwd):/workspace": The agent can only read/write inside your current repo. Your~/.ssh,~/.aws, and browser profiles do not exist.--cap-drop=ALL&--security-opt=no-new-privileges: Strips Linux kernel capabilities so even a rogue script cannot escalate privileges.
Stop Exporting Raw API Keys in Plaintext .zshrc Files
Instead of leaving ANTHROPIC_API_KEY in plain shell history, wrap your sandbox command with 1Password CLI (op run -- ai-sandbox) or spin up an isolated remote Linux Cloud VPS via DigitalOcean so nothing ever touches your primary laptop.
2. Option B: Zero-Friction VS Code & Cursor devcontainer.json
Prefer visual diffs inside Cursor or VS Code? Create a .devcontainer/devcontainer.json file inside your project. When you click "Reopen in Container", your entire editor UI connects seamlessly to the isolated sandbox while keeping host files completely off-limits.
{
"name": "AI Agent Sandbox",
"image": "mcr.microsoft.com/devcontainers/javascript-node:22",
"remoteUser": "node",
"runArgs": [
"--cap-drop=ALL",
"--security-opt=no-new-privileges"
],
"postCreateCommand": "npm install -g @anthropic-ai/claude-code",
"customizations": {
"vscode": {
"extensions": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode"
]
}
}
}
3. Bare-Metal Host vs Docker Sandbox: Risk Breakdown
| Threat Vector | Standard Host Terminal | Disposable Docker Sandbox |
|---|---|---|
Accidental rm -rf |
❌ Can wipe home folder & other repos | ✅ Contained to mounted repo (recoverable via Git) |
~/.ssh & ~/.aws Exposure |
❌ Fully readable by any agent script | ✅ Completely invisible inside container |
| Prompt Approval Fatigue | ❌ High risk when clicking "Don't ask again" | ✅ Safe to grant broad autonomy in workspace |
Ship Faster Without Risking Your Workstation
Combine disposable Docker containers with pre-commit secret scanning and remote Cloud VPS runners for total peace of mind.