在 Artifact Registry 中儲存 Docker 容器映像檔

Artifact Registry:讓您在單一位置集中管理私人套件和 Docker 容器映像檔。

本快速入門導覽課程會說明完成下列作業的方法:

  • 在 Artifact Registry 建立私人 Docker 存放區
  • 設定驗證機制
  • 將映像檔推送至存放區
  • 從存放區提取映像檔

如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


事前準備

  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.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry API.

    Enable the API

  5. Make sure that you have the following role or roles on the project: Artifact Registry Administrator

    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. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Enable the Artifact Registry API.

    Enable the API

  9. Make sure that you have the following role or roles on the project: Artifact Registry Administrator

    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. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]

選擇殼層

如要完成本快速入門導覽課程,請使用 Cloud Shell 或本機殼層。

Cloud Shell
Cloud Shell 是殼層環境,用於管理託管在 Google Cloud上的資源。這個環境已預先安裝 Docker 和 Google Cloud 的主要指令列介面:Google Cloud CLI。 Google Cloud
本機殼層
如果您想要使用本機殼層,就必須在環境中安裝 Docker 和 gcloud CLI。

啟動 Cloud Shell

請執行下列步驟來啟動 Cloud Shell:

  1. 前往 Google Cloud 控制台。

    Google Cloud console

  2. 按一下「Activate Cloud Shell」(啟用 Cloud Shell) 按鈕:

此時 Cloud Shell 工作階段會在主控台底部的頁框中開啟,您可以使用這個殼層來執行 gcloud 指令。

設定本機殼層

如要安裝 gcloud CLI 和 Docker,請執行下列步驟:

  1. 安裝 gcloud CLI。如要更新現有安裝項目,請執行 gcloud components update 指令。

  2. 如果尚未安裝 Docker,請先安裝。

  3. Docker 需要具備權限的存取權,才能與登錄檔互動。 在 Linux 或 Windows 上,將用於執行 Docker 指令的使用者新增至 Docker 安全性群組。由於 Docker Desktop 會以超級使用者身分在虛擬機器上執行,因此 macOS 不需執行這個步驟。

    Linux

    Docker 安全性群組稱為 docker。 如要新增使用者名稱,請執行下列指令:

    sudo usermod -a -G docker ${USER}
    

    Windows

    Docker 安全性群組稱為 docker-users。 如要從管理員命令提示字元新增使用者,請執行下列指令:

    net localgroup docker-users DOMAIN\USERNAME /add
    

    地點

    • DOMAIN 是您的 Windows 網域。
    • USERNAME 是您的使用者名稱。
  4. 如要讓群組成員異動生效,請登出後再重新登入。 如果您使用的是虛擬機器,可能需要重新啟動虛擬機器,成員異動才會生效。

  5. 如要確認 Docker 正在運作,請執行下列 Docker 指令,可傳回目前的時間和日期:

    docker run --rm busybox date
    

    --rm 旗標會在結束時刪除容器執行個體。

建立 Docker 存放區

您必須建立 Docker 存放區,以便儲存本快速入門導覽課程所用的範例映像檔。

主控台

  1. 在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。

    開啟「存放區」頁面

  2. 按一下「新增」 「建立存放區」

  3. 指定 quickstart-docker-repo 做為存放區名稱。

  4. 選擇「Docker」做為格式,並選擇「Standard」做為模式。

  5. 在「位置類型」下方,選取「區域」,接著選擇位置 us-west1

  6. 點選「建立」

存放區會新增至存放區清單。

gcloud

  1. 執行下列指令,在 us-west1 位置中新建名為 quickstart-docker-repo 的 Docker 存放區,並將說明設為「docker repository」(Docker 存放區)。

    gcloud artifacts repositories create quickstart-docker-repo --repository-format=docker \
        --location=us-west1 --description="Docker repository" \
        --project=PROJECT
    

    其中 PROJECT 是您的 Google Cloud 專案 ID。

  2. 執行下列指令來確認存放區已建立完成:

    gcloud artifacts repositories list \
        --project=PROJECT
    

如要進一步瞭解 Artifact Registry 指令,請執行 gcloud artifacts 指令。

設定驗證機制

在推送或提取映像檔前,請先設定 Docker,透過 Google Cloud CLI 驗證傳送至 Artifact Registry 的要求。

如要為 us-west1 區域中的 Docker 存放區設定驗證機制,請執行下列指令:

gcloud auth configure-docker us-west1-docker.pkg.dev

這個指令會更新 Docker 設定。您現在可以在 Google Cloud 專案中連結 Artifact Registry,以推送及提取映像檔。

如要瞭解其他驗證方式,請參閱驗證方式的相關說明。

取得要推送的映像檔

在本快速入門導覽課程中,您將推送名為 hello-app 的範例映像檔。

執行下列指令,提取映像檔的 1.0 版。

   docker pull us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0

Artifact Registry 的映像檔路徑包含多個部分,下方以這個範例映像檔為例:

  • us-docker.pkg.dev 是儲存在 Artifact Registry Docker 存放區的容器映像檔主機名稱,其中包含存放區位置 (us)。
  • google-samples 是專案 ID。
  • containers 是存放區 ID。
  • /gke/hello-app 是映像檔在存放區 containers 的路徑。

將映像檔新增至存放區

請務必先完成標記步驟,再推送 Docker 映像檔。

使用儲存庫名稱標記映像檔

使用存放區名稱標記 Docker 映像檔可設定 docker push 指令,將映像檔推送至特定位置。在本快速入門導覽課程中,主機位置為 us-west1-docker.pkg.dev

執行下列指令,將映像檔標記為 quickstart-image:tag1

docker tag us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 \
us-west1-docker.pkg.dev/PROJECT/quickstart-docker-repo/quickstart-image:tag1

其中:

  • us-west1 是存放區位置。
  • us-west1-docker.pkg.dev 是新建 Docker 存放區的主機名稱。
  • PROJECT 是您的 Google Cloud 專案 ID。 如果專案 ID 包含冒號 (:),請參閱「網域範圍專案」。
  • quickstart-docker-repo 是您建立的存放區 ID。
  • quickstart-image 是您要用於存放區的映像檔名稱。可以使用和本機映像檔不同的名稱。在本快速入門導覽課程中,您會將映像檔直接儲存在 ID 為 quickstart-docker-repo 的存放區。
  • tag1 是您要新增至 Docker 映像檔的標記。如未指定,Docker 會套用預設標記 latest

現在可以開始將映像檔推送至新建的存放區了!

將映像檔推送至 Artifact Registry

設定驗證方法並標記本機映像檔後,您就可以將映像檔推送至自己建立的存放區。

請執行下列指令來推送 Docker 映像檔:

docker push us-west1-docker.pkg.dev/PROJECT/quickstart-docker-repo/quickstart-image:tag1
PROJECT 替換為 Google Cloud 專案 ID。 如果專案 ID 包含冒號 (:),請參閱「網域範圍專案」。

從 Artifact Registry 提取映像檔

如要將映像檔從 Artifact Registry 提取到本機電腦,請執行下列指令:

docker pull us-west1-docker.pkg.dev/PROJECT/quickstart-docker-repo/quickstart-image:tag1
PROJECT 替換為 Google Cloud 專案 ID。 如果專案 ID 包含冒號 (:),請參閱「網域範圍專案」。
latest: Pulling from [PROJECT-ID]/quickstart-image:tag1
Digest: sha256:70c42...
Status: Image is up to date for us-west1-docker.pkg.dev/PROJECT/quickstart-docker-repo/quickstart-image:tag1

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。

移除存放區之前,請先確認要保留的映像檔均已存放於其他位置。

如要刪除存放區,請按照下列指示操作:

主控台

  1. 在 Google Cloud 控制台中開啟「Repositories」(存放區) 頁面。

    開啟「存放區」頁面

  2. 在存放區清單中,選取 quickstart-docker-repo 存放區。

  3. 點選「刪除」。

gcloud

如要刪除 quickstart-docker-repo 存放區,請執行下列指令:

gcloud artifacts repositories delete quickstart-docker-repo --location=us-west1

後續步驟