検索

2.6. Buildah によるカスタムイメージビルド

download PDF

OpenShift Container Platform 4.15 では、ホストノードに docker ソケットは存在しません。これは、カスタムビルドの mount docker socket オプションがカスタムビルドイメージ内で使用できる docker ソケットを提供しない可能性がゼロではないことを意味します。

イメージのビルドおよびプッシュにこの機能を必要とする場合、Buildah ツールをカスタムビルドイメージに追加し、これを使用してカスタムビルドロジック内でイメージをビルドし、プッシュします。以下の例は、Buildah でカスタムビルドを実行する方法を示しています。

注記

カスタムビルドストラテジーを使用するためには、デフォルトで標準ユーザーが持たないパーミッションが必要です。このパーミッションはユーザーがクラスターで実行される特権付きコンテナー内で任意のコードを実行することを許可します。このレベルのアクセスを使用するとクラスターが危険にさらされる可能性があるため、このアクセスはクラスターで管理者権限を持つ信頼されたユーザーのみに付与される必要があります。

2.6.1. 前提条件

2.6.2. カスタムビルドアーティファクトの作成

カスタムビルドイメージとして使用する必要のあるイメージを作成する必要があります。

手順

  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}

2.6.3. カスタムビルダーイメージのビルド

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 という名前のイメージストリームタグのプロジェクトで利用可能になります。

2.6.4. カスタムビルダーイメージの使用

カスタムビルダーイメージとカスタムストラテジーを併用する 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 image stream にプッシュします。

Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.