Nspawn 3.0: A Docker-compatible Engine API for systemd-nspawn containers

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Nspawn 3.0: A Docker-compatible Engine API for systemd-nspawn containers

Nspawn 3.0: A Docker-compatible Engine API for systemd-nspawn containers. Since 2.0.0, the machines that mount Docker’s socket were provided with a read-only Docker API, sufficient for tools like Traefik or Dozzle to discover them. Version 3.0.0 now serves this API, along with its updates, on the host as well.

Nspawn 3.0:用于 systemd-nspawn 容器的 Docker 兼容引擎 API。自 2.0.0 版本以来,挂载 Docker 套接字的机器获得了一个只读的 Docker API,足以让 Traefik 或 Dozzle 等工具发现它们。3.0.0 版本现在也在宿主机上提供该 API 及其更新。

In between: 2.1.0: Published ports now respond over IPv6, and -p accepts IPv6 addresses. 2.2.0: Fixes for issues encountered when migrating an entire server of compose projects to nspawn. Compose gained anonymous volumes, networks are recreated when definitions change, and “down SERVICE” now triggers a start that brings up service dependencies. 2.3.0: Every layer is applied inside the tree it is extracted into and verified against the digest its image names. 2.3.1: Packages available on apt.nspawn.org (Debian 13+, Ubuntu 24.04+) and rpm.nspawn.org (Fedora 44+). 2.4.0: -p PORT uses a free port on the host, and pulling an image index selects the entry matching the host’s platform.

在此期间:2.1.0 版本:发布的端口现在可以通过 IPv6 响应,且 -p 参数接受 IPv6 地址。2.2.0 版本:修复了将整个 compose 项目服务器迁移到 nspawn 时遇到的问题。Compose 增加了匿名卷,网络在定义变更时会重新创建,且“down SERVICE”现在会触发启动其依赖的服务。2.3.0 版本:每一层都在其提取到的树内应用,并根据其镜像名称的摘要进行校验。2.3.1 版本:软件包可在 apt.nspawn.org (Debian 13+, Ubuntu 24.04+) 和 rpm.nspawn.org (Fedora 44+) 上获取。2.4.0 版本:-p PORT 使用宿主机上的空闲端口,拉取镜像索引时会选择匹配宿主机平台的条目。

The Docker Engine API on the host: The host’s socket is /run/nspawn/api.sock, opened by nspawn-api.socket. Its primary purpose is compatibility: tools that manage containers via the Docker API, such as GitLab Runner’s docker executor, can use nspawn’s machines instead. While nspawn remains the command line for management, the docker command also functions against this socket.

宿主机上的 Docker 引擎 API:宿主机的套接字为 /run/nspawn/api.sock,由 nspawn-api.socket 打开。其主要目的是兼容性:通过 Docker API 管理容器的工具(如 GitLab Runner 的 docker 执行器)可以改用 nspawn 的机器。虽然 nspawn 仍然是用于管理的命令行工具,但 docker 命令也可以针对该套接字运行。

The packages ship with the socket disabled. Once it listens, nspawn-api.service starts upon the first connection: $ sudo systemctl enable --now nspawn-api.socket. With DOCKER_HOST pointing to it, the docker command communicates with nspawn: $ export DOCKER_HOST=unix:///run/nspawn/api.sock, followed by commands like docker run, docker exec, docker cp, and docker rm.

软件包默认不启用该套接字。一旦开始监听,nspawn-api.service 会在首次连接时启动:$ sudo systemctl enable --now nspawn-api.socket。将 DOCKER_HOST 指向它后,docker 命令即可与 nspawn 通信:$ export DOCKER_HOST=unix:///run/nspawn/api.sock,随后可执行 docker run、docker exec、docker cp 和 docker rm 等命令。

The API server acts as a client of the bus service, just like the command line. Every action is a method of org.nspawn, governed by the same polkit actions: reads and waits require org.nspawn.inspect, while logs and changes require org.nspawn.manage. Any user can connect, but polkit will never prompt, as a socket cannot interact with an authentication agent. Root has full access, while other users require a polkit rule, such as the example nspawn-wheel.rules provided with the packages. Similar to the docker group, a user with org.nspawn.manage permissions acts as root on the host, capable of running commands and mounting any host path.

API 服务器与命令行一样,是总线服务的客户端。其所有操作均为 org.nspawn 的方法,并受相同的 polkit 操作管理:读取和等待需要 org.nspawn.inspect,日志和变更需要 org.nspawn.manage。任何用户都可以连接,但 polkit 永远不会弹出提示,因为套接字无法与身份验证代理交互。Root 用户拥有完全访问权限,而其他用户需要 polkit 规则,例如软件包中提供的 nspawn-wheel.rules 示例。与 docker 组类似,拥有 org.nspawn.manage 权限的用户在宿主机上相当于 root,能够运行命令并挂载任何宿主机路径。

Resources held by a single socket’s clients (connections, log/event streams, waits, attaches, execs) are bounded. Non-root users receive a share of each, ensuring no single user can exclude others or the root user. Clients looking only at /run/docker.sock can find the API using a drop-in file on a host where that path is otherwise unused.

单个套接字客户端持有的资源(连接、日志和事件流、等待、附加、执行)是有限的。非 root 用户会获得每种资源的一部分,确保没有任何用户可以将其他人或 root 用户排除在外。仅查找 /run/docker.sock 的客户端可以在宿主机上通过插入文件(drop-in)找到该 API,前提是该路径在宿主机上未被占用。

What the API serves: It responds in the formats of API 1.51 and earlier versions down to 1.24 as requested by clients. It supports container operations (create, start, stop, restart, kill, pause, unpause, wait, update, remove, prune), attach (including input), exec (with terminal or separate stdout/stderr), and archive (HEAD, GET, PUT on /containers/ID/archive for docker cp functionality). Images, volumes, and networks are also supported with standard management operations.

API 服务内容:它以 API 1.51 及客户端请求的低至 1.24 的版本格式进行响应。它支持容器操作(创建、启动、停止、重启、终止、暂停、取消暂停、等待、更新、移除、清理)、附加(包括输入)、执行(带终端或分离的 stdout/stderr)以及归档(针对 /containers/ID/archive 的 HEAD、GET、PUT 操作,以实现 docker cp 功能)。镜像、卷和网络也支持标准的管理操作。

GitLab Runner: The GitLab Runner docker executor runs jobs on nspawn machines via the API by pointing its host setting to the socket. Variable values can now include line breaks.

GitLab Runner:GitLab Runner 的 docker 执行器通过将 host 设置指向该套接字,经由 API 在 nspawn 机器上运行作业。变量值现在可以包含换行符。

Breaking changes: A machine’s API socket now supports changes. In 2.0.0, the socket inside a machine was read-only. Now, a machine mounting /var/run/docker.sock, /run/docker.sock, or /run/nspawn/api.sock with read-write access can manage containers, images, volumes, and networks. With :ro, the socket is restricted to reading, following logs, and killing, with all other actions returning 403. Only root inside the machine can connect.

破坏性变更:机器的 API 套接字现在支持变更。在 2.0.0 版本中,机器内部的套接字是只读的。现在,以读写权限挂载 /var/run/docker.sock、/run/docker.sock 或 /run/nspawn/api.sock 的机器可以管理容器、镜像、卷和网络。使用 :ro 时,套接字仅限于读取、跟踪日志和终止操作,所有其他操作均返回 403。只有机器内部的 root 用户可以连接。

A record pulled or built that has never run is classified as an image, not a container. The API’s container list, inspect, and prune operations exclude these, which are instead listed via GET /images/json. The bus reports failures using error names under org.nspawn.Error, such as NotAuthorized, NoSuchMachine, or InUse.

已拉取或构建但从未运行的记录被归类为镜像,而非容器。API 的容器列表、检查和清理操作会排除这些记录,它们通过 GET /images/json 列出。总线使用 org.nspawn.Error 下的错误名称报告故障,例如 NotAuthorized、NoSuchMachine 或 InUse。

Other changes: “nspawn info” provides service version, machine states, images, directories, backend, log driver, registry, and host information. It supports JSON or template output and exits with 1 if the service is unresponsive. The API’s GET /info endpoint provides this data. “nspawn images inspect” is also available.

其他变更:“nspawn info”提供服务版本、机器状态、镜像、目录、后端、日志驱动、注册表和宿主机信息。它支持 JSON 或模板输出,如果服务无响应则退出代码为 1。API 的 GET /info 端点提供此数据。“nspawn images inspect”功能也已可用。