2.2. S2I スクリプトを使用した Dockerfile からのアプリケーションイメージの構築

Red Hat Software Collections コンテナーイメージをビルダーイメージとして使用し、ビルダーイメージに含まれる assemble および run S2I スクリプトを使用して、Dockerfile からアプリケーションイメージをビルドできます。assemble および run S2I スクリプトの詳細は、「ビルダーイメージとしての Red Hat Software Collections コンテナーイメージ」 を参照してください。

S2I スクリプトを使用して Dockerfile からアプリケーションイメージを作成するには、以下の手順に従います。

  1. コンテナーレジストリーにログインします。

    # podman login registry.redhat.io
  2. ビルダーイメージをプルします。

    # podman pull registry.redhat.io/rhscl_image_name
  3. アプリケーションコードを準備します。
  4. アプリケーションイメージのカスタム Dockerfile を作成し、以下を実行します。

    1. 以下の行を使用してビルダーイメージを定義します。

      FROM registry.redhat.io/rhscl_image_name
    2. src/ ディレクトリーのアプリケーションソースをコンテナーに配置して、デフォルトのコンテナーユーザーがソースにアクセスするのに十分なパーミッションが指定されていることを確認します。

      ADD --chown=1001:0 src /tmp/src
    3. /usr/libexec/s2i/assemble スクリプトを使用して依存関係をインストールします。

      RUN /usr/libexec/s2i/assemble
    4. 作成されたイメージに、/usr/libexec/s2i/run スクリプトを使用して、デフォルトのコマンドを設定します。

      CMD /usr/libexec/s2i/run
  5. podman を使用してアプリケーションイメージをビルドします。

    # podman build -t application_image_name .
  6. podman を使用してアプリケーションイメージを実行します。たとえば、アプリケーションイメージ内でインタラクティブシェルを起動するには、以下を実行します。

    # podman run -ti application_image_name /bin/bash -l

例2.2 S2I スクリプトを使用した Dockerfile からの Python 3.8 アプリケーションイメージの作成

この例は、ビルダーイメージによって提供される S2I スクリプトで Dockerfile から Python 3.8 アプリケーションをビルドし、実行する方法を示しています。

  1. コンテナーレジストリーにログインします。

    # podman login registry.redhat.io
  2. ビルダーイメージをプルします。

    # podman pull registry.redhat.io/rhscl/python-38-rhel7
  3. 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 で入手できる例を使用します。

  4. 以下の内容で 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
  5. 前の手順で準備した Dockerfile から新規イメージを構築します。

    # podman build -t python-app .
  6. 作成されたイメージを Python アプリケーションで実行します。

    # podman run -d python-app

関連資料