run-cloud-sandboxes
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseOperate run.cloud Sandboxes
操作run.cloud沙箱
Use the CLI for terminal and interactive workflows. Use
for TypeScript applications, CI, and agent code.
runcloud@run-cloud/sdk对于终端和交互式工作流,使用 CLI。对于TypeScript应用、CI和Agent代码,使用。
runcloud@run-cloud/sdkAuthenticate
身份验证
- Install the CLI with .
npm install -g runcloud - Use for an interactive browser handoff. Use
runcloud loginwhen a local callback cannot open.runcloud login --manual - In CI, set .
RUN_CLOUD_API_KEYis an equivalent alias.RUN_CLOUD_API_TOKEN - Set only to override the production default
RUN_CLOUD_API_URL.https://api.run.cloud - Never print, commit, or place credentials in a skill file.
- Treat signed desktop and tunnel URLs as bearer secrets.
- Require Node.js 20 or newer for the CLI and TypeScript SDK.
Inspect account and organization usage before starting metered work:
bash
runcloud account --json- 使用安装CLI。
npm install -g runcloud - 使用进行交互式浏览器授权。当无法打开本地回调时,使用
runcloud login。runcloud login --manual - 在CI环境中,设置。
RUN_CLOUD_API_KEY是其等效别名。RUN_CLOUD_API_TOKEN - 仅当需要覆盖生产环境默认地址时,设置
https://api.run.cloud。RUN_CLOUD_API_URL - 绝不要打印、提交或在技能文件中放置凭证。
- 将已签名的桌面和隧道URL视为Bearer密钥。
- CLI和TypeScript SDK要求Node.js 20或更高版本。
在开始计量工作前,检查账户和组织使用情况:
bash
runcloud account --jsonChoose the Interface
选择交互方式
- Prefer CLI commands with for shell automation.
--json - Prefer the TypeScript SDK when code needs streaming command output, binary file transfer, retry-safe creation, or short-lived public tunnels.
- Inspect , a subcommand's
runcloud sandbox --help, or installed SDK types before using a method not documented here.--help - Use the noun. The older
sandboxcommands are deprecated aliases.box
- 对于Shell自动化,优先使用带参数的CLI命令。
--json - 当代码需要流式命令输出、二进制文件传输、可重试创建或短期公共隧道时,优先使用TypeScript SDK。
- 在使用本文档未记录的方法前,查看、子命令的
runcloud sandbox --help或已安装SDK的类型定义。--help - 使用名词。旧版
sandbox命令已废弃,仅作为别名存在。box
Run a CLI Lifecycle
执行CLI生命周期流程
Create a sandbox, capture its ID, run a command, and destroy it:
bash
SANDBOX_ID=$(runcloud sandbox create \
--image runcloud/agent-base \
--timeout 900 \
--json | jq -r '.id')
trap 'runcloud sandbox rm "$SANDBOX_ID" >/dev/null 2>&1 || true' EXIT
runcloud sandbox get "$SANDBOX_ID" --json
runcloud sandbox exec "$SANDBOX_ID" "npm install && npm test"The main lifecycle commands are:
runcloud sandbox createruncloud sandbox list [--state <state>]runcloud sandbox get <id>runcloud sandbox exec <id> <cmd...>runcloud sandbox shell <id>runcloud sandbox pause|resume <id>runcloud sandbox logs <id> [--lines <n>]runcloud sandbox metrics <id> [--range <range>] [--watch]runcloud sandbox rm <id>
Create accepts , , , , , ,
, , , , , secret
selectors, , and .
--image--region--name--org--cpu--memory--disk--idle-pause--timeout--persistent--expose--no-wait--jsonThe default reservation is 0.125 vCPU and 128 MiB when CPU and memory are
omitted. Set finite timeouts for unattended work. Use only when a
persistent workload is intentional.
--timeout 0CLI runs through and returns the guest command's exit code.
A paused sandbox must be resumed before ; resumes it
automatically.
exec/bin/sh -cexecshell创建沙箱、捕获其ID、运行命令并销毁它:
bash
SANDBOX_ID=$(runcloud sandbox create \
--image runcloud/agent-base \
--timeout 900 \
--json | jq -r '.id')
trap 'runcloud sandbox rm "$SANDBOX_ID" >/dev/null 2>&1 || true' EXIT
runcloud sandbox get "$SANDBOX_ID" --json
runcloud sandbox exec "$SANDBOX_ID" "npm install && npm test"主要的生命周期命令包括:
runcloud sandbox createruncloud sandbox list [--state <state>]runcloud sandbox get <id>runcloud sandbox exec <id> <cmd...>runcloud sandbox shell <id>runcloud sandbox pause|resume <id>runcloud sandbox logs <id> [--lines <n>]runcloud sandbox metrics <id> [--range <range>] [--watch]runcloud sandbox rm <id>
create--image--region--name--org--cpu--memory--disk--idle-pause--timeout--persistent--expose--no-wait--json当省略CPU和内存参数时,默认预留0.125 vCPU和128 MiB内存。对于无人值守的工作,设置有限的超时时间。仅当需要持久化工作负载时,才使用。
--timeout 0CLI的命令通过执行,并返回客端命令的退出码。暂停的沙箱必须先恢复才能执行;命令会自动恢复沙箱。
exec/bin/sh -cexecshellUse the TypeScript SDK
使用TypeScript SDK
Install the SDK:
bash
npm install @run-cloud/sdkUse an idempotency key when a job runner may retry creation, check non-zero
exit codes explicitly, and always destroy metered resources:
ts
import { Client } from "@run-cloud/sdk";
const cloud = new Client();
const sandbox = await cloud.sandboxes.create({
image: "runcloud/agent-base",
cpu: 1,
memory: 1024,
timeoutSeconds: 900,
idempotencyKey: process.env.CI_JOB_ID,
});
try {
const result = await cloud.sandboxes.exec(
sandbox.id,
["npm", "test"],
{
onStdout: (chunk) => process.stdout.write(chunk),
onStderr: (chunk) => process.stderr.write(chunk),
},
);
if (result.exitCode !== 0) {
throw new Error(`tests failed with exit code ${result.exitCode}`);
}
} finally {
await cloud.sandboxes.destroy(sandbox.id);
}The TypeScript sandbox surface is:
- :
cloud.sandboxes,create,list,get,exec,readFile,writeFile,openTunnel,closeTunnel,setTimeout,snapshot, and thedestroyaliasdelete - :
cloud.snapshots,list,restoredelete - and
cloud.account()cloud.usage({ orgId? })
A string passed to SDK runs through ; an argv array executes
directly. A non-zero guest exit code is returned rather than thrown. Use
, , , , , and as needed.
exec/bin/sh -ccwdenvtimeoutSecondsonStdoutonStderrsignalUse and for binary-safe SDK file transfer. For
interactive terminal workflows, configure SSH with
and inspect , , and help before use.
readFilewriteFileruncloud sandbox setup-sshruncloud sandbox sshcpcode安装SDK:
bash
npm install @run-cloud/sdk当作业运行器可能重试创建操作时,使用幂等键;显式检查非零退出码;并始终销毁计量资源:
ts
import { Client } from "@run-cloud/sdk";
const cloud = new Client();
const sandbox = await cloud.sandboxes.create({
image: "runcloud/agent-base",
cpu: 1,
memory: 1024,
timeoutSeconds: 900,
idempotencyKey: process.env.CI_JOB_ID,
});
try {
const result = await cloud.sandboxes.exec(
sandbox.id,
["npm", "test"],
{
onStdout: (chunk) => process.stdout.write(chunk),
onStderr: (chunk) => process.stderr.write(chunk),
},
);
if (result.exitCode !== 0) {
throw new Error(`tests failed with exit code ${result.exitCode}`);
}
} finally {
await cloud.sandboxes.destroy(sandbox.id);
}TypeScript沙箱的核心接口包括:
- :
cloud.sandboxes、create、list、get、exec、readFile、writeFile、openTunnel、closeTunnel、setTimeout、snapshot,以及别名destroydelete - :
cloud.snapshots、list、restoredelete - 和
cloud.account()cloud.usage({ orgId? })
传递给SDK 的字符串会通过执行;参数数组则会直接执行。客端返回的非零退出码会被返回而非抛出异常。可根据需要使用、、、、和参数。
exec/bin/sh -ccwdenvtimeoutSecondsonStdoutonStderrsignal使用和进行二进制安全的SDK文件传输。对于交互式终端工作流,使用配置SSH,并在使用前查看、和命令的帮助文档。
readFilewriteFileruncloud sandbox setup-sshruncloud sandbox sshcpcodeSnapshot and Restore
快照与恢复
Snapshot a prepared filesystem and restore it into fresh sandboxes:
bash
runcloud sandbox snapshot create "$SANDBOX_ID" --label deps-installed --json
runcloud sandbox snapshot list --json
runcloud sandbox restore <snapshot-id> --json
runcloud sandbox snapshot rm <snapshot-id>The SDK equivalents are ,
, , and
.
cloud.sandboxes.snapshotcloud.snapshots.restorecloud.snapshots.listcloud.snapshots.deleteA restore creates a new billed sandbox with a new ID. One snapshot can fan out
to parallel workers, but every restored sandbox counts toward concurrency and
must be destroyed. A restored sandbox inherits no secrets; attach only the
secrets it needs.
为已准备好的文件系统创建快照,并将其恢复到新沙箱中:
bash
runcloud sandbox snapshot create "$SANDBOX_ID" --label deps-installed --json
runcloud sandbox snapshot list --json
runcloud sandbox restore <snapshot-id> --json
runcloud sandbox snapshot rm <snapshot-id>对应的SDK方法为、、和。
cloud.sandboxes.snapshotcloud.snapshots.restorecloud.snapshots.listcloud.snapshots.delete恢复操作会创建一个新的计费沙箱并生成新ID。一个快照可以扩展到多个并行工作器,但每个恢复的沙箱都会占用并发资源,必须被销毁。恢复后的沙箱不会继承任何密钥;仅附加它所需的密钥即可。
Use Images
使用镜像
Use reusable images when every sandbox needs the same base tools:
bash
runcloud image create my-agent --dockerfile ./Dockerfile
runcloud image list --json
runcloud sandbox create --image my-agent --json
runcloud image refresh my-agentInspect for current build-source options. Do not invent
image methods on the TypeScript SDK.
runcloud image --help当每个沙箱都需要相同的基础工具时,使用可复用镜像:
bash
runcloud image create my-agent --dockerfile ./Dockerfile
runcloud image list --json
runcloud sandbox create --image my-agent --json
runcloud image refresh my-agent查看获取当前的构建源选项。不要在TypeScript SDK中自行实现镜像相关方法。
runcloud image --helpExpose a Service
暴露服务
Choose one exposure model deliberately:
- For a stable hostname, create with
, or use
runcloud sandbox create --name <name> --expose <port> --persistent.runcloud sandbox expose <id> --port <port> - For a short-lived random URL in TypeScript, call
, then
cloud.sandboxes.openTunnel(id, port, { ttlSeconds }).closeTunnel(id, tunnel.id)
An SDK tunnel does not make the sandbox persistent or disable idle pause. Do
not log its URL. Revocation can take effect shortly after
returns; stop the guest service or destroy the sandbox when access must end
immediately.
closeTunnel谨慎选择一种暴露模式:
- 若需要稳定的主机名,使用创建沙箱,或使用
runcloud sandbox create --name <name> --expose <port> --persistent。runcloud sandbox expose <id> --port <port> - 若需要TypeScript中的短期随机URL,调用,之后调用
cloud.sandboxes.openTunnel(id, port, { ttlSeconds })。closeTunnel(id, tunnel.id)
SDK隧道不会使沙箱持久化或禁用空闲暂停功能。不要记录其URL。调用后,撤销操作会立即生效;当必须立即终止访问时,请停止客端服务或销毁沙箱。
closeTunnelHandle Secrets Safely
安全处理密钥
- Create secret groups from ,
--from-dotenv,--from-json, a file, or a hidden prompt. Never pass a secret value as a command argument.--stdin - Attach only the required groups or names with repeatable and
--secret-group. Later selectors win on name collisions;--secretis applied last.--env - Use to state explicitly that a new sandbox needs none.
--no-secrets - Treat as full replacement, not a merge.
runcloud sandbox secrets <id> ... - Remember that values cannot be read back and snapshots do not contain secrets.
Inspect and for the
current non-plaintext input forms.
runcloud secret-group --helpruncloud secrets --help- 通过、
--from-dotenv、--from-json、文件或隐藏提示创建密钥组。绝不要将密钥值作为命令参数传递。--stdin - 仅使用可重复的和
--secret-group参数附加所需的组或名称。当名称冲突时,后续的选择器优先级更高;--secret会最后应用。--env - 使用明确表示新沙箱不需要任何密钥。
--no-secrets - 将视为完全替换操作,而非合并操作。
runcloud sandbox secrets <id> ... - 请注意,密钥值无法被回读,且快照中不包含密钥。
查看和获取当前的非明文输入形式。
runcloud secret-group --helpruncloud secrets --helpOperate Desktop Sandboxes
操作桌面沙箱
For a compatible desktop image, use to open its
signed browser desktop. The CLI also provides , , ,
and subcommands for explicit pixel-coordinate automation. Keep signed
desktop URLs private and inspect each subcommand's help before automation.
runcloud sandbox desktop <id>screenshotclicktypekey对于兼容的桌面镜像,使用打开其已签名的浏览器桌面。CLI还提供、、和子命令,用于基于像素坐标的显式自动化。请保持已签名的桌面URL私密,并在自动化前查看每个子命令的帮助文档。
runcloud sandbox desktop <id>screenshotclicktypekeyGuardrails
防护准则
- Destroy every sandbox created during a task unless the user explicitly asks to keep it. Also remove unused snapshots, tunnels, and public hostnames.
- Use or a shell trap around every metered lifecycle.
try/finally - Check ; do not treat a completed SDK
exitCodecall as success by itself.exec - Do not expose API credentials, secret values, signed desktop URLs, or tunnel URLs in logs, screenshots, PR comments, or chat output.
- Do not claim that CLI-only lifecycle, image, secret-group, desktop, or stable hostname commands are TypeScript SDK methods.
- 除非用户明确要求保留,否则销毁任务期间创建的所有沙箱。同时删除未使用的快照、隧道和公共主机名。
- 在每个计量生命周期流程周围使用或Shell陷阱。
try/finally - 检查;不要仅将SDK
exitCode调用完成视为成功。exec - 不要在日志、截图、PR评论或聊天输出中暴露API凭证、密钥值、已签名的桌面URL或隧道URL。
- 不要声称仅CLI支持的生命周期、镜像、密钥组、桌面或稳定主机名命令是TypeScript SDK的方法。