Docker

Docker 对象

listContainers(options?): Promise<unknown>

获取容器列表

const containers = await ddClient.docker.listContainers();

listImages(options?): Promise<unknown>

获取本地容器镜像列表

const images = await ddClient.docker.listImages();

有关这些方法的详细信息,请参阅 Docker API 参考

已弃用的 Docker 对象访问

以下方法已弃用,并将在未来版本中移除。请使用上面指定的方法。

const containers = await window.ddClient.listContainers();

const images = await window.ddClient.listImages();

Docker 命令

扩展还可以直接执行 docker 命令行。

exec(cmd, args): Promise< ExecResult>

const result = await ddClient.docker.cli.exec("info", [
  "--format",
  '"{{ json . }}"',
]);

结果包含已执行命令的标准输出和标准错误

{
  "stderr": "...",
  "stdout": "..."
}

在此示例中,命令输出为 JSON。为方便起见,命令结果对象还具有用于轻松解析它的方法

  • result.lines(): string[] 分割输出行。
  • result.parseJsonObject(): any 解析格式良好的 json 输出。
  • result.parseJsonLines(): any[] 将每行输出解析为 json 对象。

exec(cmd, args, options): void

上面的命令将输出作为执行 Docker 命令的结果进行流式传输。如果您需要以流的形式获取输出或命令输出太长,这很有用。

await ddClient.docker.cli.exec("logs", ["-f", "..."], {
  stream: {
    onOutput(data) {
      if (data.stdout) {
        console.error(data.stdout);
      } else {
        console.log(data.stderr);
      }
    },
    onError(error) {
      console.error(error);
    },
    onClose(exitCode) {
      console.log("onClose with exit code " + exitCode);
    },
    splitOutputLines: true,
  },
});

当您关闭 Docker Desktop 中的仪表板或退出扩展 UI 时,扩展创建的子进程会自动终止(SIGTERM)。如果需要,您还可以使用 exec(streamOptions) 调用的结果来终止(SIGTERM)进程。

const logListener = await ddClient.docker.cli.exec("logs", ["-f", "..."], {
  stream: {
    // ...
  },
});

// when done listening to logs or before starting a new one, kill the process
logListener.close();

exec(streamOptions) API 也可用于监听 docker 事件

await ddClient.docker.cli.exec(
  "events",
  ["--format", "{{ json . }}", "--filter", "container=my-container"],
  {
    stream: {
      onOutput(data) {
        if (data.stdout) {
          const event = JSON.parse(data.stdout);
          console.log(event);
        } else {
          console.log(data.stderr);
        }
      },
      onClose(exitCode) {
        console.log("onClose with exit code " + exitCode);
      },
      splitOutputLines: true,
    },
  }
);
注意

您不能使用此方法在单个 exec() 调用中链式执行命令(例如 docker kill $(docker ps -q) 或在命令之间使用管道)。

您需要为每个命令调用 exec(),并在需要时解析结果以将参数传递给下一个命令。

有关这些方法的详细信息,请参阅 Exec API 参考

已弃用的 Docker 命令执行

此方法已弃用,并将在未来版本中移除。请使用下面指定的方法。

const output = await window.ddClient.execDockerCmd(
  "info",
  "--format",
  '"{{ json . }}"'
);

window.ddClient.spawnDockerCmd("logs", ["-f", "..."], (data, error) => {
  console.log(data.stdout);
});
© . This site is unofficial and not affiliated with Kubernetes or Docker Inc.