> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/handbook/di-18-zhang-oci-rong-qi/18.13.-gou-jian-zi-ding-yi-jing-xiang.md).

# 18.13.构建自定义镜像

可以直接从压缩包（例如 [poudriere(8)](https://man.freebsd.org/cgi/man.cgi?query=poudriere\&sektion=8\&format=html) 生成的压缩包或官方发行版压缩包）、从已有的带标签镜像构建自定义镜像，也可以通过 [pkg(8)](https://man.freebsd.org/cgi/man.cgi?query=pkg\&sektion=8\&format=html) 引导生成。

## 18.13.1. 从压缩包构建

使用 [podman-import(1)](https://man.freebsd.org/cgi/man.cgi?query=podman-import\&sektion=1\&format=html) 可以从任意 FreeBSD 根文件系统压缩包制作自定义镜像。压缩包可以是官方发行的 `base.txz`、由 [poudriere-image(8)](https://man.freebsd.org/cgi/man.cgi?query=poudriere-image\&sektion=8\&format=html) 生成的压缩包，也可以是你自己的定制压缩包。最终得到的带标签镜像可以推送到注册表、继续修改，或用于进一步创建镜像。

```sh
# podman import --os freebsd \
    --arch amd64 \
    --message 'Import FreeBSD 15.1-RELEASE base.txz' \
    https://download.freebsd.org/releases/amd64/15.1-RELEASE/base.txz
Downloading from "https://download.freebsd.org/releases/amd64/15.1-RELEASE/base.txz"
Getting image source signatures
Copying blob 3768988b151c done   |
Copying config 332bec83a4 done   |
Writing manifest to image destination
sha256:332bec83a428efa6c9f7d1f8b3fbd589c5613d938dca7da5125240c89f4f3dcd

# podman image tag 332bec83a428 localhost/freebsd-base:15.1

# podman image ls
REPOSITORY              TAG   IMAGE ID      CREATED        SIZE
localhost/freebsd-base  15.1  332bec83a428  5 minutes ago  715 MB
```

## 18.13.2. 从 Containerfile 构建

可以使用 `Containerfile` 构建镜像，它与 Dockerfile 类似。在大多数情况下，`freebsd-notoolchain` 镜像是最方便的基础镜像，因为它包含几乎所有标准系统工具。若需更精简的容器，可根据情况从 `freebsd-static`、`freebsd-dynamic` 或 `freebsd-runtime` 起步，只添加需要的内容：

```sh
# Containerfile
FROM ghcr.io/freebsd/freebsd-notoolchain:15.1
RUN env ASSUME_ALWAYS_YES=yes IGNORE_OS_VERSION=yes pkg bootstrap -r FreeBSD
RUN pkg update
RUN pkg upgrade -y
RUN pkg clean -y
ENTRYPOINT /bin/sh
```

使用 [podman-build(1)](https://man.freebsd.org/cgi/man.cgi?query=podman-build\&sektion=1\&format=html) 构建镜像：

```sh
# podman build --no-hosts --squash \
    -t localhost/freebsd-base:15.1 \
    -f ./Containerfile
```

还可以在其上叠加更多镜像：

```sh
# Containerfile.webserver
FROM localhost/freebsd-base:15.1
RUN pkg install -y lang/python3
RUN pkg clean -y
ENTRYPOINT /usr/local/bin/python3 -m http.server
```

```sh
# podman build --no-hosts \
    -t localhost/python-www:15.1 \
    -f ./Containerfile.webserver
```

使用 `--detach` 在后台运行 Web 服务器，并用 `-p` 将主机端口映射到容器端口。本例中，容器监听其默认端口 8000，该端口映射到主机的 8888 端口：

```sh
# c=$(podman run --detach --rm -p 8888:8000 localhost/python-www:15.1)
```

端口映射使用 [pf(4)](https://man.freebsd.org/cgi/man.cgi?query=pf\&sektion=4\&format=html) 重定向规则。这些规则只重定向从网络到达的流量，因此必须从另一台主机、或通过主机面向网络的 IP 地址访问映射的端口：

```sh
$ fetch -v http://172.16.2.10:8888/
resolving server address: 172.16.2.10:8888
requesting http://172.16.2.10:8888/
remote size / mtime: 784 / 0
fetch.out                                              784  B 3791 kBps    00s
```

要从主机直接访问容器，请使用容器自身的 IP 地址及其监听端口：

```sh
# podman inspect -f '{{.NetworkSettings.IPAddress}}' $c
10.88.0.91
# fetch -v http://10.88.0.91:8000/
resolving server address: 10.88.0.91:8000
requesting http://10.88.0.91:8000/
remote size / mtime: 784 / 0
fetch.out                                              784  B   25 MBps    00s
```

> **注意**
>
> 通过 `-p` 进行的端口映射由 [pf(4)](https://man.freebsd.org/cgi/man.cgi?query=pf\&sektion=4\&format=html) 重定向规则处理，仅对从网络到达的流量生效。来自 jail 主机本机、目标为 `localhost` 或 **127.0.0.1** 的流量不会被重定向。在主机上请直接使用容器的 IP 地址。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://book.bsdcn.org/handbook/di-18-zhang-oci-rong-qi/18.13.-gou-jian-zi-ding-yi-jing-xiang.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
