> 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.11.-shi-yong-bian-ti-yu-ji-qiao.md).

# 18.11.实用变体与技巧

`podman run ...` 的几乎所有参数同样适用于 `podman build`。详情请参阅 [podman-run(1)](https://man.freebsd.org/cgi/man.cgi?query=podman-run\&sektion=1\&format=html) 与 [podman-build(1)](https://man.freebsd.org/cgi/man.cgi?query=podman-build\&sektion=1\&format=html) 手册页。

使用 `--rm` 选项可以让临时容器在结束后自行清理。

使用 `podman images -a` 显示所有已下载的镜像：

```sh
# podman images -a
REPOSITORY                       TAG            IMAGE ID      CREATED      SIZE
ghcr.io/freebsd/freebsd-runtime  15.1           90c493675429  2 weeks ago  34.1 MB
```

使用 `podman ps -a` 查看运行中的容器：

```sh
# podman ps -a
CONTAINER ID  IMAGE                                          COMMAND  CREATED        STATUS        NAMES
3123623bef9b  ghcr.io/freebsd/freebsd-runtime:15.1           /bin/sh  4 minutes ago  Up 4 minutes  admiring_brattain
```

## 18.11.1. 环境变量

Podman 会在每个容器中设置若干默认环境变量：

```sh
# podman run -it --rm ghcr.io/freebsd/freebsd-notoolchain:15.1 /usr/bin/env
container=podman
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOME=/root
HOSTNAME=60bae3343e6b
```

`HOSTNAME` 默认设置为容器 ID。可以用 `--hostname` 覆盖它，当容器内的应用程序需要稳定或有意义的主机名时很有用。

可以用 `--env` 传入任意环境变量：

```sh
# podman run --env FOO=BAR -it --rm ghcr.io/freebsd/freebsd-runtime:15.1
# echo $FOO
BAR
```

## 18.11.2. 挂载文件与卷

文件和卷都可以用 `--volume` 传入容器。例如，runtime 镜像不包含 [which(1)](https://man.freebsd.org/cgi/man.cgi?query=which\&sektion=1\&format=html)：

```sh
# podman run -it --rm ghcr.io/freebsd/freebsd-runtime:15.1
# which
/bin/sh: which: not found
```

可以将它从主机挂载到容器中：

```sh
# podman run -it --rm --volume /usr/bin/which:/usr/bin/which \
    ghcr.io/freebsd/freebsd-runtime:15.1
# which which
/usr/bin/which
```

对于 `freebsd-static` 这类精简镜像，可以从主机挂载现成的 [pkg-static(8)](https://man.freebsd.org/cgi/man.cgi?query=pkg-static\&sektion=8\&format=html)，在容器内引导 [pkg(8)](https://man.freebsd.org/cgi/man.cgi?query=pkg\&sektion=8\&format=html)。通过 `--volume /var/cache/pkg` 共享主机的包缓存，可以避免重新下载已获取过的软件包，在构建多个容器时尤其有用。`--no-hosts` 选项可避免挂载 **/etc/hosts**，否则它会干扰软件包升级。

> **注意**
>
> 只有主机与容器运行相同的 FreeBSD 主版本时，共享包缓存才有效，因为软件包是特定于版本的。

```sh
# podman run -it --rm \
    --env ASSUME_ALWAYS_YES=true \
    --no-hosts \
    --volume /usr/local/sbin/pkg-static:/bin/pkg-static \
    --volume /var/cache/pkg \
    ghcr.io/freebsd/freebsd-static:15.1
# pkg-static bootstrap -r FreeBSD
Bootstrapping pkg from pkg+https://pkg.FreeBSD.org/FreeBSD:14:amd64/latest, please wait...
Verifying signature with trusted certificate pkg.freebsd.org.2013102301... done
[fd343e182fa5] Installing pkg-2.1.2...
[fd343e182fa5] Extracting pkg-2.1.2: 100%
```

主机上的文件和目录也可以挂载到容器中：

```sh
# podman run -it --rm \
    --volume /etc/fstab:/etc/fstab \
    --volume /tmp:/var/tmp \
    --volume /tmp \
    ghcr.io/freebsd/freebsd-runtime:15.1
```

## 18.11.3. 检查镜像内容

要列出任意镜像中的所有文件（包括没有 shell 或工具的极简镜像），可以导出容器文件系统并列出其内容：

```sh
# podman export $(podman create --rm ghcr.io/freebsd/freebsd-static:15.1 /) | tar tf -
```

容器镜像本身不包含 [pkg(8)](https://man.freebsd.org/cgi/man.cgi?query=pkg\&sektion=8\&format=html)，但可以从主机挂载 [pkg-static(8)](https://man.freebsd.org/cgi/man.cgi?query=pkg-static\&sektion=8\&format=html) 来查询已安装的基本系统软件包。使用 `pkg info -q` 列出软件包名称，或用 `pkg info -ql` 显示软件包拥有的所有文件：

```sh
# podman run --rm \
    -v /usr/local/sbin/pkg-static:/pkg \
    ghcr.io/freebsd/freebsd-runtime:15.1 /pkg info -q
FreeBSD-audit-lib-15.1
FreeBSD-bzip2-lib-15.1
FreeBSD-certctl-15.1
FreeBSD-clibs-15.1
FreeBSD-fetch-15.1
FreeBSD-kerberos-lib-15.1
FreeBSD-libarchive-15.1
FreeBSD-libcasper-15.1
FreeBSD-libexecinfo-15.1
FreeBSD-libucl-15.1
FreeBSD-mtree-15.1
FreeBSD-ncurses-lib-15.1
FreeBSD-openssl-15.1
FreeBSD-openssl-lib-15.1
FreeBSD-pam-lib-15.1
FreeBSD-pkg-bootstrap-15.1
FreeBSD-rc-15.1
FreeBSD-runtime-15.1
FreeBSD-xz-lib-15.1
FreeBSD-zlib-15.1
FreeBSD-zoneinfo-15.1
FreeBSD-zstd-lib-15.1
```

当主机与容器运行不同的 FreeBSD 主版本时，[pkg(8)](https://man.freebsd.org/cgi/man.cgi?query=pkg\&sektion=8\&format=html) 会通过检查容器内的 **/bin/sh** 来检测 ABI，并就版本不匹配发出警告：

```sh
# podman run -it --rm \
    --env ASSUME_ALWAYS_YES=true \
    --no-hosts \
    --volume /usr/local/sbin/pkg-static:/usr/sbin/pkg \
    --volume /var/cache/pkg \
    ghcr.io/freebsd/freebsd-runtime:14.4 /bin/sh
# pkg bootstrap
pkg: Warning: Major OS version upgrade detected.  Running "pkg bootstrap -f" recommended
```

对于没有 **/bin/sh** 的镜像（如 `freebsd-dynamic`），[pkg-static(8)](https://man.freebsd.org/cgi/man.cgi?query=pkg-static\&sektion=8\&format=html) 完全无法检测系统 ABI。
