在 Artifact Registry 中存储 Docker 容器映像
Artifact Registry 提供用于管理私有软件包和 Docker 容器映像的单个位置。
本快速入门向您展示了如何执行以下操作:
- 在 Artifact Registry 中创建专用 Docker 仓库
- 设置身份验证
- 将映像推送到仓库
- 从仓库中拉取映像
如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示:
准备工作
- 登录您的 Google Cloud 账号。如果您是新手 Google Cloud, 请创建一个账号来评估我们的产品在 实际场景中的表现。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
Make sure that you have the following role or roles on the project: Artifact Registry Administrator
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.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
Make sure that you have the following role or roles on the project: Artifact Registry Administrator
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.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
选择 shell
如需完成本快速入门,请使用 Cloud Shell 或本地 shell。
- Cloud Shell
- Cloud Shell 是一种 shell 环境,用于管理托管在 上的资源 Google Cloud。它预安装有 Docker 和 Google Cloud CLI,这是主要的 命令行界面 Google Cloud。
- 本地 shell
- 如果您更喜欢使用本地 shell,则必须在您的环境中安装 Docker 和 gcloud CLI。
启动 Cloud Shell
要启动 Cloud Shell,请执行以下步骤:
前往 Google Cloud 控制台。
点击激活 Cloud Shell 按钮:
。
控制台下方的框架内会打开一个 Cloud Shell 会话。您可以使用此 shell 运行 gcloud 命令。
设置本地 shell
如需安装 gcloud CLI 和 Docker,请执行以下步骤:
安装 gcloud CLI。如需更新现有安装,请运行命令
gcloud components update。安装 Docker(如果尚未安装)。
Docker 需要特别访问权限才能与注册表进行交互。 在 Linux 或 Windows 上,将您用于运行 Docker 命令的用户添加到 Docker 安全群组。在 macOS 上, Docker Desktop 以根用户身份在 虚拟机上运行,因此无需执行此步骤。
Linux
Docker 安全群组称为
docker。 如需添加用户名,请运行以下命令:sudo usermod -a -G docker ${USER}Windows
Docker 安全群组称为
docker-users。 如需从管理员命令提示符添加用户,请运行以下命令:net localgroup docker-users DOMAIN\USERNAME /add
其中
- DOMAIN 是您的 Windows 网域。
- USERNAME 是您的用户名。
退出并重新登录以使群组成员资格变更生效。如果您使用的是虚拟机,则可能需要重启虚拟机才能使成员资格变更生效。
若要确保 Docker 正在运行,请运行以下 Docker 命令,该命令返回当前时间和日期:
docker run --rm busybox date--rm标志表示会在退出时删除容器实例。
创建 Docker 代码库
创建 Docker 代码库以存储此快速入门的示例映像。
控制台
在 Google Cloud 控制台中打开代码库 页面。
点击 add 创建代码库。
指定
quickstart-docker-repo作为该代码库的名称。选择 Docker 作为格式,选择标准作为模式。
在位置类型下,选择区域,然后选择位置
us-west1。点击创建。
该代码库会被添加到代码库列表中。
gcloud
运行以下命令,在位置
us-west1中创建一个名为quickstart-docker-repo且带有说明“Docker 代码库”的新 Docker 代码库。gcloud artifacts repositories create quickstart-docker-repo --repository-format=docker \ --location=us-west1 --description="Docker repository" \ --project=PROJECT其中,PROJECT 是您的 Google Cloud 项目 ID。
运行以下命令确认已创建仓库。
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 映像推送到 Artifact Registry,您必须使用代码库名称标记该映像。
使用注册表名称标记映像
使用代码库名称标记 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
:),请参阅网域级项目。
从 Artifact Registry 中拉取映像
如需将映像从 Artifact Registry 拉取到本地机器,请运行以下命令:
docker pull us-west1-docker.pkg.dev/PROJECT/quickstart-docker-repo/quickstart-image:tag1
:),请参阅网域级项目。
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 账号产生费用,请按照以下步骤操作。
在移除代码库之前,请确保您要保留的任何映像在其他位置提供。
如需删除代码库,请执行以下操作:
控制台
在 Google Cloud 控制台中打开代码库 页面。
在代码库列表中,选择
quickstart-docker-repo代码库。点击删除。
gcloud
如需删除 quickstart-docker-repo 代码库,请运行以下命令:
gcloud artifacts repositories delete quickstart-docker-repo --location=us-west1
后续步骤
- 详细了解如何使用容器映像。
- 详细了解 Docker。
- 阅读我们关于 DevOps 的资源,并探索 DevOps 研究和评估 (DORA) 研究项目。