Skip to content

Commit 0ac3cfa

Browse files
Merge pull request #5 from cloudflare/fix-deploy
Add commands to build and publish docker image
2 parents eb0ea62 + 7c15b81 commit 0ac3cfa

File tree

11 files changed

+87
-19
lines changed

11 files changed

+87
-19
lines changed

.changeset/ten-areas-doubt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/sandbox": patch
3+
---
4+
5+
Make package ready for deployment

.github/workflows/release.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@ jobs:
3636
GITHUB_TOKEN: ${{ secrets.SANDBOX_GITHUB_TOKEN }}
3737
NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
3838
NPM_PUBLISH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
39+
40+
- name: Build and push Docker image
41+
if: steps.changesets.outputs.published == 'true'
42+
run: |
43+
cd packages/sandbox
44+
npm run docker:build
45+
npm run docker:publish
46+
env:
47+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,23 @@
22

33
> **⚠️ Experimental** - This library is currently experimental and we're actively seeking feedback. Please try it out and let us know what you think!
44
5-
A library to spin up a sandboxed environment.
5+
A library to spin up a sandboxed environment. **If you'd like to try one of our pre-made examples, take a look at [examples/basic](./examples/basic) ready to deploy to your Cloudflare account!**
6+
7+
First, create a Dockerfile at the root of your project, with the following content:
8+
9+
```Dockerfile
10+
# If building your project on amd64, use:
11+
FROM docker.io/ghostwriternr/cloudflare-sandbox:0.0.4
12+
# If building your project on arm64, use:
13+
# FROM docker.io/ghostwriternr/cloudflare-sandbox-arm:0.0.4
14+
15+
EXPOSE 3000
16+
17+
# Run the same command as the original image
18+
CMD ["bun", "index.ts"]
19+
```
20+
21+
> **NOTE**: In an upcoming release, this step will be removed entirely and you can reference a single Docker image published by us directly in your wrangler configuration below.
622
723
First, setup your wrangler.json to use the sandbox:
824

@@ -12,7 +28,7 @@ First, setup your wrangler.json to use the sandbox:
1228
"containers": [
1329
{
1430
"class_name": "Sandbox",
15-
"image": "./node_modules/@cloudflare/sandbox/Dockerfile",
31+
"image": "./Dockerfile",
1632
"name": "sandbox"
1733
}
1834
],

examples/basic/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# syntax=docker/dockerfile:1
2+
3+
FROM docker.io/ghostwriternr/cloudflare-sandbox:0.0.4
4+
5+
EXPOSE 3000
6+
7+
# Run the same command as the original image
8+
CMD ["bun", "index.ts"]

examples/basic/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"private": true,
66
"description": "An example of using the sandbox package",
77
"scripts": {
8-
"start": "wrangler dev"
8+
"start": "wrangler dev",
9+
"deploy": "wrangler deploy"
910
},
1011
"author": "",
1112
"license": "MIT"

examples/basic/wrangler.jsonc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"containers": [
1818
{
1919
"class_name": "Sandbox",
20-
"image": "../../node_modules/@cloudflare/sandbox/Dockerfile",
20+
"image": "./Dockerfile",
2121
"name": "sandbox",
2222
"max_instances": 1
2323
}

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"tsup": "^8.5.0",
3333
"tsx": "^4.20.3",
3434
"typescript": "^5.8.3",
35-
"wrangler": "^4.21.0"
35+
"wrangler": "^4.21.2"
3636
},
3737
"private": true,
3838
"packageManager": "[email protected]"

packages/sandbox/container_src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ const server = serve({
8484
const url = new URL(req.url);
8585
const pathname = url.pathname;
8686

87+
console.log(`[Container] Incoming ${req.method} request to ${pathname}`);
88+
8789
// Handle CORS
8890
const corsHeaders = {
8991
"Access-Control-Allow-Headers": "Content-Type, Authorization",
@@ -93,11 +95,13 @@ const server = serve({
9395

9496
// Handle preflight requests
9597
if (req.method === "OPTIONS") {
98+
console.log(`[Container] Handling CORS preflight for ${pathname}`);
9699
return new Response(null, { headers: corsHeaders, status: 200 });
97100
}
98101

99102
try {
100103
// Handle different routes
104+
console.log(`[Container] Processing ${req.method} ${pathname}`);
101105
switch (pathname) {
102106
case "/":
103107
return new Response("Hello from Bun server! 🚀", {
@@ -352,13 +356,14 @@ const server = serve({
352356
break;
353357

354358
default:
359+
console.log(`[Container] Route not found: ${pathname}`);
355360
return new Response("Not Found", {
356361
headers: corsHeaders,
357362
status: 404,
358363
});
359364
}
360365
} catch (error) {
361-
console.error("[Server] Error handling request:", error);
366+
console.error(`[Container] Error handling ${req.method} ${pathname}:`, error);
362367
return new Response(
363368
JSON.stringify({
364369
error: "Internal server error",
@@ -374,6 +379,7 @@ const server = serve({
374379
);
375380
}
376381
},
382+
hostname: "0.0.0.0",
377383
port: 3000,
378384
} as any);
379385

@@ -2874,7 +2880,7 @@ function executeMoveFile(
28742880
});
28752881
}
28762882

2877-
console.log(`🚀 Bun server running on http://localhost:${server.port}`);
2883+
console.log(`🚀 Bun server running on http://0.0.0.0:${server.port}`);
28782884
console.log(`📡 HTTP API endpoints available:`);
28792885
console.log(` POST /api/session/create - Create a new session`);
28802886
console.log(` GET /api/session/list - List all sessions`);

packages/sandbox/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"durable objects"
1818
],
1919
"scripts": {
20-
"build": "rm -rf dist && tsup src/*.ts --outDir dist --dts --sourcemap --format esm"
20+
"build": "rm -rf dist && tsup src/*.ts --outDir dist --dts --sourcemap --format esm",
21+
"docker:build": "docker build -t ghostwriternr/cloudflare-sandbox:$npm_package_version .",
22+
"docker:publish": "docker push docker.io/ghostwriternr/cloudflare-sandbox:$npm_package_version"
2123
},
2224
"exports": {
2325
".": {

0 commit comments

Comments
 (0)