Skip to content

Commit d231b5e

Browse files
Add documentation for lean and Python image variants
Sync documentation for sandbox-sdk PR #259 which introduces optimized Docker image variants. Changes: - Add image variant documentation to Dockerfile reference - Update getting started guide with image selection guidance - Add Python runtime requirements to code execution guide - Document PYTHON_NOT_AVAILABLE error in interpreter API - Add changelog entry for breaking change Breaking change: Default image no longer includes Python. Users must explicitly use the -python image variant for Python code execution. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 12357ec commit d231b5e

File tree

5 files changed

+178
-28
lines changed

5 files changed

+178
-28
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: Sandbox SDK adds lean and Python image variants
3+
description: Docker image size reduced by up to 62% with new lean and Python image variants. Breaking change for Python users.
4+
products:
5+
- agents
6+
- workers
7+
date: 2025-11-27
8+
---
9+
10+
The Sandbox SDK now offers two optimized Docker image variants to improve performance and reduce resource usage.
11+
12+
## Breaking change
13+
14+
**The default image no longer includes Python.** If you use Python code execution, you must update your Dockerfile.
15+
16+
## New image variants
17+
18+
### Default image (lean)
19+
20+
The new default image is optimized for JavaScript, TypeScript, and shell workloads:
21+
22+
- **Size**: ~600-800 MB (62% smaller than previous default)
23+
- **Includes**: Node.js, Bun, Git, system utilities
24+
- **Use for**: JavaScript/TypeScript execution, shell scripts, Git workflows
25+
26+
```dockerfile
27+
FROM docker.io/cloudflare/sandbox:0.5.6
28+
```
29+
30+
### Python image
31+
32+
The Python image includes everything from the default image plus Python runtime and data science packages:
33+
34+
- **Size**: ~1.3 GB (26% smaller than previous default)
35+
- **Includes**: Python 3.11, matplotlib, numpy, pandas, ipython
36+
- **Use for**: Python code execution, data analysis, AI/ML workflows
37+
38+
```dockerfile
39+
FROM docker.io/cloudflare/sandbox:0.5.6-python
40+
```
41+
42+
## Migration guide
43+
44+
If your application uses any of these features, update your Dockerfile to use the Python image:
45+
46+
- `CodeInterpreter.runCode()` with Python
47+
- Python scripts via `exec()` or `startProcess()`
48+
- Python-based tools or workflows
49+
50+
### Before (version 0.5.5 and earlier)
51+
52+
```dockerfile
53+
FROM docker.io/cloudflare/sandbox:0.5.5
54+
```
55+
56+
### After (version 0.5.6+)
57+
58+
```dockerfile
59+
# For Python support
60+
FROM docker.io/cloudflare/sandbox:0.5.6-python
61+
62+
# Or for JavaScript/shell only
63+
FROM docker.io/cloudflare/sandbox:0.5.6
64+
```
65+
66+
## Error handling
67+
68+
If you attempt to execute Python code without the Python image, you will receive a clear error:
69+
70+
- **Error code**: `PYTHON_NOT_AVAILABLE`
71+
- **HTTP status**: 501 Not Implemented
72+
- **Message**: Python is not available in this container
73+
74+
The error message includes instructions to update your Dockerfile to the Python image variant.
75+
76+
## Benefits
77+
78+
- **Faster cold starts**: Smaller images load and start faster
79+
- **Lower resource usage**: Reduced memory footprint for non-Python workloads
80+
- **Better optimization**: Each variant is optimized for its specific use case
81+
- **Cost savings**: Smaller images reduce storage and bandwidth costs
82+
83+
## Learn more
84+
85+
- [Dockerfile reference](/sandbox/configuration/dockerfile/) - Complete guide to image variants
86+
- [Code Interpreter API](/sandbox/api/interpreter/) - Python code execution documentation
87+
- [Getting started](/sandbox/get-started/) - Updated quickstart guide

src/content/docs/sandbox/api/interpreter.mdx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ import { TypeScriptExample } from "~/components";
99

1010
Execute Python, JavaScript, and TypeScript code with support for data visualizations, tables, and rich output formats. Contexts maintain state (variables, imports, functions) across executions.
1111

12+
:::caution[Python runtime requirements]
13+
To execute Python code, your container must use the Python image variant:
14+
15+
```dockerfile
16+
FROM docker.io/cloudflare/sandbox:0.5.6-python
17+
```
18+
19+
The default image does not include Python. Attempting to create a Python context or run Python code without the Python image will return a `PYTHON_NOT_AVAILABLE` error with HTTP 501 status.
20+
21+
See [Dockerfile reference](/sandbox/configuration/dockerfile/) for details.
22+
:::
23+
1224
## Methods
1325

1426
### `createCodeContext()`
@@ -114,13 +126,24 @@ console.log(result.results[0].text); // "15"
114126

115127
<TypeScriptExample>
116128
```
129+
// Runtime errors in code execution
117130
const result = await sandbox.runCode('x = 1 / 0', { language: 'python' });
118131
119132
if (result.error) {
120133
console.error(result.error.name); // "ZeroDivisionError"
121134
console.error(result.error.value); // "division by zero"
122135
console.error(result.error.traceback); // Stack trace array
123136
}
137+
138+
// Python not available error
139+
try {
140+
const ctx = await sandbox.createCodeContext({ language: 'python' });
141+
} catch (error) {
142+
// Error: Python is not available in this container
143+
// Solution: Update Dockerfile to use Python image variant
144+
// FROM docker.io/cloudflare/sandbox:0.5.6-python
145+
console.error(error.message);
146+
}
124147
```
125148
</TypeScriptExample>
126149

src/content/docs/sandbox/configuration/dockerfile.mdx

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ Customize the sandbox container image with your own packages, tools, and configu
1111

1212
The Sandbox SDK provides two image variants optimized for different use cases:
1313

14-
### Default (lean) image
14+
### Default image (lean)
1515

16-
Optimized for shell commands and JavaScript/TypeScript execution (~600-800MB):
16+
Optimized for shell scripts, JavaScript, TypeScript, and Node.js workloads (~600-800 MB):
1717

1818
```dockerfile
1919
FROM docker.io/cloudflare/sandbox:0.5.6
@@ -28,7 +28,7 @@ FROM docker.io/cloudflare/sandbox:0.5.6
2828

2929
### Python image
3030

31-
Full image with Python and data science packages (~1.3GB):
31+
Full-featured image with Python and data science packages (~1.3 GB):
3232

3333
```dockerfile
3434
FROM docker.io/cloudflare/sandbox:0.5.6-python
@@ -39,26 +39,52 @@ FROM docker.io/cloudflare/sandbox:0.5.6-python
3939
- Python 3.11.14 with pip and venv
4040
- Pre-installed Python packages: matplotlib, numpy, pandas, ipython
4141

42-
:::note[Choosing the right variant]
43-
Use the default (lean) image for shell operations, JavaScript/TypeScript execution, and Git workflows. Only use the `-python` variant if you need Python code execution with [`CodeInterpreter.runCode()`](/sandbox/api/interpreter/).
42+
:::caution[Breaking change: Python now requires explicit image selection]
43+
**Starting in version 0.5.6**, the default image no longer includes Python. If your code uses:
4444

45-
Using the lean image reduces image size by ~500MB and improves cold start times.
45+
- `CodeInterpreter.runCode()` with Python
46+
- Python scripts via `exec()` or `startProcess()`
47+
- Any Python-based workflows
48+
49+
You **must** update your Dockerfile to use the `-python` variant:
50+
51+
```dockerfile
52+
# Before (0.5.5 and earlier)
53+
FROM docker.io/cloudflare/sandbox:0.5.5
54+
55+
# After (0.5.6+)
56+
FROM docker.io/cloudflare/sandbox:0.5.6-python
57+
```
58+
59+
Without this change, Python execution will fail with `PYTHON_NOT_AVAILABLE` error.
4660
:::
4761

48-
:::caution[Version synchronization required]
49-
Always match the Docker image version to your npm package version. If you're using `@cloudflare/sandbox@0.5.6`, use `docker.io/cloudflare/sandbox:0.5.6` (or `0.5.6-python`) as your base image.
62+
:::note[Version synchronization required]
63+
Always match the Docker image version to your npm package version. If you are using `@cloudflare/sandbox@0.5.6`, use `docker.io/cloudflare/sandbox:0.5.6` (or `0.5.6-python`) as your base image.
5064

51-
**Why this matters**: The SDK automatically checks version compatibility on startup. Mismatched versions can cause features to break or behave unexpectedly. If versions don't match, you'll see warnings in your logs.
65+
**Why this matters**: The SDK automatically checks version compatibility on startup. Mismatched versions can cause features to break or behave unexpectedly. If versions do not match, you will see warnings in your logs.
5266

5367
See [Version compatibility](/sandbox/concepts/sandboxes/#version-compatibility) for troubleshooting version mismatch warnings.
5468
:::
5569

70+
### Choosing an image variant
71+
72+
Use the **default image** if you:
73+
- Execute shell scripts, JavaScript, or TypeScript only
74+
- Want faster cold starts and lower resource usage
75+
- Do not need Python or Python packages
76+
77+
Use the **Python image** if you:
78+
- Execute Python code via `CodeInterpreter`
79+
- Use Python scripts or tools
80+
- Need data science packages (pandas, numpy, matplotlib)
81+
5682
## Creating a custom image
5783

5884
Create a `Dockerfile` in your project root:
5985

6086
```dockerfile title="Dockerfile"
61-
# Use Python variant if you need Python packages
87+
# Choose the appropriate base image
6288
FROM docker.io/cloudflare/sandbox:0.5.6-python
6389

6490
# Install additional Python packages
@@ -97,7 +123,6 @@ When you run `wrangler dev` or `wrangler deploy`, Wrangler automatically builds
97123
Run services automatically when the container starts by creating a custom startup script:
98124

99125
```dockerfile title="Dockerfile"
100-
# Choose appropriate variant (default or -python)
101126
FROM docker.io/cloudflare/sandbox:0.5.6
102127

103128
COPY my-app.js /workspace/my-app.js
@@ -131,22 +156,9 @@ node /workspace/api-server.js &
131156
exec bun /container-server/dist/index.js
132157
```
133158

134-
## Migration guide
135-
136-
If you are upgrading from a version prior to 0.5.6 and use Python code execution, update your Dockerfile to use the `-python` variant:
137-
138-
```diff title="Dockerfile"
139-
- FROM docker.io/cloudflare/sandbox:0.5.5
140-
+ FROM docker.io/cloudflare/sandbox:0.5.6-python
141-
```
142-
143-
**Why this change was made**: Separating Python into its own image variant reduces the default image size by ~500MB, improving cold start times for workloads that do not require Python.
144-
145-
If you attempt to execute Python code without the `-python` variant, you will receive a `PYTHON_NOT_AVAILABLE` error with instructions to update your Dockerfile.
146-
147159
## Related resources
148160

149-
- [Image Management](/containers/platform-details/image-management/) - Building and pushing images to Cloudflare's registry
161+
- [Image Management](/containers/platform-details/image-management/) - Building and pushing images to Cloudflare\'s registry
150162
- [Wrangler configuration](/sandbox/configuration/wrangler/) - Using custom images in wrangler.jsonc
151163
- [Docker documentation](https://docs.docker.com/reference/dockerfile/) - Complete Dockerfile syntax
152164
- [Container concepts](/sandbox/concepts/containers/) - Understanding the runtime environment

src/content/docs/sandbox/get-started.mdx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,26 @@ This creates a `my-sandbox` directory with everything you need:
3737

3838
- `src/index.ts` - Worker with sandbox integration
3939
- `wrangler.jsonc` - Configuration for Workers and Containers
40-
- `Dockerfile` - Container environment definition
40+
- `Dockerfile` - Container environment definition (includes Python by default)
4141

4242
```sh
4343
cd my-sandbox
4444
```
4545

46+
:::note[Image variants]
47+
The template uses the Python image variant (`cloudflare/sandbox:<version>-python`) by default. If you only need JavaScript/shell execution and want a smaller image (~600-800 MB vs ~1.3 GB), you can change the Dockerfile to use the default image:
48+
49+
```dockerfile
50+
# Lean image (no Python)
51+
FROM docker.io/cloudflare/sandbox:0.5.6
52+
53+
# Full image (with Python)
54+
FROM docker.io/cloudflare/sandbox:0.5.6-python
55+
```
56+
57+
See [Dockerfile reference](/sandbox/configuration/dockerfile/) for details on choosing the right image variant.
58+
:::
59+
4660
## 2. Explore the template
4761

4862
The template provides a minimal Worker that demonstrates core sandbox capabilities:

src/content/docs/sandbox/guides/code-execution.mdx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ Use `exec()` for **advanced or custom workflows**:
3636
- **Shell commands** - Git operations, system utilities, complex pipelines
3737
- **Long-running processes** - Background services, servers
3838

39+
## Python runtime requirements
40+
41+
:::caution[Python image required]
42+
To execute Python code, your Dockerfile **must** use the Python image variant:
43+
44+
```dockerfile
45+
FROM docker.io/cloudflare/sandbox:0.5.6-python
46+
```
47+
48+
The default image (`cloudflare/sandbox:0.5.6`) does not include Python. If you attempt to run Python code without the Python image, you will receive a `PYTHON_NOT_AVAILABLE` error.
49+
50+
See [Dockerfile reference](/sandbox/configuration/dockerfile/) for details on image variants.
51+
:::
52+
3953
## Create an execution context
4054

4155
Code contexts maintain state between executions:
@@ -46,15 +60,15 @@ import { getSandbox } from '@cloudflare/sandbox';
4660
4761
const sandbox = getSandbox(env.Sandbox, 'my-sandbox');
4862
49-
// Create a Python context
63+
// Create a Python context (requires Python image)
5064
const pythonContext = await sandbox.createCodeContext({
5165
language: 'python'
5266
});
5367
5468
console.log('Context ID:', pythonContext.id);
5569
console.log('Language:', pythonContext.language);
5670
57-
// Create a JavaScript context
71+
// Create a JavaScript context (works on both image variants)
5872
const jsContext = await sandbox.createCodeContext({
5973
language: 'javascript'
6074
});

0 commit comments

Comments
 (0)