> 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.14.-gao-ji-yong-fa.md).

# 18.14.高级用法

## 18.14.1. Podman 守护化

FreeBSD 的 Podman 实现提供两个 [rc(8)](https://man.freebsd.org/cgi/man.cgi?query=rc\&sektion=8\&format=html) 服务：

* `podman` 服务在系统引导时运行，确保所有标记为 `restart-policy=always` 的容器都会启动。
* `podman_service` 服务运行 [Podman API 服务](https://docs.podman.io/en/latest/_static/api.html)，提供 REST API，默认监听 **/var/run/podman/podman.sock**。

按如下方式启用：

```sh
# sysrc podman_enable=YES
# sysrc podman_service_enable=YES
# sysrc podman_service_flags='--time 0'
# service podman start
# service podman_service start
```

主机重启后需要自动重启的容器必须使用 `--restart=always` 选项运行：

```sh
# podman run --detach --restart=always my-image
```

## 18.14.2. 运行 Linux 容器

可以借助 FreeBSD 的 Linux 二进制兼容层运行许多 Linux 容器镜像：

```sh
# service linux onestart
# podman run --rm --os=linux docker.io/alpine cat /etc/os-release | head -1
Trying to pull docker.io/library/alpine:latest...
Getting image source signatures
Copying blob 4abcf2066143 done   |
Copying config 05455a0888 done   |
Writing manifest to image destination
NAME="Alpine Linux"
```

> **注意**
>
> 依赖 systemd、要求自身为 PID 1、或依赖 Linux 特有内核接口的 Linux 容器，在 FreeBSD 的 Linux 二进制兼容层下可能无法正常工作。

## 18.14.3. 运行本地注册表

注册表是用于存储和共享 OCI 镜像的 Web 服务器。[sysutils/docker-registry](https://freshports.org/sysutils/docker-registry) Port 提供了一个简单的注册表，适合本地开发或小型组织使用。[Zot Registry](https://zotregistry.dev/) 是功能更全面的现代替代品。

```sh
# pkg install -r FreeBSD sysutils/docker-registry
# zfs create zroot/var/db/registry
# chown -R www:www /var/db/registry
# sysrc docker_registry_enable=YES
# sysrc docker_registry_user=www
# sed -i '' -E -e 's,rootdirectory:.+,rootdirectory: /var/db/registry,' \
    /usr/local/etc/docker-registry/config.yml
# touch /usr/local/etc/docker-registry/htpasswd
# chown -R root:www /usr/local/etc/docker-registry/config.yml \
    /usr/local/etc/docker-registry/htpasswd
# chmod 0640 /usr/local/etc/docker-registry/config.yml \
    /usr/local/etc/docker-registry/htpasswd
```

**/usr/local/etc/docker-registry/config.yml** 的示例配置：

```yaml
version: 0.1
log:
  fields:
    service: registry
storage:
  cache:
    blobdescriptor: inmemory
  filesystem:
    rootdirectory: /var/db/registry
http:
  addr: :5000
  secret: <random-secret>
  headers:
    X-Content-Type-Options: [nosniff]
auth:
  htpasswd:
    realm: basic-realm
    path: /usr/local/etc/docker-registry/htpasswd
health:
  storagedriver:
    enabled: true
    interval: 10s
    threshold: 3
```

启动注册表并验证其正在运行：

```sh
# service registry start
# curl -u admin:passwd http://localhost:5000/v2/
```

> **技巧**
>
> 建议通过 TLS 提供注册表服务，将其置于 nginx 或 haproxy 等反向代理之后。

## 18.14.4. 同步注册表

可以使用 `skopeo` 将官方 FreeBSD 镜像同步到本地注册表。下面的示例将 [Docker Hub](https://hub.docker.com/u/freebsd) 上所有与 FreeBSD 相关的镜像复制到本地注册表：

```yaml
# sync.yml
docker.io:
  images:
    freebsd/freebsd-static: []
    freebsd/freebsd-dynamic: []
    freebsd/freebsd-runtime: []
```

```sh
$ skopeo sync --src yaml --dest docker --all sync.yml cr.example.net/freebsd
INFO[0000] Processing repo                               repo=freebsd/freebsd-static
INFO[0000] Querying registry for image tags              repo=freebsd/freebsd-static
...
INFO[0771] Synced 42 images from 3 sources
```

只要 **/root/.config/containers/auth.json** 中的凭据是最新的，同步镜像就不需要 root 权限。

## 18.14.5. 使用容器注册表

注册表通常要求推送操作进行身份验证，拉取操作可选。登录后，凭据默认保存在 **/root/.config/containers/auth.json**：

```sh
# podman login ghcr.io -u username
Password:
Login Succeeded!
```

然后就可以将镜像推送到注册表：

```sh
# podman push localhost/freebsd-runtime:15.1 \
    docker://ghcr.io/example/freebsd-runtime:15.1
Getting image source signatures
Copying blob bbacfdfbe140 done   |
Copying config 786f2592a8 done   |
Writing manifest to image destination
```

公共镜像无需身份验证即可拉取：

```sh
# podman pull ghcr.io/freebsd/freebsd-runtime:15.1
```
