使用 Cloud Build 构建和推送 Docker 映像
了解如何通过构建 Docker 映像并将映像推送到 Artifact Registry 来开始使用 Cloud Build。Artifact Registry 提供用于管理私有软件包和 Docker 容器映像的单个位置。
您首先使用 Docker 配置文件 Dockerfile
构建映像,然后使用 Cloud Build 配置文件构建相同的映像。
如需遵循有关此任务的分步指导,请直接在 Cloud Shell Editor 中点击操作演示:
准备工作
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Compute Engine and Artifact Registry APIs.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Compute Engine and Artifact Registry APIs.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
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.
- 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
-
In the Google Cloud console, go to the IAM page.
前往 IAM - 选择项目。
- 点击 授予访问权限。
-
在新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击 Save(保存)。
打开一个终端窗口。
创建名为
quickstart-docker
的新目录并导航至该目录:mkdir quickstart-docker cd quickstart-docker
创建名为
quickstart.sh
且包含以下内容的文件:创建名为
Dockerfile
且包含以下内容的文件:在终端窗口中运行以下命令,使
quickstart.sh
成为可执行文件:chmod +x quickstart.sh
在位置
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"
验证您的代码库已创建:
gcloud artifacts repositories list
您会在显示的代码库列表中看到
quickstart-docker-repo
。运行以下命令,获取您的 Google Cloud 项目 ID:
gcloud config get-value project
从包含
quickstart.sh
和Dockerfile
的目录运行以下命令:gcloud builds submit --region=us-west2 --tag us-west2-docker.pkg.dev/PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1
在包含
quickstart.sh
和Dockerfile
的同一目录中,创建名为cloudbuild.yaml
且包含以下内容的文件。此文件是您的构建配置文件。构建时,Cloud Build 会自动将$PROJECT_ID
替换为您的项目 ID。通过运行以下命令启动构建:
gcloud builds submit --region=us-west2 --config cloudbuild.yaml
在 Google Cloud 控制台中打开 Cloud Build 页面。
如有必要,请选择您的项目,然后点击打开。
您将看到构建记录页面:
如有必要,请在区域下拉菜单中选择
us-west2
以查看该区域中的 build。系统会列出两个 build,分别对应您在本快速入门中执行的两个 build。
点击其中一个 build。
您将看到构建详情页面。
如需查看构建的工件,请点击构建摘要下的构建工件。
您将看到类似于以下内容的输出:
您可以从此页下载您的构建日志,并查看 Artifact Registry 中映像的详情。
准备源文件以进行构建
您需要一些示例源代码来打包到容器映像中。在此部分,您将创建一个 shell 脚本和
Dockerfile
。Dockerfile
是文本文档,其中有 Docker 如何构建映像的说明。在 Artifact Registry 中创建 Docker 代码库
使用 Dockerfile 构建映像
Cloud Build 允许您使用
Dockerfile
构建 Docker 映像。您不需要单独的 Cloud Build 配置文件。如需使用
Dockerfile
构建,请执行以下操作:构建完成后,您将看到类似于以下内容的输出:
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。使用构建配置文件构建映像
在此部分,您将使用 Cloud Build 配置文件构建与上文相同的 Docker 映像。构建配置文件会指示 Cloud Build 根据您的规范执行任务。
构建完成后,您将看到类似于以下内容的输出:
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。查看构建详情
-
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。
在 Google Cloud 控制台中打开 Artifact Registry 页面。
选择您的项目,然后点击打开。
选择 quickstart-docker-repo。
点击删除。
您现在已经删除了在此快速入门中创建的代码库。
后续步骤
- 了解如何运行 Docker 映像。
- 了解如何创建基本 build 配置文件。
- 了解如何使用 Cloud Build 进行部署。
- 了解如何构建 Node.js 应用。
- 了解如何构建 Java 应用。
- 了解如何构建 Go 应用。