Docker Sandboxes Tutorial and Cheatsheet
Docker Sandboxes lets AI coding agents like Claude Code run safely in isolated containers. Get full autonomy without compromising your localhost security. Docker Desktop 4.50+
AI coding agents have become incredibly powerful. Tools like Claude Code, GitHub Copilot, and Devin AI can write code, debug issues, and even manage entire development workflows. But there's a critical problem: running them locally introduces significant risks.
The Three Horsemen of AI Agent Risks

Environment Pollution
AI agents can install packages and dependencies globally, creating conflicts with other projects. Imagine your agent installing a different version of Python or Node.js that breaks your existing applications.
# Agent installs globally
npm install -g some-package@beta
# Now your other projects using stable versions are brokenUnintended File System Changes
An agent could mistakenly modify, move, or delete critical files outside the project workspace. One wrong command and your ~/.ssh keys, environment configs, or system files could be compromised.
Recent research from NVIDIA's AI Red Team (CVE-2024-12366) demonstrated how AI-generated code can escalate into remote code execution (RCE) when executed without proper isolation.
Security Vulnerabilities
Giving an agent unrestricted network and file access could expose sensitive data or create security holes. According to a comprehensive survey by ACM Computing Surveys, insufficient isolation between agents and the host system poses one of the most significant security challenges in agentic AI systems.
The uncomfortable truth: Most LLM tools have full access to your machine by default, with only imperfect attempts at blocking risky behavior.
The Solution: Isolated Yet Familiar
Docker Sandboxes solves these problems by isolating AI agents from your local machine while preserving a familiar development experience

Three Core Principles
π‘οΈ Security & Isolation
Protect your host machine from unintended changes, dependency conflicts, and vulnerabilities. The agent runs in a containerized environment with controlled access.
π― High-Fidelity Dev Experience
Your project directory is mounted at the same absolute path inside the container. File paths in error messages match your host, scripts with hard-coded paths work as expected, and changes are reflected instantly.
# On your Mac/Linux host
/Users/alice/projects/myapp
# Inside the sandbox
/Users/alice/projects/myapp # Exact same path!βοΈ Powerful & Customizable
Tailor the environment with variables, Docker socket access (with caution), volume mounts, and custom templates for any workflow.
Getting Started in 60 Seconds
Prerequisites
- Docker Desktop 4.50 or later (Download)
- A Claude Code subscription (or other supported AI agent)
Run Your First Sandboxed Agent
# Navigate to your project
cd ~/my-project
# Start the sandbox
docker sandbox run claudeThat's it! On first run, you'll be prompted to authenticate. Credentials are securely stored in a persistent Docker volume for future sessions.
What Just Happened?
The docker sandbox run command automated several key steps:
- Container Creation: Created from
docker/sandbox-templates:claude-code - Workspace Mounting: Your current directory mounted at the exact same path
- Git Configuration: Your host's Git
user.nameanduser.emailinjected automatically - Persistent Credentials: API key stored in
docker-claude-sandbox-datavolume
Under the Hood: How It Works
The Anatomy of a Sandbox

βββββββββββββββββββββββββββββββββββββββββββ
β Host Machine (Protected) β
β β
β ββββββββββββββββββββββββββββββββββββββ β
β β Sandbox Container (Isolated) β β
β β β β
β β ββββββββββββββββββββββββββββββββ β β
β β β AI Agent (Claude Code) β β β
β β ββββββββββββββββββββββββββββββββ β β
β β β Mounted β β
β β ββββββββββββββββββββββββββββββββ β β
β β β Project Workspace βββΌββΌββ
β β β /Users/dev/project β β β β
β β ββββββββββββββββββββββββββββββββ β β β
β ββββββββββββββββββββββββββββββββββββββ β β
β β β
β ββββββββββββββββββββββββββββββββββββββ β β
β β Your Actual Project Files β β β
β β /Users/dev/project βββ β
β ββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββOne Sandbox Per Workspace

Docker enforces one sandbox per workspace. Running docker sandbox run again in the same directory reuses the existing container. This means:
- Installed packages persist across sessions
- Environment changes are maintained
- Temporary files remain between runs
Important: To modify a sandbox's configuration, you must remove and recreate it.
Advanced Configuration

Managing Your Sandboxes
# List all running sandboxes
docker sandbox ls
# Inspect a sandbox's configuration (JSON output)
docker sandbox inspect <sandbox-id>
# Remove a specific sandbox
docker sandbox rm <sandbox-id>
# Pro Tip: Remove all sandboxes at once
docker sandbox rm $(docker sandbox ls -q)Environment Variables
Use the -e flag to pass environment variables directly into the sandbox.
Example: Full Development Environment Setup
docker sandbox run \
-e NODE_ENV=development \
-e DATABASE_URL=postgresql://localhost/myapp_dev \
-e DEBUG=true \
claudeExample: API Keys for Testing
docker sandbox run -e STRIPE_TEST_KEY=sk_test_xxx claudeβ οΈ Caution: Only use test or development API keys in sandboxes. Never expose production keys.
Volume Mounts
Use the -v flag to mount host directories into the sandbox. Syntax: host-path:container-path[:ro]
Example: Machine Learning Workflow
docker sandbox run \
-v ~/datasets:/data:ro \
-v ~/models:/models \
-v ~/.cache/pip:/root/.cache/pip \
claudeThis provides:
- Read-only access to datasets (prevents accidental modifications)
- Read-write access to save trained models
- Persistent pip cache for faster package installs
Custom Templates

Instead of installing tools every time, build a custom Docker image with everything pre-installed.
Step 1: Create a Dockerfile
# syntax=docker/dockerfile:1
FROM docker/sandbox-templates:claude-code
# Install the 'ruff' linter using 'uv'
RUN curl -LsSf https://astral.sh/uv/install.sh | sh && \
. ~/.local/bin/env && \
uv tool install ruff@latestStep 2: Build and Run
# Build your custom template image
docker build -t my-python-env .
# Run the agent using your new template
docker sandbox run --template my-python-env claudeSecurity Considerations
Docker Socket Access (Use With Extreme Caution)
The --mount-docker-socket flag gives the agent full access to your Docker daemon.

docker sandbox run --mount-docker-socket claudeβ οΈ SECURITY WARNING
Mounting the Docker socket grants the agent root-level privileges on your system.
- Can start/stop any container
- Access volumes and networks
- Potentially escape the sandbox
Only use this option when you fully trust the code the agent is working with.
When It's Useful
- Building images from a Dockerfile
- Running multi-container applications with Docker Compose
- Testing and validating containerized applications

Authentication Strategies
--credentials=sandbox (Default)
Securely stores your API key in a managed Docker volume for reuse across sandboxes.
docker sandbox run claude # Uses sandbox mode by default--credentials=none
No automatic credential management. You must authenticate manually inside the container for each new sandbox.
docker sandbox run --credentials=none claudeBest Practices
Based on research from Martin Fowler's team and NVIDIA's AI security guidelines:
- Least Privilege: Start with read-only access for AI agents
- Never store production credentials in files accessible to agents
- Use temporary tokens with limited scopes
- Review all AI-generated code before committing
- Limit Docker socket access to trusted workflows only
- Monitor resource usage to detect anomalies
Real-World Use Cases
1. Machine Learning Development
docker sandbox run \
-v ~/datasets:/data:ro \
-v ~/models:/models \
-v ~/.cache/pip:/root/.cache/pip \
claudeScenario: Train a model on the MNIST dataset
- Agent: "Train a model on the MNIST dataset and save it to /models"
- Claude:
*runs* python train.py --data /data/mnist --output /models/mnist_model.h5
2. Multi-Container Application Development
docker sandbox run --mount-docker-socket claudeScenario: Build and test a microservices application
- Agent can build Docker images
- Run Docker Compose setups
- Test containerized applications
3. Frontend Development with Hot Reload
docker sandbox run \
-e NODE_ENV=development \
-v ~/.cache/go-build:/root/.cache/go-build \
claude4. Continue Previous Work
# Continue the last conversation
docker sandbox run claude --continue5. Start with a Specific Task
docker sandbox run claude "Add error handling to the login function"Troubleshooting
Common Issues and Solutions
| Problem | Solution |
|---|---|
'sandbox' is not a docker command | Verify ~/.docker/cli-plugins/docker-sandbox exists and is executable. Restart Docker Desktop. |
| Experimental Features not enabled | Your organization uses Settings Management. Ask admin to set "allowBetaFeatures": { "value": true } |
Authentication failure or API key errors | Remove credential volume: docker volume rm docker-claude-sandbox-data, then re-authenticate |
Permission denied accessing workspace files | Ensure project directory is in Docker Desktop β Settings β Resources β File Sharing |
When to Recreate a Sandbox
You must remove and recreate a sandbox when changing:
- Environment variables (
-e) - Volume mounts (
-v) - Docker socket access (
--mount-docker-socket) - Credentials mode (
--credentials)
# Get the sandbox ID
docker sandbox ls
# Remove it
docker sandbox rm <sandbox-id>
# Recreate with new configuration
docker sandbox run -e NEW_VAR=value claudeThe Future of AI Agent Security
Docker Sandboxes represents a critical step forward in making AI agents both powerful and safe. As recent vulnerabilities in tools like OpenAI Codex CLI (CVE-2025-61260) demonstrate, the security of AI coding assistants is an evolving challenge.
Industry Trends
- Containerization as Standard: Leading platforms are adopting container-based isolation (E2B Sandboxes, Daytona SDK)
- MCP Integration: Model Context Protocol enables secure, controlled access to external tools
- Zero Trust Architecture: Assuming all AI-generated code is untrusted by default
- Ephemeral Runtimes: Destroying execution environments after each task
What's Next?
Docker is actively developing:
- Enhanced sandbox templates for different tech stacks
- Better integration with CI/CD pipelines
- Advanced security policies and guardrails
- Cross-platform support improvements
Conclusion
Docker Sandboxes solves the fundamental tension between AI agent autonomy and system security. By providing true isolation with zero-overhead development experience, it enables developers to harness the full power of AI coding assistants without compromising their machines.
The three principles that make it work:
- Security through isolation - Containers protect your host
- Familiarity through path mounting - Same paths, same workflows
- Power through customization - Adapt to any use case
As AI agents become more sophisticated and autonomous, proper sandboxing isn't optionalβit's essential. Docker Sandboxes makes it practical.