DockerSandbox
public class DockerSandbox extends AbstractBaseSandbox implements SandboxFileTransfer
io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:74–775Docker 沙箱,承载智能体的部署、调度与服务化运行。位于 io.agentscope.harness.agent.sandbox.impl.docker 包,继承自 AbstractBaseSandbox,实现 SandboxFileTransfer。核心成员包括 start、shutdown、doExec、doPersistWorkspace、doHydrateWorkspace、doSetupWorkspace。
Docker io.agentscope.harness.agent.sandbox.Sandbox that runs commands in a container.
Uses the docker CLI via ProcessBuilder; no docker-java library
dependency is required. The Docker daemon must be accessible on the host.
Container Lifecycle
-
On
#start(): the container is created and started if it does not exist; if the container exists but is stopped it is restarted; if it is already running the existing container is reused. -
On
#stop(): the workspace snapshot is persisted (if configured). The container keeps running. -
On
#shutdown(): the container is stopped and removed if self-managed.
Workspace Operations
-
Exec:
docker exec -w <root> <containerId> sh -c <command> -
PersistWorkspace:
docker exec <containerId> tar -cf - -C <root> . -
HydrateWorkspace:
docker exec -i <containerId> tar -xf - -C <root> -
UploadFile / DownloadFile (
SandboxFileTransfer): a host temp file round-tripped throughdocker cp, so file bytes never pass through exec argv or the stdout capture cap. Scoped to the workspace subtree by lexical validation; uploads land with mode 0644, matching the archive-hydrate fallback. Symlinks inside the sandbox are not resolved — the isolation boundary is the container, not the subtree.
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public DockerSandbox(DockerSandboxState state) | 初始化 `DockerSandbox` 实例。 Creates a new instance. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public void start() throws Exception | 执行启动相关操作。 Ensures the backing Docker container is running before executing the standard
4-branch workspace start logic. |
public void shutdown() throws Exception | 关闭资源并释放连接。 Stops and removes the Docker container if self-managed. |
protected ExecResult doExec(RuntimeContext runtimeContext, String command, int timeoutSeconds) throws Exception | 执行 `doExec` 回调。 Performs the do exec operation. |
protected InputStream doPersistWorkspace() throws Exception | 执行 `doPersistWorkspace` 回调。 Returns the do persist workspace. |
protected void doHydrateWorkspace(InputStream archive) throws Exception | 执行 `doHydrateWorkspace` 回调。 Performs the do hydrate workspace operation. |
protected void doSetupWorkspace() throws Exception | 执行 `doSetupWorkspace` 回调。 Returns the do setup workspace. |
protected void doDestroyWorkspace() throws Exception | 执行 `doDestroyWorkspace` 回调。 Returns the do destroy workspace. |
protected String getWorkspaceRoot() | 获取工作区根。 Returns the workspace root. |
public boolean supportsFileTransfer(String path) | 设置该参数。 Performs the supports file transfer operation. |
public void uploadFile(String path, byte[] content) throws Exception | 执行上传文件相关操作。 Performs the upload file operation. |
public byte[] downloadFile(String path) throws Exception | 设置下载文件。 Performs the download file operation. |
构造器详情Constructor Detail
DockerSandbox
public DockerSandbox(DockerSandboxState state)
初始化 DockerSandbox 实例。
Creates a new instance.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
state | DockerSandboxState | 状态对象(类型 `DockerSandboxState`)。The state. |
io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:85–88方法详情Method Detail
start
public void start() throws Exception
执行启动相关操作。
Ensures the backing Docker container is running before executing the standard 4-branch workspace start logic.
Exception可能抛出 `Exception`。Thrown when the operation cannot be completed.
io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:96–100shutdown
public void shutdown() throws Exception
关闭资源并释放连接。
Stops and removes the Docker container if self-managed.
Exception可能抛出 `Exception`。Thrown when the operation cannot be completed.
io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:107–142doExec
protected ExecResult doExec(RuntimeContext runtimeContext, String command, int timeoutSeconds) throws Exception
执行 doExec 回调。
Performs the do exec operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
runtimeContext | RuntimeContext | 运行时上下文(类型 `RuntimeContext`)。The runtime context. |
command | String | 命令(类型 `String`)。The command. |
timeoutSeconds | int | 超时秒(类型 `int`)。The timeout seconds. |
ExecResultException可能抛出 `Exception`。Thrown when the operation cannot be completed.
io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:144–200doPersistWorkspace
protected InputStream doPersistWorkspace() throws Exception
执行 doPersistWorkspace 回调。
Returns the do persist workspace.
InputStreamException可能抛出 `Exception`。Thrown when the operation cannot be completed.
io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:202–259doHydrateWorkspace
protected void doHydrateWorkspace(InputStream archive) throws Exception
执行 doHydrateWorkspace 回调。
Performs the do hydrate workspace operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
archive | InputStream | 归档(类型 `InputStream`)。The archive. |
Exception可能抛出 `Exception`。Thrown when the operation cannot be completed.
io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:261–333doSetupWorkspace
protected void doSetupWorkspace() throws Exception
执行 doSetupWorkspace 回调。
Returns the do setup workspace.
Exception可能抛出 `Exception`。Thrown when the operation cannot be completed.
io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:335–340doDestroyWorkspace
protected void doDestroyWorkspace() throws Exception
执行 doDestroyWorkspace 回调。
Returns the do destroy workspace.
Exception可能抛出 `Exception`。Thrown when the operation cannot be completed.
io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:342–357getWorkspaceRoot
protected String getWorkspaceRoot()
获取工作区根。
Returns the workspace root.
Stringio/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:359–362supportsFileTransfer
public boolean supportsFileTransfer(String path)
设置该参数。
Performs the supports file transfer operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | String | 路径(类型 `String`)。The path. |
booleanio/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:364–372uploadFile
public void uploadFile(String path, byte[] content) throws Exception
执行上传文件相关操作。
Performs the upload file operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | String | 路径(类型 `String`)。The path. |
content | byte[] | 内容(类型 `byte[]`)。The content. |
Exception可能抛出 `Exception`。Thrown when the operation cannot be completed.
io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:374–397downloadFile
public byte[] downloadFile(String path) throws Exception
设置下载文件。
Performs the download file operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
path | String | 路径(类型 `String`)。The path. |
byte[]Exception可能抛出 `Exception`。Thrown when the operation cannot be completed.
io/agentscope/harness/agent/sandbox/impl/docker/DockerSandbox.java:399–414