2.9. 额外的自定义构建步骤
您可以在定义文件的 additional_build_steps 部分中为任何构建阶段指定自定义构建命令。这允许对构建阶段进行精细的控制。
使用 prepend_ 和 append_ 命令在执行主构建步骤之前或之后运行的 Containerfile 中添加指令。命令必须符合运行时系统所需的任何规则。
下表列出了 additional_build_steps 中可以使用的值:
| 值 | Description |
|---|---|
|
| 您可以在构建基础镜像前插入命令。 |
|
| 您可以在构建基础镜像后插入命令。 |
|
| 您可以在构建 galaxy 镜像前插入命令。 |
|
| 您可以在构建 galaxy 镜像后插入命令。 |
|
| 您可以在构建 Python 构建器镜像前插入命令。 |
|
| 您可以在构建 Python 构建器镜像后插入命令。 |
|
| 您可以在构建最终镜像前插入命令。 |
|
| 您可以在构建最终镜像后插入命令。 |
additional_build_steps 的语法支持多行字符串和列表。请参见以下示例:
例 2.1. 一个多行的字符串条目
prepend_final: |
RUN whoami
RUN cat /etc/os-release
例 2.2. 一个列表条目
append_final:
- RUN echo This is a post-install command!
- RUN ls -la /etc
例 2.3. 将任意文件复制到执行环境
additional_build_files:
# copy arbitrary files next to this EE def into the build context - we can refer to them later...
- src: files/rootCA.crt
dest: configs
additional_build_steps:
prepend_base:
# copy a custom CA cert into the base image and recompute the trust database
# because this is in "base", all stages will inherit (including the final EE)
- COPY _build/configs/rootCA.crt /usr/share/pki/ca-trust-source/anchors
- RUN update-ca-trust
additional_build_files 部分可让您将 rootCA.crt 添加到构建上下文目录中。将此文件复制到构建上下文目录时,可在构建过程中使用它。要使用该文件,请使用 additional_build_steps 部分的 prepend_base 步骤中指定的 COPY 指令从构建上下文目录中复制它。您可以基于复制的文件执行任何操作,例如在本示例中通过运行 RUN update-ca-trust 更新 CA 证书的动态配置。
2.9.1. 使用环境变量构建执行环境 复制链接链接已复制到粘贴板!
以下示例文件指定了构建过程可能需要的环境变量。
要实现此功能,它会在 additional_build_steps 部分的 prepend_base 步骤中使用 ENV 变量定义。
-
additional_build_steps:
prepend_base:
- ENV FOO=bar
- RUN echo $FOO > /tmp/file1.txt
构建过程的后续阶段可以使用相同的环境变量。