Sandbox Execution
Qwen Code supports sandboxed execution environments to isolate command execution and enhance security. This is particularly useful when running untrusted code or working in sensitive environments.Supported Sandbox Runtimes
Qwen Code supports three sandbox backends:Docker
Container-based isolation using Docker Engine.Podman
Rootless container runtime, ideal for environments without Docker daemon privileges.macOS Seatbelt (sandbox-exec)
Native macOS sandboxing without containers. Only available on macOS.Configuration
Environment Variables
Enable Sandbox
Custom Sandbox Image
Additional Configuration
Command Line Options
Settings File Configuration
In.qwen/settings.json:
Docker/Podman Sandbox Details
How It Works
When using Docker or Podman:-
Container Creation: Qwen Code spawns a container with:
- Current working directory mounted
- User settings directory (
~/.qwen) mounted - Temporary directory mounted
- Network access configured
-
Environment Inheritance: The following are passed through:
- API keys (GEMINI_API_KEY, OPENAI_API_KEY, etc.)
- Terminal settings (TERM, COLORTERM)
- Virtual environment paths (VIRTUAL_ENV)
- Google Cloud credentials
-
Lifecycle: Container is automatically removed on exit (
--rmflag)
Volume Mounts
By default, these are mounted:UID/GID Handling
On Linux, Qwen Code automatically matches the host user’s UID/GID to prevent permission issues:macOS Seatbelt Sandbox
Built-in Profiles
Qwen Code includes six built-in seatbelt profiles:Profile Locations
- Built-in:
packages/cli/src/utils/sandbox-macos-{profile}.sb - Custom:
.qwen/sandbox-macos-{profile}.sb(project-specific)
Custom Profile Example
Create.qwen/sandbox-macos-custom.sb:
Seatbelt Variables
Available in.sb files:
TARGET_DIR: Current working directoryTMP_DIR: System temporary directoryHOME_DIR: User home directoryCACHE_DIR: macOS cache directoryINCLUDE_DIR_0throughINCLUDE_DIR_4: Additional workspace directories
Proxy Support
For sandboxed environments with restricted network access:Container-Based Proxy
- Runs on port 8877 by default
- Connected to both host network and internal sandbox network
- Automatically started/stopped with sandbox
macOS Seatbelt Proxy
Custom Sandbox Images
Building Custom Image
Create.qwen/sandbox.Dockerfile:
Build and Use
Startup Scripts
Container Bashrc
Create.qwen/sandbox.bashrc to customize the container environment:
Troubleshooting
Permission Issues (Linux)
Problem: Files created in sandbox have wrong ownership. Solution: Enable UID/GID matching:Image Not Found
Problem:Sandbox image 'qwen-code-sandbox' is missing
Solution: Build the sandbox image:
Network Access Issues
Problem: Container cannot access network. Solution: Check Docker/Podman network configuration:Virtual Environment Issues
Problem: Python venv not working in sandbox. Solution: The sandbox creates a separate venv directory:Port Binding Issues
Problem: Cannot access exposed ports. Solution: Ensure ports are properly exposed:macOS Seatbelt Issues
Problem: Seatbelt denies file access. Solution: Use a more permissive profile or create custom profile:Security Considerations
Container Isolation
- Containers run with
--initflag for proper signal handling - Containers auto-removed on exit (
--rm) - Host Docker socket NOT mounted by default
- Network can be restricted using internal networks + proxy
Seatbelt Isolation
- File system access restricted to defined paths
- Network access can be blocked in restrictive profiles
- Process execution can be limited
Best Practices
- Use restrictive profiles in production
- Mount only necessary paths with appropriate permissions (ro/rw)
- Review custom Dockerfiles before building
- Use proxy mode for network filtering
- Keep sandbox images updated with security patches
Integration Tests
For testing with sandbox enabled:- Uses unique container names with random suffixes
- Supports checkpoint/resume functionality
- Handles temporary file cleanup
Advanced Topics
Multiple Workspaces
Mount additional workspace directories:Custom Entrypoint
Override container entrypoint:Resource Limits
Debug Mode
Enable debugging:Source Code References
- Sandbox implementation:
packages/cli/src/utils/sandbox.ts - Sandbox config:
packages/cli/src/config/sandboxConfig.ts - Seatbelt profiles:
packages/cli/src/utils/sandbox-macos-*.sb
