Cloud Build を使用して Docker イメージをビルドし、push する

Docker イメージをビルドし、そのイメージを Artifact Registry に push して、Cloud Build の使用を開始する方法を学習します。Artifact Registry では、プライベート・パッケージと Docker コンテナ イメージを管理する単一の場所が準備されています。

まず、Docker 構成ファイルの Dockerfile を使用してイメージをビルドし、次に Cloud Build 構成ファイルを使用して同じイメージをビルドします。


このタスクを Cloud Shell エディタで直接行う際のガイダンスについては、「ガイドを表示」をクリックしてください。

ガイドを表示


始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Build, Compute Engine and Artifact Registry APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

  7. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Cloud Build, Compute Engine and Artifact Registry APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  11. Install the Google Cloud CLI.

  12. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

  13. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  14. Make sure that you have the following role or roles on the project: Artifact Registry Administrator, Cloud Build Editor, Logs Viewer, Service Account User, Service Usage Admin, Storage Bucket Viewer, Storage Object Creator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      IAM に移動
    2. プロジェクトを選択します。
    3. [ アクセスを許可] をクリックします。
    4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。
    8. ビルドするソースファイルを準備する

      コンテナ イメージにパッケージ化するには、サンプルのソースコードが必要です。このセクションでは、シェル スクリプトと Dockerfile を作成します。Dockerfile は、Docker がイメージを作成するための指示を含むテキスト ドキュメントです。

      1. ターミナル ウィンドウを開きます。

      2. quickstart-docker という名前で新しいディレクトリを作成し、そのディレクトリに移動します。

        mkdir quickstart-docker
        cd quickstart-docker
        
      3. 次の内容のファイルを quickstart.sh という名前で作成します。

        #!/bin/sh
        echo "Hello, world! The time is $(date)."
      4. 次の内容のファイルを Dockerfile という名前で作成します。

        FROM alpine
        COPY quickstart.sh /
        CMD ["/quickstart.sh"]
      5. ターミナル ウィンドウで次のコマンドを実行して、quickstart.sh を実行可能にします。

        chmod +x quickstart.sh
        

      Artifact Registry で Docker リポジトリを作成する

      1. ロケーション us-west2quickstart-docker-repo という名前の Docker リポジトリを作成し、「Docker repository」という説明を指定します。

        gcloud artifacts repositories create quickstart-docker-repo --repository-format=docker \
            --location=us-west2 --description="Docker repository"
        
      2. リポジトリが作成されたことを確認します。

        gcloud artifacts repositories list
        

        表示されたリポジトリのリストに quickstart-docker-repo が表示されます。

      Dockerfile を使用してイメージをビルドする

      Cloud Build で、Docker イメージを Dockerfile を使用してビルドできます。個別の Cloud Build 構成ファイルは不要です。

      Dockerfile を使用してビルドするには:

      1. 次のコマンドを実行して、 Google Cloud プロジェクト ID を取得します。

        gcloud config get-value project
        
      2. quickstart.shDockerfile を含むディレクトリから次のコマンドを実行します。

        gcloud builds submit --region=us-west2 --tag us-west2-docker.pkg.dev/PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1
        

      ビルドが完了すると、次のような出力が表示されます。

      DONE
      ------------------------------------------------------------------------------------------------------------------------------------
      ID                                    CREATE_TIME                DURATION  SOURCE   IMAGES     STATUS
      545cb89c-f7a4-4652-8f63-579ac974be2e  2020-11-05T18:16:04+00:00  16S       gs://gcb-docs-project_cloudbuild/source/1604600163.528729-b70741b0f2d0449d8635aa22893258fe.tgz  us-west2-docker.pkg.dev/gcb-docs-project/quickstart-docker-repo/quickstart-image:tag1  SUCCESS
      

      Dockerfile で、quickstart-image という名前の Docker イメージをビルドしました。このイメージを Artifact Registry に push します。

      ビルド構成ファイルを使用してイメージをビルドする

      このセクションでは、Cloud Build 構成ファイルを使用して、上記と同じ Docker イメージを作成します。ビルド構成ファイルは、指定した内容に基づいてタスクを実行するよう Cloud Build に指示します。

      1. quickstart.shDockerfile があるディレクトリに、cloudbuild.yaml という名前のファイルを作成します。このファイルには以下を含めます。このファイルがビルド構成ファイルです。ビルド時に、Cloud Build が自動的に $PROJECT_ID をプロジェクト ID に置き換えます。

        steps:
        - name: 'gcr.io/cloud-builders/docker'
          script: |
            docker build -t us-west2-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1 .
          automapSubstitutions: true
        images:
        - 'us-west2-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1'
      2. 次のコマンドを実行してビルドを開始します。

        gcloud builds submit --region=us-west2 --config cloudbuild.yaml
        

      ビルドが完了すると、次のような出力が表示されます。

      DONE
      ------------------------------------------------------------------------------------------------------------------------------------
      ID                                    CREATE_TIME                DURATION  SOURCE          IMAGES          STATUS
      046ddd31-3670-4771-9336-8919e7098b11  2020-11-05T18:24:02+00:00  15S       gs://gcb-docs-project_cloudbuild/source/1604600641.576884-8153be22c94d438aa86c78abf11403eb.tgz  us-west2-docker.pkg.dev/gcb-docs-project/quickstart-docker-repo/quickstart-image:tag1  SUCCESS
      

      ビルド構成ファイルを使用して quickstart-image をビルドし、イメージを Artifact Registry に push しました。

      ビルドの詳細を表示

      1. Google Cloud コンソールで Cloud Build ページを開きます。

        Cloud Build ページを開く

      2. 必要に応じて、プロジェクトを選択して [開く] をクリックします。

        [ビルド履歴] ページが表示されます。

        ビルド履歴ページのスクリーンショット

      3. 必要に応じて、[リージョン] プルダウン メニューで us-west2 を選択してそのリージョンのビルドを表示します。

        このクイックスタートで実行したビルドごとに 1 つずつ、2 つのビルドが表示されます。

      4. いずれかのビルドをクリックします。

        [ビルドの詳細] ページが表示されます。

      5. ビルドのアーティファクトを表示するには、[ビルドの概要] で [ビルド アーティファクト] をクリックします。

        出力は次のようになります。

        ビルド アーティファクトのスクリーンショット

        このページから、ビルドログをダウンロードして、Artifact Registry でイメージの詳細を表示できます。

      クリーンアップ

      このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の手順を実施します。

      1. Google Cloud コンソールで Artifact Registry ページを開きます。

        Artifact Registry ページを開きます。

      2. プロジェクトを選択し、[開く] をクリックします。

      3. [quickstart-docker-repo] を選択します。

      4. [削除] をクリックします。

      このクイックスタートで作成したリポジトリの削除は完了です。

      次のステップ