@computesdk/docker 1.2.7 → 1.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -39
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -35,35 +35,48 @@ Common environment variables (used by Docker/dockerode):
|
|
|
35
35
|
|
|
36
36
|
## Usage
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
### Gateway Mode (Recommended)
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
Use the gateway for zero-config auto-detection:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { compute } from 'computesdk';
|
|
44
|
+
|
|
45
|
+
// Auto-detects Docker (requires Docker daemon running)
|
|
46
|
+
const sandbox = await compute.sandbox.create();
|
|
47
|
+
|
|
48
|
+
const result = await sandbox.runCode(`print("Hello from Python")`, 'python');
|
|
49
|
+
console.log(result.stdout.trim()); // Hello from Python
|
|
50
|
+
|
|
51
|
+
await sandbox.destroy();
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Direct Mode
|
|
55
|
+
|
|
56
|
+
For direct SDK usage without the gateway:
|
|
41
57
|
|
|
42
58
|
```ts
|
|
43
|
-
import { createCompute } from 'computesdk';
|
|
44
59
|
import { docker } from '@computesdk/docker';
|
|
45
60
|
|
|
46
|
-
const
|
|
61
|
+
const compute = docker({
|
|
47
62
|
runtime: 'python', // or 'node'
|
|
48
63
|
image: { name: 'python:3.11-slim', pullPolicy: 'ifNotPresent' },
|
|
49
64
|
});
|
|
50
65
|
|
|
51
|
-
const compute = createCompute({ defaultProvider: provider });
|
|
52
|
-
|
|
53
66
|
// Create a sandbox and run Python
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
console.log(
|
|
67
|
+
const sandbox = await compute.sandbox.create();
|
|
68
|
+
const result = await sandbox.runCode(`print("Hello from Python")`, 'python');
|
|
69
|
+
console.log(result.stdout.trim()); // Hello from Python
|
|
57
70
|
|
|
58
|
-
await
|
|
71
|
+
await sandbox.destroy();
|
|
59
72
|
```
|
|
60
73
|
|
|
61
|
-
###
|
|
74
|
+
### Advanced Configuration
|
|
62
75
|
|
|
63
76
|
```ts
|
|
64
77
|
import { docker } from '@computesdk/docker';
|
|
65
78
|
|
|
66
|
-
const
|
|
79
|
+
const compute = docker({
|
|
67
80
|
runtime: 'node',
|
|
68
81
|
image: { name: 'node:20-alpine' },
|
|
69
82
|
container: {
|
|
@@ -72,15 +85,15 @@ const provider = docker({
|
|
|
72
85
|
},
|
|
73
86
|
});
|
|
74
87
|
|
|
75
|
-
const
|
|
88
|
+
const sandbox = await compute.sandbox.create({ runtime: 'node' });
|
|
76
89
|
|
|
77
|
-
const
|
|
78
|
-
console.log(
|
|
90
|
+
const result = await sandbox.runCode(`console.log("Hello, World!")`, 'node');
|
|
91
|
+
console.log(result.stdout.trim()); // Hello, World!
|
|
79
92
|
|
|
80
|
-
const
|
|
81
|
-
console.log(
|
|
93
|
+
const cmd = await sandbox.runCommand('sh', ['-lc', 'echo Hello from command']);
|
|
94
|
+
console.log(cmd.stdout.trim()); // Hello from command
|
|
82
95
|
|
|
83
|
-
await
|
|
96
|
+
await sandbox.destroy();
|
|
84
97
|
```
|
|
85
98
|
|
|
86
99
|
---
|
|
@@ -212,12 +225,12 @@ Interactive terminals aren’t exposed. Use `runCommand('sh', ['-lc', '…'])` f
|
|
|
212
225
|
### Sandbox Management
|
|
213
226
|
|
|
214
227
|
```ts
|
|
215
|
-
const
|
|
216
|
-
await provider.sandbox.destroy(sb.sandboxId);
|
|
228
|
+
const sandbox = await compute.sandbox.create({ runtime?: 'python' | 'node' });
|
|
217
229
|
|
|
218
|
-
await
|
|
219
|
-
await
|
|
220
|
-
|
|
230
|
+
await sandbox.getInfo(); // { id, provider, runtime, status, createdAt, timeout, metadata }
|
|
231
|
+
await sandbox.getUrl({ port, protocol?: 'http' | 'https' });
|
|
232
|
+
sandbox.getInstance(); // { docker: Docker, container: Container, ... }
|
|
233
|
+
await sandbox.destroy();
|
|
221
234
|
```
|
|
222
235
|
|
|
223
236
|
---
|
|
@@ -254,14 +267,14 @@ if (res.exitCode !== 0) {
|
|
|
254
267
|
import { handleComputeRequest } from 'computesdk';
|
|
255
268
|
import { docker } from '@computesdk/docker';
|
|
256
269
|
|
|
257
|
-
const
|
|
270
|
+
const compute = docker({
|
|
258
271
|
runtime: 'node',
|
|
259
272
|
image: { name: 'node:20-alpine' },
|
|
260
273
|
});
|
|
261
274
|
|
|
262
275
|
export async function POST(req: Request) {
|
|
263
276
|
const body = await req.json(); // ComputeRequest
|
|
264
|
-
return handleComputeRequest(body,
|
|
277
|
+
return handleComputeRequest(body, compute);
|
|
265
278
|
}
|
|
266
279
|
```
|
|
267
280
|
|
|
@@ -272,40 +285,40 @@ export async function POST(req: Request) {
|
|
|
272
285
|
### Data Science Workflow (Python)
|
|
273
286
|
|
|
274
287
|
```ts
|
|
275
|
-
const
|
|
276
|
-
await
|
|
277
|
-
await
|
|
288
|
+
const sandbox = await compute.sandbox.create({ runtime: 'python' });
|
|
289
|
+
await sandbox.runCommand('sh', ['-lc', 'python3 -m pip install --no-cache-dir pandas']);
|
|
290
|
+
await sandbox.filesystem.writeFile('/workspace/app.py', `
|
|
278
291
|
import pandas as pd
|
|
279
292
|
print("rows:", len(pd.DataFrame({"x":[1,2,3]})))
|
|
280
293
|
`);
|
|
281
|
-
const run = await
|
|
294
|
+
const run = await sandbox.runCommand('sh', ['-lc', 'python3 /workspace/app.py']);
|
|
282
295
|
console.log(run.stdout.trim()); // rows: 3
|
|
283
|
-
await
|
|
296
|
+
await sandbox.destroy();
|
|
284
297
|
```
|
|
285
298
|
|
|
286
299
|
### Interactive-like Loop
|
|
287
300
|
|
|
288
301
|
```ts
|
|
289
|
-
const
|
|
290
|
-
await
|
|
291
|
-
await
|
|
292
|
-
await
|
|
302
|
+
const sandbox = await compute.sandbox.create({ runtime: 'node' });
|
|
303
|
+
await sandbox.runCommand('sh', ['-lc', 'echo boot > /tmp/state']);
|
|
304
|
+
await sandbox.runCommand('sh', ['-lc', 'cat /tmp/state']); // 'boot'
|
|
305
|
+
await sandbox.destroy();
|
|
293
306
|
```
|
|
294
307
|
|
|
295
308
|
### Machine Learning Pipeline (Python + Ports)
|
|
296
309
|
|
|
297
310
|
```ts
|
|
298
|
-
const
|
|
299
|
-
const bg = await
|
|
300
|
-
const url = await
|
|
311
|
+
const sandbox = await compute.sandbox.create({ runtime: 'python' });
|
|
312
|
+
const bg = await sandbox.runCommand('sh', ['-lc', 'python3 -m http.server 8080'], { background: true });
|
|
313
|
+
const url = await sandbox.getUrl({ port: 8080 });
|
|
301
314
|
console.log('Serving at', url);
|
|
302
|
-
await
|
|
315
|
+
await sandbox.destroy();
|
|
303
316
|
```
|
|
304
317
|
|
|
305
318
|
> To make ports reachable from the host, publish them up front:
|
|
306
319
|
>
|
|
307
320
|
> ```ts
|
|
308
|
-
> const
|
|
321
|
+
> const compute = docker({
|
|
309
322
|
> runtime: 'python',
|
|
310
323
|
> image: { name: 'python:3.11-slim' },
|
|
311
324
|
> container: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@computesdk/docker",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.9",
|
|
4
4
|
"description": "Docker provider for ComputeSDK - local containerized sandboxes for development and testing",
|
|
5
5
|
"author": "IberAI",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"dockerode": "^4.0.8",
|
|
22
|
-
"computesdk": "1.10.
|
|
22
|
+
"computesdk": "1.10.3"
|
|
23
23
|
},
|
|
24
24
|
"keywords": [
|
|
25
25
|
"computesdk",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"tsup": "^8.0.0",
|
|
53
53
|
"typescript": "^5.0.0",
|
|
54
54
|
"vitest": "^1.0.0",
|
|
55
|
-
"@computesdk/test-utils": "1.5.
|
|
55
|
+
"@computesdk/test-utils": "1.5.1"
|
|
56
56
|
},
|
|
57
57
|
"scripts": {
|
|
58
58
|
"build": "tsup",
|