- S2Iとは
- S2Iコマンドのインストール
- S2Iビルダーイメージのビルド
- アプリケーションイメージのS2Iビルド
S2Iとは
S2Iとは、Source To Imageの略で、OpenShiftの特徴的な機能の一つです。その名の通り、ソースコードをコンテナイメージに変換します。S2Iビルダーイメージとソースコードからアプリケーションイメージをビルドすることによって、ビルドを自動化するだけでなく、イメージとソースコードの保守を分離することができます。
この機能における具体的なメリットは、以下の公式ドキュメントをご参照ください。
https://access.redhat.com/documentation/ja-jp/openshift_container_platform/4.4/html/builds/understanding-image-builds#build-strategy-s2i_understanding-image-builds
本記事は、S2Iツールをローカルマシン(Mac)にインストールしてローカル上で使ってみます。
S2Iコマンドのインストール
S2IコマンドをインストールしてMac PC上で操作します。
S2Iツールのパッケージは以下のURLからダウンロードできますが、Macではbrewコマンドでインストールができます。
https://github.com/openshift/source-to-image/releases/tag/v1.3.0
以下のコマンドでインストールします。
$ brew install source-to-image $ s2i version s2i v1.3.0-dirty
S2Iビルダーイメージのビルド
S2Iコマンドを使ってS2Iビルダーイメージをビルドしてみます。
以下のコマンドで例のようにS2Iビルダーイメージの雛形を作成することができます。指定したdirectory配下には、必要なファイルが作成されます。それぞれのファイルの解説はまた今度記事を書きたいと思いますが、自身で作成したいイメージに合わせてファイルを編集する必要があります。
$ s2i create [image名] [directory名] 例) $ s2i create python:3.6 test-python $ tree test-python/ test-python/ ├── Dockerfile ├── Makefile ├── README.md ├── s2i │ └── bin │ ├── assemble │ ├── run │ ├── save-artifacts │ └── usage └── test ←テストソースコードなので、S2Iビルダーイメージのビルドには不要です。 ├── run └── test-app └── index.html $ docker build -t python-s2i-builder test-python/ $ docker images python-s2i-builder REPOSITORY TAG IMAGE ID CREATED SIZE python-s2i-builder latest 490710255550 3 minutes ago 426MB
今回は自分でS2Iビルダーイメージを作成するのではなく、以下のコマンドでregistry.redhat.ioのイメージを利用してアプリケーションイメージをS2Iビルドします(Red Hatのアカウントが必要になります。)。このように、OpenShiftではS2Iビルダーイメージをサポートしてくれるのが魅力の一つでもあります。
$ docker login registry.redhat.io Username: Password: Login Succeeded $ docker pull registry.redhat.io/rhel8/python-36 Using default tag: latest latest: Pulling from rhel8/python-36 1a6747857d79: Pull complete fc5aa93e3b58: Pull complete dd09aac02f79: Pull complete 8542dbe7d6e8: Pull complete ad98eb9d602e: Pull complete Digest: sha256:74057ed15aba3ba78ddf061a964b60a4e5c1d0eb8c989ad6831ea086721d9acf Status: Downloaded newer image for registry.redhat.io/rhel8/python-36:latest $ docker images registry.redhat.io/rhel8/python-36:latest REPOSITORY TAG IMAGE ID CREATED SIZE registry.redhat.io/rhel8/python-36 latest 77c14d37c369 4 weeks ago 781MB $ docker tag registry.redhat.io/rhel8/python-36:latest python-s2i-builder $ docker images python-s2i-builder REPOSITORY TAG IMAGE ID CREATED SIZE python-s2i-builder latest 77c14d37c369 4 weeks ago 781MB
アプリケーションイメージのS2Iビルド
上記で作成したS2Iビルダーイメージを使って、アプリケーションイメージをビルドします。
以下のコマンドでアプリケーションイメージをビルドすることができます。
$ s2i build [ソースコード] [S2Iビルダーイメージ名] [アプリケーションイメージ名]
registry.redhat.ioからpullしたS2IビルダーイメージとGit上のサンプルPythonソースコードを使って、python-s2iアプリケーションイメージをビルドします。
$ s2i build https://github.com/sclorg/s2i-python-container.git --context-dir=3.6/test/setup-test-app/ python-s2i-builder python-s2i $ docker images python-s2i REPOSITORY TAG IMAGE ID CREATED SIZE python-s2i latest b07c7f096566 15 seconds ago 789MB
アプリケーションイメージが実行できることを確認できました。
$ docker run --name hello -d -p 8080:8080 python-s2i ae57680ef2705b92a51727bc0e6d67ce14fca3d6a515abdffc48c9f4f6913bf4 $ curl localhost:8080 Hello from gunicorn WSGI application!
以上です!