使用 Buildpacks 建構應用程式

本指南說明如何搭配應用程式原始碼使用 Buildpack,建立容器映像檔。舉例來說,您可以使用 Buildpack,將 Cloud Run 服務的原始碼建構為容器映像檔。

您可以使用以下兩種方法,透過建構包建構容器映像檔:

  • 使用 pack CLI 在本機建構,在本機測試應用程式,並在部署前快速製作變更原型。
  • 使用 Cloud Build從遠端建構。對於資源密集型建構程序的大型應用程式,使用 Cloud Build 建構很有幫助,也能協助保護軟體供應鏈。

本機建構作業

您可以使用 pack CLI,在本機將應用程式建構為容器映像檔。

事前準備

  1. 在工作站上安裝 Docker Community Edition (CE)。pack 會使用 Docker 做為 OCI 映像檔建構工具。
  2. 安裝 Pack CLI。
  3. 安裝 Git 來源控制工具,從 GitHub 擷取範例應用程式。

在本機建構應用程式

您可以使用 pack build 指令並指定預設建構工具 --builder=gcr.io/buildpacks/builder,在本機建構容器映像檔。

pack build --builder=gcr.io/buildpacks/builder IMAGE_NAME

將 IMAGE_NAME 替換為服務的容器映像檔名稱。

您也可以擴充建構和執行映像檔,自訂容器映像檔。

在本機建構範例應用程式

下列範例說明如何在本地建構範例。

  1. 將範例存放區複製到本機電腦:
    git clone https://github.com/GoogleCloudPlatform/buildpack-samples.git
  2. 變更為包含應用程式程式碼範例的目錄:

    Go

    cd buildpack-samples/sample-go

    Java

    cd buildpack-samples/sample-java-gradle

    Node.js

    cd buildpack-samples/sample-node

    PHP

    cd buildpack-samples/sample-php

    Python

    cd buildpack-samples/sample-python

    Ruby

    cd buildpack-samples/sample-ruby

    .NET

    cd buildpack-samples/sample-dotnet
  3. 使用 pack 建構範例應用程式映像檔:

    Go

    pack build --builder=gcr.io/buildpacks/builder sample-go

    Java

    pack build --builder=gcr.io/buildpacks/builder sample-java-gradle

    Node.js

    pack build --builder=gcr.io/buildpacks/builder sample-node

    PHP

    pack build --builder=gcr.io/buildpacks/builder sample-php

    Python

    pack build --builder=gcr.io/buildpacks/builder sample-python

    Ruby

    pack build --builder=gcr.io/buildpacks/builder sample-ruby

    .NET

    pack build --builder=gcr.io/buildpacks/builder sample-dotnet
  4. 使用 docker 執行圖片:

    Go

    docker run -p8080:8080 sample-go

    Java

    docker run -it -ePORT=8080 -p8080:8080 sample-java-gradle

    Node.js

    docker run -it -ePORT=8080 -p8080:8080 sample-node

    PHP

    docker run -it --rm -p 8080:8080 sample-php

    Python

    docker run -it -ePORT=8080 -p8080:8080 sample-python

    Ruby

    docker run -it -ePORT=8080 -p8080:8080 sample-ruby

    .NET

    docker run -it -ePORT=8080 -p8080:8080 sample-dotnet
  5. 瀏覽至 localhost:8080,即可查看正在執行的應用程式。

遠端建構

使用 Cloud Build 將應用程式建構成容器映像檔,並使用 Artifact Registry 做為容器存放區,儲存及部署每個映像檔。

事前準備

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自體驗產品的實際應用成效。新客戶還能獲得價值 $300 美元的免費抵免額,能用於執行、測試及部署工作負載。
  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 role (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 and Artifact Registry APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    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 role (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 and Artifact Registry APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init
  14. 確認 Google Cloud 專案有權存取容器映像檔存放區。

    如要設定 Artifact Registry 中的 Docker 存放區存取權,請按照下列步驟操作:

    1. 在 Google Cloud 專案的相同位置建立新的 Docker 存放區。
      gcloud artifacts repositories create REPO_NAME \
      --repository-format=docker \
      --location=REGION --description="DESCRIPTION"
      取代:
      • REPO_NAME,並將 Docker 存放區命名為您選擇的名稱。
      • REGION,該位置必須位於專案位置或最靠近專案位置。 Google Cloud
      • DESCRIPTION,並加上您選擇的說明。

      舉例來說,如要在 us-west2 中建立 docker 存放區,並將說明設為「Docker repository」,請執行下列指令:

      gcloud artifacts repositories create buildpacks-docker-repo --repository-format=docker \
      --location=us-west2 --description="Docker repository"
    2. 確認存放區是否已成功建立:
      gcloud artifacts repositories list

      清單中應該會顯示您為 Docker 存放區選擇的名稱。

遠端建構應用程式

您可以使用 gcloud builds submit 指令,建構容器映像檔並上傳至存放區。

您可以選擇在指令中指定容器映像檔,或是使用設定檔。

使用指令建構

如要在沒有設定檔的情況下建構,請指定 image 標記:

gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME

更改項目:

  • LOCATION 替換為容器存放區的區域名稱。示例: us-west2
  • 將 PROJECT_ID 替換為 Google Cloud 專案 ID。
  • 將 REPO_NAME 換成 Docker 存放區的名稱。
  • IMAGE_NAME,並將其替換為容器映像檔的名稱。

範例:

gcloud builds submit --pack image=us-west2-docker.pkg.dev/my-project-id/my-buildpacks-docker-repo/app-image

使用設定檔建構

您可以使用設定檔定義映像檔存放區設定詳細資料。設定檔採用 YAML 檔案格式,且必須包含使用 pack CLI 的建構步驟。

  1. 建立名為 cloudbuild.yaml 的 YAML 檔案,其中包含容器映像檔存放區的 URI。

     options:
       logging: CLOUD_LOGGING_ONLY
       pool: {}
     projectId: PROJECT_ID
     steps:
     - name: gcr.io/k8s-skaffold/pack
       entrypoint: pack
       args:
       - build
       - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
       - --builder
       - gcr.io/buildpacks/builder:latest
       - --network
       - cloudbuild
       - --publish
     - name: gcr.io/cloud-builders/docker
       entrypoint: pack
       args:
       - pull
       - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
     images:
     - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
    

    更改項目:

    • LOCATION,例如 us-west2。
    • 將 PROJECT_ID 替換為 Google Cloud 專案 ID。
    • 將 REPO_NAME 換成 Docker 存放區的名稱。
    • IMAGE_NAME,並將其替換為容器映像檔的名稱。
  2. 建構應用程式。

    如果設定檔名為 cloudbuild.yaml,請執行下列指令:

    gcloud builds submit .
    

範例:從遠端建構範例應用程式

以下範例說明如何遠端建構範例,然後確認容器映像檔已推送至 Artifact Registry 中的存放區。

  1. 將範例存放區複製到本機電腦:
    git clone https://github.com/GoogleCloudPlatform/buildpack-samples.git
  2. 變更為包含應用程式程式碼範例的目錄:

    Go

    cd buildpack-samples/sample-go

    Java

    cd buildpack-samples/sample-java-gradle

    Node.js

    cd buildpack-samples/sample-node

    PHP

    cd buildpack-samples/sample-php

    Python

    cd buildpack-samples/sample-python

    Ruby

    cd buildpack-samples/sample-ruby

    .NET

    cd buildpack-samples/sample-dotnet
  3. 使用 gcloud 將應用程式原始碼提交至 Cloud Build:

    Go

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-go

    Java

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-java-gradle

    Node.js

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-node

    PHP

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-php

    Python

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-python

    Ruby

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-ruby

    .NET

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-dotnet
  4. 確認範例應用程式已成功發布至 REPO_NAME:
    gcloud artifacts docker images list LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME

    更改項目:

    • LOCATION 替換為容器存放區的區域名稱。範例:us-west2
    • 將 PROJECT_ID 替換為 Google Cloud 專案 ID。
    • 將 REPO_NAME 換成 Docker 存放區的名稱。

後續步驟