> For the complete documentation index, see [llms.txt](https://handbook.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://handbook.bsdcn.org/di-18-zhang-oci-rong-qi/18.10.-dao-ru-bing-yun-hang-rong-qi.md).

# 18.10.导入并运行容器

> **注意**
>
> 目前，FreeBSD 上的所有 Podman 容器都必须以 root 身份运行，这是 FreeBSD jail 的要求。

在必要的工具和防火墙规则就位后，就可以从 [FreeBSD Releases](https://download.freebsd.org/releases/OCI-IMAGES/) 获取官方发布的镜像，并根据情况选择 [aarch64](https://download.freebsd.org/releases/OCI-IMAGES/15.1-RELEASE/aarch64/Latest/) 或 [amd64](https://download.freebsd.org/releases/OCI-IMAGES/15.1-RELEASE/amd64/Latest/)。

## 18.10.1. 导入 FreeBSD OCI 镜像

如上所述，虽然直接从公共容器注册表拉取镜像最为方便，但为了获得最强的信任链，请直接从 [FreeBSD.org](https://download.freebsd.org/releases) 下载镜像，并根据 PGP 签名的发行公告校验校验和。

```sh
# export OCIBASE=https://download.freebsd.org/releases/OCI-IMAGES/15.1-RELEASE/amd64/Latest
# podman load -i=$OCIBASE/FreeBSD-15.1-RELEASE-amd64-container-image-static.txz
Getting image source signatures
Copying blob 44defc6cee2e done   |
Copying config 5921c62e76 done   |
Writing manifest to image destination
Loaded image: localhost/freebsd-static:15.1-RELEASE-amd64

# podman load -i=$OCIBASE/FreeBSD-15.1-RELEASE-amd64-container-image-dynamic.txz
Getting image source signatures
Copying blob 6054dffc3887 done   |
Copying config 33427d59f9 done   |
Writing manifest to image destination
Loaded image: localhost/freebsd-dynamic:15.1-RELEASE-amd64

# podman load -i=$OCIBASE/FreeBSD-15.1-RELEASE-amd64-container-image-runtime.txz
Getting image source signatures
Copying blob dcbbfcb598fe done   |
Copying config 90c4936754 done   |
Writing manifest to image destination
Loaded image: localhost/freebsd-runtime:15.1-RELEASE-amd64

# podman load -i=$OCIBASE/FreeBSD-15.1-RELEASE-amd64-container-image-notoolchain.txz
Getting image source signatures
Copying blob a16c94176cc3 done   |
Copying config 086572f35c done   |
Writing manifest to image destination
Loaded image: localhost/freebsd-notoolchain:15.1-RELEASE-amd64

# podman load -i=$OCIBASE/FreeBSD-15.1-RELEASE-amd64-container-image-toolchain.txz
Getting image source signatures
Copying blob b04531b7d93d done   |
Copying config b927c11b2e done   |
Writing manifest to image destination
Loaded image: localhost/freebsd-toolchain:15.1-RELEASE-amd64
```

## 18.10.2. 列出镜像

```sh
# podman images
REPOSITORY                    TAG                   IMAGE ID      CREATED       SIZE
localhost/freebsd-toolchain   15.1-RELEASE-amd64  b927c11b2e8d  13 days ago   660 MB
localhost/freebsd-notoolchain 15.1-RELEASE-amd64  086572f35cc9  13 days ago   152 MB
localhost/freebsd-runtime     15.1-RELEASE-amd64  90c493675429  13 days ago   34.1 MB
localhost/freebsd-dynamic     15.1-RELEASE-amd64  33427d59f990  13 days ago   14.5 MB
localhost/freebsd-static      15.1-RELEASE-amd64  5921c62e76c1  13 days ago   2.92 MB
```

注意，镜像 ID 每次都完全可复现，且 IMAGE ID 列与导入镜像时报告的哈希值一致。

## 18.10.3. 列出层

可以显示构成镜像的各层，由此可以看出 runtime 镜像由三个部分组成，每个部分本身即是一层：

```sh
# podman image tree ghcr.io/freebsd/freebsd-runtime:14.2
Image ID: c5f3e77557a9
Tags:     [ghcr.io/freebsd/freebsd-runtime:14.2]
Size:     35.07MB
Image Layers
├── ID: cd53fb07fb66 Size: 5.449MB Top Layer of: [ghcr.io/freebsd/freebsd-static:14.2]
├── ID: a01d37f7777b Size:  10.4MB Top Layer of: [ghcr.io/freebsd/freebsd-dynamic:14.2]
└── ID: 36b0c80ca1f7 Size: 19.21MB Top Layer of: [ghcr.io/freebsd/freebsd-runtime:14.2]
```

## 18.10.4. 使用公共注册表

虽然最安全的来源是从[官方 FreeBSD 发行版](https://download.freebsd.org/releases)下载，但还有两个由 FreeBSD 发行团队和集群管理员团队管理的公共容器注册表。两者提供的镜像完全相同，但工作流更简单，也更贴合容器的使用方式。

* [Docker Hub](https://hub.docker.com/u/freebsd)
* [GitHub Container Registry](https://github.com/orgs/freebsd/packages)

## 18.10.5. 获取容器

可以使用 `podman pull` 从公共注册表获取镜像：

```sh
# podman pull ghcr.io/freebsd/freebsd-notoolchain:15.1
Trying to pull ghcr.io/freebsd/freebsd-notoolchain:15.1...
Getting image source signatures
Copying blob 711cf0d3c0ef done   |
Copying config 086572f35c done   |
Writing manifest to image destination
086572f35cc90b1a1d2b7904b63e8a849071b4ba89b15492f6da47a802372aa2
```

## 18.10.6. 运行容器

使用 `podman run` 启动容器。`-it` 选项分配一个交互式终端，`--rm` 在容器退出时将其删除。如果未指定命令，则使用镜像的默认入口：

```sh
# podman run -it --rm ghcr.io/freebsd/freebsd-notoolchain:15.1
# exit
```

也可以指定具体的命令：

```sh
# podman run -it --rm ghcr.io/freebsd/freebsd-notoolchain:15.1 uname -a
FreeBSD c0038de70651 15.1-RELEASE FreeBSD 15.1-RELEASE releng/15.1-n283562-96841ea08dcf GENERIC amd64
```

由于容器镜像不包含内核，[freebsd-version(1)](https://man.freebsd.org/cgi/man.cgi?query=freebsd-version\&sektion=1\&format=html) 对运行中的内核与已安装的用户空间会显示不同的结果。本例中，15.0-RELEASE 容器运行在 15.1-RELEASE 主机上：

```sh
# podman run -it --rm ghcr.io/freebsd/freebsd-notoolchain:15.0 /bin/sh
# freebsd-version -ru
15.1-RELEASE
15.0-RELEASE
# exit
```
