替代容器运行时

Docker Engine 使用 containerd 来管理容器生命周期,包括创建、启动和停止容器。默认情况下,containerd 使用 runc 作为其容器运行时。

我可以使用哪些运行时?

您可以使用任何实现了 containerd shim API 的运行时。这类运行时自带一个 containerd shim,无需任何额外配置即可使用。请参阅使用 containerd shims

实现了自己的 containerd shims 的运行时示例包括

您还可以使用被设计为 runc 的直接替代品的运行时。这类运行时依赖于 runc containerd shim 来调用运行时二进制文件。您必须在守护进程配置中手动注册这类运行时。

youki 是一个可以作为 runc 直接替代品的运行时示例。请参考解释设置的 youki 示例

使用 containerd shims

containerd shims 让您可以使用备用运行时,而无需更改 Docker 守护进程的配置。要使用 containerd shim,请在运行 Docker 守护进程的系统上,将 shim 二进制文件安装到 PATH 中。

要通过 docker run 使用 shim,请将运行时的完全限定名称指定为 --runtime 标志的值

$ docker run --runtime io.containerd.kata.v2 hello-world

在不安装到 PATH 的情况下使用 containerd shim

您可以在不将 shim 安装到 PATH 的情况下使用它,这种情况下,您需要在守护进程配置中按如下方式注册该 shim

{
  "runtimes": {
    "foo": {
      "runtimeType": "/path/to/containerd-shim-foobar-v1"
    }
  }
}

要使用该 shim,请指定您为其分配的名称

$ docker run --runtime foo hello-world

配置 shims

如果您需要为 containerd shim 传递额外配置,可以使用守护进程配置文件中的 runtimes 选项。

  1. 通过为您想要配置的 shim 添加一个 runtimes 条目来编辑守护进程配置文件。

    • runtimeType 键中指定运行时的完全限定名称
    • options 键下添加您的运行时配置
    {
      "runtimes": {
        "gvisor": {
          "runtimeType": "io.containerd.runsc.v1",
          "options": {
            "TypeUrl": "io.containerd.runsc.v1.options",
            "ConfigPath": "/etc/containerd/runsc.toml"
          }
        }
      }
    }
  2. 重新加载守护进程的配置。

    # systemctl reload docker
    
  3. 使用 docker run--runtime 标志来使用自定义的运行时。

    $ docker run --runtime gvisor hello-world
    

有关 containerd shims 配置选项的更多信息,请参阅配置 containerd shims

示例

以下示例向您展示如何设置和使用备用容器运行时与 Docker Engine。

youki

youki 是一个用 Rust 编写的容器运行时。youki 声称比 runc 更快且占用更少的内存,这使其成为资源受限环境的理想选择。

youki 可作为 runc 的直接替代品,这意味着它依赖 runc shim 来调用运行时二进制文件。当您注册作为 runc 替代品的运行时,您需要配置运行时可执行文件的路径,以及可选的一组运行时参数。更多信息,请参阅配置 runc 直接替代品

要将 youki 添加为容器运行时

  1. 安装 youki 及其依赖项。

    有关说明,请参阅官方设置指南

  2. 通过编辑 Docker 守护进程配置文件(默认位于 /etc/docker/daemon.json)将 youki 注册为 Docker 的运行时。

    path 键应指定您安装 youki 的路径。

    # cat > /etc/docker/daemon.json <<EOF
    {
      "runtimes": {
        "youki": {
          "path": "/usr/local/bin/youki"
        }
      }
    }
    EOF
    
  3. 重新加载守护进程的配置。

    # systemctl reload docker
    

现在,您可以运行使用 youki 作为运行时的容器。

$ docker run --rm --runtime youki hello-world

Wasmtime

可用性: 实验性

Wasmtime 是一个 字节码联盟 项目,也是一个让您能够运行 Wasm 容器的 Wasm 运行时。使用 Docker 运行 Wasm 容器提供了两层安全保障。您不仅能获得容器隔离带来的所有好处,还能享受到 Wasm 运行时环境提供的额外沙箱保护。

要将 Wasmtime 添加为容器运行时,请遵循以下步骤

  1. 在守护进程配置文件中打开 containerd 镜像存储 功能。

    {
      "features": {
        "containerd-snapshotter": true
      }
    }
  2. 重启 Docker 守护进程。

    # systemctl restart docker
    
  3. PATH 中安装 Wasmtime containerd shim。

    以下命令 Dockerfile 从源代码构建 Wasmtime 二进制文件,并将其导出到 ./containerd-shim-wasmtime-v1

    $ docker build --output . - <<EOF
    FROM rust:latest as build
    RUN cargo install \
        --git https://github.com/containerd/runwasi.git \
        --bin containerd-shim-wasmtime-v1 \
        --root /out \
        containerd-shim-wasmtime
    FROM scratch
    COPY --from=build /out/bin /
    EOF
    

    将二进制文件放入 PATH 上的一个目录中。

    $ mv ./containerd-shim-wasmtime-v1 /usr/local/bin
    

现在,您可以运行使用 Wasmtime 作为运行时的容器。

$ docker run --rm \
 --runtime io.containerd.wasmtime.v1 \
 --platform wasi/wasm32 \
 michaelirwin244/wasm-example
© . This site is unofficial and not affiliated with Kubernetes or Docker Inc.