2.2. S2I スクリプトを使用した Dockerfile からのアプリケーションイメージの構築
Red Hat Software Collections コンテナーイメージをビルダーイメージとして使用し、ビルダーイメージに含まれる assemble
および run
S2I スクリプトを使用して、Dockerfile からアプリケーションイメージをビルドできます。assemble
および run
S2I スクリプトの詳細は、「ビルダーイメージとしての Red Hat Software Collections コンテナーイメージ」 を参照してください。
S2I スクリプトを使用して Dockerfile からアプリケーションイメージを作成するには、以下の手順に従います。
コンテナーレジストリーにログインします。
# podman login registry.redhat.io
ビルダーイメージをプルします。
# podman pull registry.redhat.io/rhscl_image_name
- アプリケーションコードを準備します。
アプリケーションイメージのカスタム Dockerfile を作成し、以下を実行します。
以下の行を使用してビルダーイメージを定義します。
FROM registry.redhat.io/rhscl_image_name
src/
ディレクトリーのアプリケーションソースをコンテナーに配置して、デフォルトのコンテナーユーザーがソースにアクセスするのに十分なパーミッションが指定されていることを確認します。ADD --chown=1001:0 src /tmp/src
/usr/libexec/s2i/assemble
スクリプトを使用して依存関係をインストールします。RUN /usr/libexec/s2i/assemble
作成されたイメージに、
/usr/libexec/s2i/run
スクリプトを使用して、デフォルトのコマンドを設定します。CMD /usr/libexec/s2i/run
podman を使用してアプリケーションイメージをビルドします。
# podman build -t application_image_name .
podman を使用してアプリケーションイメージを実行します。たとえば、アプリケーションイメージ内でインタラクティブシェルを起動するには、以下を実行します。
# podman run -ti application_image_name /bin/bash -l
例2.2 S2I スクリプトを使用した Dockerfile からの Python 3.8 アプリケーションイメージの作成
この例は、ビルダーイメージによって提供される S2I スクリプトで Dockerfile から Python 3.8 アプリケーションをビルドし、実行する方法を示しています。
コンテナーレジストリーにログインします。
# podman login registry.redhat.io
ビルダーイメージをプルします。
# podman pull registry.redhat.io/rhscl/python-38-rhel7
https://github.com/sclorg/django-ex.git から入手できるアプリケーションコードをプルします。
$ git clone https://github.com/sclorg/django-ex.git app-src
または https://github.com/sclorg/s2i-python-container/tree/master/examples で入手できる例を使用します。
以下の内容で Dockerfile を作成します。
FROM registry.redhat.io/rhscl/python-38-rhel7 # Add application sources to a directory that the assemble script expects them # and set permissions so that the container runs without root access USER 0 ADD app-src /tmp/src RUN chown -R 1001:0 /tmp/src USER 1001 # Install the dependencies RUN /usr/libexec/s2i/assemble # Set the default command for the resulting image CMD /usr/libexec/s2i/run
前の手順で準備した Dockerfile から新規イメージを構築します。
# podman build -t python-app .
作成されたイメージを Python アプリケーションで実行します。
# podman run -d python-app
関連資料
- Dockerfile からのイメージの構築
- Dockerfile リファレンスドキュメント
-
イメージの
/help.1
ファイルまたは、アップストリームの GitHub リポジトリー にある、該当のビルダーイメージの README ファイルの Environment variables for Source-to-Image セクション - 環境変数は、Red Hat Ecosystem Catalog のイメージの詳細な説明にも記載されています。