LoginSignup
0
0

More than 3 years have passed since last update.

Google Cloud Buildを利用してUnity ML-AgentsのDockerイメージを作成する(v0.9.1対応)

Posted at

Unity ML-AgentsをDockerで実行するにはイメージを作成する必要がありますが、Google Cloud Buildを利用するようにしてみました。

今回はトリガーを利用せず、ローカルでgcloud コマンドを利用してビルドしてみました。

Google Cloud Buildについては以下をご参考ください。

Google Cloud Build
https://cloud.google.com/cloud-build/?hl=ja

Google Cloud Buildとは一体何者なのか
https://swet.dena.com/entry/2018/08/20/170836

手順

GCPのプロジェクトとgcloud コマンドが利用できる前提となります。
コマンドのインストール方法は下記が参考になります。

Cloud SDK のインストール  |  Cloud SDK のドキュメント  |  Google Cloud
https://cloud.google.com/sdk/downloads?hl=JA

cloudbuild.yaml の用意

Google Cloud BuildでDockerイメージが作成できるようにcloudbuild.yaml ファイルを用意します。

> mkdir 任意のディレクトリ
> cd 任意のディレクトリ
> touch cloudbuild.yaml

gitでリポジトリを取得後、リポジトリに含まれているDockerfile からイメージを作成します。

cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/git'
  args: ['clone', 'https://github.com/Unity-Technologies/ml-agents.git', '-b', $TAG_NAME, '--depth', '1']
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/unity-ml-agents:$TAG_NAME', './ml-agents']
images: ['gcr.io/$PROJECT_ID/unity-ml-agents:$TAG_NAME']

$TAG_NAME を利用して、Unity ML-Agentsのバージョンを指定するようにしました。$TAG_NAME はコマンド実行時にパラメータ指定します。

他に利用できる変数については下記をご参考ください。

Cloud Build 変数値の置換
https://cloud.google.com/cloud-build/docs/configuring-builds/substitute-variable-values

git clone で取得したリポジトリにあるDockerfile が参照されるように./ml-agents を指定します。

イメージ名unity-ml-agents は任意の名称でOKです。

ビルド実行

準備ができたので、Google Cloud Buildにジョブ開始のコマンドを実行します。

> gcloud builds submit \
  --config=cloudbuild.yaml \
  --substitutions=TAG_NAME="0.9.1"

Creating temporary tarball archive of 1 file(s) totalling 413 bytes before compression.
Uploading tarball of [.] to [gs://GCPのプロジェクトID_cloudbuild/source/1566436194.89-b50bc19f2aec4fafa922c3b61ca9e8ce.tgz]
Created [https://cloudbuild.googleapis.com/v1/projects/GCPのプロジェクトID/builds/2860d702-303d-4c55-a5ff-fbfd6bc27e25].
Logs are available at [https://console.cloud.google.com/gcr/builds/2860d702-303d-4c55-a5ff-fbfd6bc27e25?project=xxxxxxxxxxxx].
----------------------------------------------------- REMOTE BUILD OUTPUT -----------------------------------------------------
starting build "2860d702-303d-4c55-a5ff-fbfd6bc27e25"

FETCHSOURCE
Fetching storage object: gs://GCPのプロジェクトID_cloudbuild/source/1566436194.89-b50bc19f2aec4fafa922c3b61ca9e8ce.tgz#1566436196515961
Copying gs://GCPのプロジェクトID_cloudbuild/source/1566436194.89-b50bc19f2aec4fafa922c3b61ca9e8ce.tgz#1566436196515961...
/ [1 files][  391.0 B/  391.0 B]
Operation completed over 1 objects/391.0 B.
BUILD
Starting Step #0
Step #0: Already have image (with digest): gcr.io/cloud-builders/git
Step #0: Cloning into 'ml-agents'...
Step #0: Note: checking out 'd11229820f3d7f090341c76edea7bad83fc0eeec'.
Step #0:
Step #0: You are in 'detached HEAD' state. You can look around, make experimental
Step #0: changes and commit them, and you can discard any commits you make in this
Step #0: state without impacting any branches by performing another checkout.
Step #0:
Step #0: If you want to create a new branch to retain commits you create, you may
Step #0: do so (now or later) by using -b with the checkout command again. Example:
Step #0:
Step #0:   git checkout -b <new-branch-name>
Step #0:
Finished Step #0
Starting Step #1
Step #1: Already have image (with digest): gcr.io/cloud-builders/docker
Step #1: Sending build context to Docker daemon  199.6MB
Step #1: Step 1/20 : FROM ubuntu:16.04
(略)
0.9.1: digest: sha256:3573056b2abca07328ac0ec7179e7625793aea9721d58d641898a45ff6cecb2b size: 3259
DONE
-------------------------------------------------------------------------------------------------------------------------------

ID                                    CREATE_TIME                DURATION  SOURCE                                                                                        IMAGES                                           STATUS
2860d702-303d-4c55-a5ff-fbfd6bc27e25  2019-08-22T01:09:58+00:00  7M53S     gs://GCPのプロジェクトID_cloudbuild/source/1566436194.89-b50bc19f2aec4fafa922c3b61ca9e8ce.tgz  gcr.io/GCPのプロジェクトID/unity-ml-agents:0.9.1  SUCCESS

GCP管理コンソールでもジョブの実行が確認できます。
Cloud_Build_-_cloudpack-rd_-_Google_Cloud_Platformのコピー.png

Google Container RegistryにもイメージがPushされていることも確認できます。
Container_Registry_-_cloudpack-rd_-_Google_Cloud_Platformのコピー.png

これでローカルにDockerがインストールされていなくても、イメージ作成ができるようになりました。

参考

Google Cloud Build
https://cloud.google.com/cloud-build/?hl=ja

Google Cloud Buildとは一体何者なのか
https://swet.dena.com/entry/2018/08/20/170836

Cloud Build 変数値の置換
https://cloud.google.com/cloud-build/docs/configuring-builds/substitute-variable-values

Cloud SDK のインストール  |  Cloud SDK のドキュメント  |  Google Cloud
https://cloud.google.com/sdk/downloads?hl=JA

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0