使用 Cloud Build 构建和推送 Docker 映像

了解如何通过构建 Docker 映像并将映像推送到 Artifact Registry 来开始使用 Cloud Build。Artifact Registry 提供用于管理私有软件包和 Docker 容器映像的单个位置。

您首先使用 Docker 配置文件 Dockerfile 构建映像,然后使用 Cloud Build 配置文件构建相同的映像。


如需遵循有关此任务的分步指导,请直接在 Cloud Shell Editor 中点击 操作演示:

操作演示


准备工作

  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. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  5. 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

  6. 安装 Google Cloud CLI。

  7. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  8. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  9. 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

  10. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  12. 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

  13. 安装 Google Cloud CLI。

  14. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  15. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init

所需角色

如需获得构建 Docker 映像并将其推送到 Artifact Registry 所需的权限,请让您的管理员向您授予 Cloud Build 服务帐号的以下 IAM 角色:

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

您也可以通过自定义 角色或其他预定义 角色来获取所需的权限。

准备源文件以进行构建

您需要一些示例源代码来打包到容器映像中。在本部分中,您将创建一个 shell 脚本和一个 DockerfileDockerfile 是文本文档,其中有 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. 使用 gcloud artifacts repositories create 命令在 位置 us-west2 中创建一个名为 quickstart-docker-repo 且说明文字为“Docker repository”(Docker 代码库)的 Docker 代码库:

    gcloud artifacts repositories create quickstart-docker-repo --repository-format=docker \
        --location=us-west2 --description="Docker repository"
    
  2. 验证您的代码库已创建:

    gcloud artifacts repositories list
    

    显示的仓库列表应包含 quickstart-docker-repo

    Artifact Registry 使用 命名惯例来识别 代码库和映像。例如,当您与存储在 Artifact Registry 中 us-west2 区域的 Docker 映像 进行交互时,您将在命令中使用 us-west2-docker.pkg.dev 作为注册表主机名。

使用 Dockerfile 构建映像

使用 gcloud builds submit 命令,结合使用 Dockerfile 来 构建 Docker 映像。您不需要单独的 Cloud Build 配置文件。

  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。

使用 build 配置文件构建映像

在本部分中,您将使用 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。

查看构建详情

  1. 在 Google Cloud 控制台中打开 Cloud Build 页面。

    打开 Cloud Build 页面

  2. 如有必要,请选择您的项目,然后点击打开

    您将看到构建记录 页面:

    构建记录页面的屏幕截图

  3. 如有必要,请在区域 下拉菜单中选择 us-west2 以查看该区域中的构建。

    系统会列出两个构建,分别对应您在本快速入门中执行的每个构建。

  4. 点击其中一个构建。

    您将看到构建详情 页面。

  5. 如需查看构建的工件,请点击构建摘要 下的构建工件

    您将看到如下所示的输出:

    构建工件的屏幕截图

    您可以从此页下载您的构建日志,并查看 Artifact Registry 中映像的详情。

清理

为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。

  1. 在 Google Cloud 控制台中打开 Artifact Registry 页面。

    打开 Artifact Registry 页面

  2. 选择您的项目,然后点击打开

  3. 选择 quickstart-docker-repo

  4. 点击删除

您现在已经删除了在此快速入门中创建的代码库。

后续步骤