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.9 では、docker ソケットはホストノードに表示されません。これは、カスタムビルドの mount docker socket オプションがカスタムビルドイメージ内で使用できる docker ソケットを提供しない可能性がゼロではないことを意味します。
イメージのビルドおよびプッシュにこの機能を必要とする場合、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 を使用してカスタムストラテジーで使用するカスタムビルダーイメージをビルドし、プッシュすることができます。
前提条件
- 新規カスタムビルダーイメージの作成に使用されるすべての入力を定義します。
手順
カスタムビルダーイメージをビルドする
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
オブジェクトを定義し、カスタムビルドロジックを実行することができます。
前提条件
- 新規カスタムビルダーイメージに必要なすべての入力を定義します。
- カスタムビルダーイメージをビルドします。
手順
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: {}
imagestream を作成します。
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 image stream
にプッシュします。