×

在 OpenShift Container Platform 4.17 中,主机节点上将不存在 docker socket。这意味着自定义构建的 *挂载 docker socket* 选项不能保证提供可访问的 docker socket 用于自定义构建镜像中。

如果您需要此功能才能构建和推送镜像,请将 Buildah 工具添加到您的自定义构建镜像中,并使用它在您的自定义构建逻辑中构建和推送镜像。以下是使用 Buildah 运行自定义构建的示例。

使用自定义构建策略需要普通用户默认情况下不具备的权限,因为它允许用户在集群上运行的特权容器内执行任意代码。此访问级别可用于破坏集群,因此应仅授予那些拥有集群管理员权限的可信用户。

先决条件

创建自定义构建工件

您必须创建要用作自定义构建镜像的镜像。

步骤
  1. 从空目录开始,创建一个名为 Dockerfile 的文件,内容如下:

    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"]
  2. 在同一目录下,创建一个名为 dockerfile.sample 的文件。此文件包含在自定义构建镜像中,并定义由自定义构建生成的镜像。

    FROM registry.access.redhat.com/ubi9/ubi
    RUN touch /tmp/build
  3. 在同一目录下,创建一个名为 build.sh 的文件。此文件包含自定义构建运行时执行的逻辑。

    #!/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}

构建自定义构建器镜像

您可以使用 OpenShift Container Platform 构建和推送自定义构建器镜像以在自定义策略中使用。

先决条件
  • 定义创建新的自定义构建器镜像的所有输入。

步骤
  1. 定义一个 BuildConfig 对象,它将构建您的自定义构建器镜像。

    $ oc new-build --binary --strategy=docker --name custom-builder-image
  2. 在创建自定义构建镜像的目录中运行构建。

    $ oc start-build custom-builder-image --from-dir . -F

    构建完成后,新的自定义构建器镜像将在您的项目中以名为 custom-builder-image:latest 的镜像流标签提供。

使用自定义构建器镜像

您可以定义一个 BuildConfig 对象,该对象将自定义策略与您的自定义构建器镜像结合使用以执行您的自定义构建逻辑。

先决条件
  • 定义新的自定义构建器镜像所需的所有输入。

  • 构建您的自定义构建器镜像。

步骤
  1. 创建一个名为buildconfig.yaml的文件。此文件定义了在您的项目中创建并执行的BuildConfig对象。

    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 指定您的项目名称。
  2. 输入以下命令创建BuildConfig对象:

    $ oc create -f buildconfig.yaml
  3. 创建一个名为imagestream.yaml的文件。此文件定义了构建将向其推送镜像的镜像流。

    kind: ImageStream
    apiVersion: image.openshift.io/v1
    metadata:
      name: sample-custom
    spec: {}
  4. 输入以下命令创建镜像流:

    $ oc create -f imagestream.yaml
  5. 输入以下命令运行您的自定义构建:

    $ oc start-build sample-custom-build -F

    构建运行时,它会启动一个运行前面构建的自定义构建器镜像的 Pod。该 Pod 运行定义为自定义构建器镜像入口点的build.sh逻辑。build.sh逻辑调用 Buildah 来构建嵌入在自定义构建器镜像中的dockerfile.sample,然后使用 Buildah 将新镜像推送到sample-custom镜像流