This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.2.6. 使用 Buildah 自定义镜像构建
在 OpenShift Container Platform 4.10 中,主机节点上没有 docker socket。这意味着,不能保证自定义构建的 mount docker socket 选项会提供可在自定义构建镜像中使用的可访问 docker socket。
如果您需要此功能来构建和推送镜像,请将 Buildah 工具添加到自定义构建镜像中,并在自定义构建逻辑中使用它来构建并推送镜像。以下是如何使用 Buildah 运行自定义构建的示例。
使用自定义构建策略需要普通用户默认情况下不具备的权限,因为它允许用户在集群上运行的特权容器内执行任意代码。此级别的访问权限可被用来进行可能对集群造成损害的操作,因此应仅授权给信任的用户。
2.6.1. 先决条件
- 查看如何授予自定义构建权限。
2.6.2. 创建自定义构建工件
您必须创建要用作自定义构建镜像的镜像。
流程
从空目录着手,使用以下内容创建名为
Dockerfile
的文件:Copy to Clipboard Copied! Toggle word wrap Toggle overflow FROM registry.redhat.io/rhel8/buildah # In this example, `/tmp/build` contains the inputs that build when this # custom builder image is run. Normally the custom builder image fetches # this content from some location at build time, by using git clone as an example. ADD dockerfile.sample /tmp/input/Dockerfile ADD build.sh /usr/bin RUN chmod a+x /usr/bin/build.sh # /usr/bin/build.sh contains the actual custom build logic that will be run when # this custom builder image is run. ENTRYPOINT ["/usr/bin/build.sh"]
FROM registry.redhat.io/rhel8/buildah # In this example, `/tmp/build` contains the inputs that build when this # custom builder image is run. Normally the custom builder image fetches # this content from some location at build time, by using git clone as an example. ADD dockerfile.sample /tmp/input/Dockerfile ADD build.sh /usr/bin RUN chmod a+x /usr/bin/build.sh # /usr/bin/build.sh contains the actual custom build logic that will be run when # this custom builder image is run. ENTRYPOINT ["/usr/bin/build.sh"]
在同一目录中,创建名为
dockerfile.sample
的文件。此文件将包含在自定义构建镜像中,并且定义将由自定义构建生成的镜像:Copy to Clipboard Copied! Toggle word wrap Toggle overflow FROM registry.access.redhat.com/ubi8/ubi RUN touch /tmp/build
FROM registry.access.redhat.com/ubi8/ubi RUN touch /tmp/build
在同一目录中,创建名为
build.sh
的文件。此文件包含自定义生成运行时将要执行的逻辑:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note that in this case the build inputs are part of the custom builder image, but normally this is retrieved from an external source. OUTPUT_REGISTRY and OUTPUT_IMAGE are env variables provided by the custom build framework performs the build of the new image defined by dockerfile.sample buildah requires a slight modification to the push secret provided by the service account to use it for pushing the image push the new image to the target for the build
#!/bin/sh # Note that in this case the build inputs are part of the custom builder image, but normally this # is retrieved from an external source. cd /tmp/input # OUTPUT_REGISTRY and OUTPUT_IMAGE are env variables provided by the custom # build framework TAG="${OUTPUT_REGISTRY}/${OUTPUT_IMAGE}" # performs the build of the new image defined by dockerfile.sample buildah --storage-driver vfs bud --isolation chroot -t ${TAG} . # buildah requires a slight modification to the push secret provided by the service # account to use it for pushing the image cp /var/run/secrets/openshift.io/push/.dockercfg /tmp (echo "{ \"auths\": " ; cat /var/run/secrets/openshift.io/push/.dockercfg ; echo "}") > /tmp/.dockercfg # push the new image to the target for the build buildah --storage-driver vfs push --tls-verify=false --authfile /tmp/.dockercfg ${TAG}
2.6.3. 构建自定义构建器镜像
您可以使用 OpenShift Container Platform 构建和推送要在 Custom 策略中使用的自定义构建器镜像。
先决条件
- 定义要用于创建新的自定义构建器镜像的所有输入。
流程
定义要用于构建自定义构建器镜像的
BuildConfig
对象:Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc new-build --binary --strategy=docker --name custom-builder-image
$ oc new-build --binary --strategy=docker --name custom-builder-image
从您在其中创建自定义构建镜像的目录中,运行构建:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc start-build custom-builder-image --from-dir . -F
$ oc start-build custom-builder-image --from-dir . -F
构建完成后,新自定义构建器镜像将在名为
custom-builder-image:latest
的镜像流标签中的项目内可用。
2.6.4. 使用自定义构建器镜像
您可以定义一个 BuildConfig
对象,它将结合使用 Custom 策略与自定义构建器镜像来执行您的自定义构建逻辑。
先决条件
- 为新自定义构建器镜像定义所有必要的输入。
- 构建您的自定义构建器镜像。
流程
创建名为
buildconfig.yaml
的文件。此文件定义要在项目中创建并执行的BuildConfig
对象:Copy to Clipboard Copied! Toggle word wrap Toggle overflow kind: BuildConfig apiVersion: build.openshift.io/v1 metadata: name: sample-custom-build labels: name: sample-custom-build annotations: template.alpha.openshift.io/wait-for-ready: 'true' spec: strategy: type: Custom customStrategy: forcePull: true from: kind: ImageStreamTag name: custom-builder-image:latest namespace: <yourproject> output: to: kind: ImageStreamTag name: sample-custom:latest
kind: BuildConfig apiVersion: build.openshift.io/v1 metadata: name: sample-custom-build labels: name: sample-custom-build annotations: template.alpha.openshift.io/wait-for-ready: 'true' spec: strategy: type: Custom customStrategy: forcePull: true from: kind: ImageStreamTag name: custom-builder-image:latest namespace: <yourproject>
1 output: to: kind: ImageStreamTag name: sample-custom:latest
- 1
- 指定项目的名称。
创建
BuildConfig
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc create -f buildconfig.yaml
$ oc create -f buildconfig.yaml
创建名为
imagestream.yaml
的文件。此文件定义构建要将镜像推送到的镜像流:Copy to Clipboard Copied! Toggle word wrap Toggle overflow kind: ImageStream apiVersion: image.openshift.io/v1 metadata: name: sample-custom spec: {}
kind: ImageStream apiVersion: image.openshift.io/v1 metadata: name: sample-custom spec: {}
创建镜像流:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc create -f imagestream.yaml
$ oc create -f imagestream.yaml
运行自定义构建:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc start-build sample-custom-build -F
$ oc start-build sample-custom-build -F
构建运行时,它会启动一个 Pod 来运行之前构建的自定义构建器镜像。该 Pod 将运行定义为自定义构建器镜像入口点的
build.sh
逻辑。build.sh
逻辑调用 Buildah 来构建自定义构建器镜像中嵌入的dockerfile.sample
,然后使用 Buildah 将新镜像推送到sample-custom 镜像流
。