本文档介绍了如何配置对 Docker Hub 上游进行身份验证 Artifact Registry 远程代码库
即使您仅使用公共 Docker Hub,我们也建议您向 Docker Hub 进行身份验证 因为进行身份验证会提高下载速率限制。有关 有关 Docker Hub 下载速率限制的信息,请参阅 Docker Hub 速率限制。远程代码库允许 您需要添加自己的 Docker Hub 用户名和以 向 Docker Hub 进行身份验证
本文档假定您已经创建了一个 Artifact Registry Docker 远程仓库以及一个 Docker Hub 。
如需详细了解远程仓库,请参阅 远程仓库概览。
所需的角色
如需获取为远程仓库配置 Docker Hub 身份验证所需的权限, 请让管理员授予您 项目的以下 IAM 角色:
-
Artifact Registry 管理员 (
roles/artifactregistry.admin
) -
Secret Manager Admin (
roles/secretmanager.admin
)
如需详细了解如何授予角色,请参阅管理访问权限。
创建 Docker Hub 个人访问令牌
- 登录 Docker Hub。
- 使用以下命令创建个人访问令牌: 只读权限。
复制访问令牌。
将访问令牌保存到本地或 Cloud Shell 的文本文件中。
将您的个人访问令牌保存在 Secret 版本中
向 Artifact Registry 服务账号授予对 Secret 的访问权限
在与 Artifact Registry 进行交互时,Artifact Registry 服务代理会代表 Artifact Registry 执行操作 Google Cloud 服务。允许服务代理使用 Secret 存储在 Secret Manager 中,则必须向服务代理授予权限 查看您的密钥版本。
服务代理标识符为:
service-PROJECT-NUMBER@gcp-sa-artifactregistry.iam.gserviceaccount.com
PROJECT-NUMBER 是 项目编号 运行 Artifact Registry 的 Google Cloud 项目的 ID。
向 Artifact Registry 服务代理授予 Secret Manager Secret Accessor 角色:
控制台
-
转到 Google Cloud 控制台中的 Secret Manager 页面。
-
在 Secret Manager 页面上,点击 Secret 名称旁边的复选框。
-
如果面板尚未打开,请点击显示信息面板以打开面板。
-
在信息面板中,点击添加主账号。
-
在新主账号文本区域中,输入要添加的成员的电子邮件地址。
-
在选择角色下拉列表中,选择 Secret Manager,然后选择 Secret Manager Secret Accessor。
gcloud
$ gcloud secrets add-iam-policy-binding secret-id \
--member="member" \
--role="roles/secretmanager.secretAccessor"
其中,member 是 IAM 成员,例如用户、群组或服务账号。
C#
如需向 Artifact Registry 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
Go
如需向 Artifact Registry 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
Java
如需向 Artifact Registry 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
Node.js
如需向 Artifact Registry 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
PHP
如需向 Artifact Registry 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
Python
如需向 Artifact Registry 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
Ruby
如需向 Artifact Registry 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
API
注意:与其他示例不同,它将替换整个 IAM 政策。
$ curl "https://secretmanager.googleapis.com/v1/projects/project-id/secrets/secret-id:setIamPolicy" \
--request "POST" \
--header "authorization: Bearer $(gcloud auth print-access-token)" \
--header "content-type: application/json" \
--data "{\"policy\": {\"bindings\": [{\"members\": [\"member\"], \"role\": \"roles/secretmanager.secretAccessor\"}]}}"
如需详细了解如何授予或撤消对 Secret 的访问权限,请参阅 管理对 Secret 的访问权限。
将 Docker Hub 凭据添加到远程代码库
如需使用 Docker Hub 凭据更新远程代码库,请执行以下操作:
控制台
打开 Google Cloud 控制台中的制品库页面。
在代码库列表中,选择该代码库,然后点击 修改代码库。
在远程仓库身份验证模式部分中,更新或添加 您的 Docker Hub 用户名和包含您的 Docker Hub 的 Secret 版本 访问令牌。
gcloud CLI
如需使用 Docker Hub 凭据更新远程代码库,请运行 以下命令:
gcloud artifacts repositories update REPOSITORY \
--project=PROJECT_ID \
--location=LOCATION \
--remote-username=USERNAME \
--remote-password-secret-version=projects/SECRET_PROJECT_ID/secrets/SECRET_ID/versions/SECRET_VERSION
替换以下内容:
- 将
REPOSITORY
替换为您的 Artifact Registry 遥控器的名称 存储库 - 将
PROJECT_ID
替换为 Google Cloud 项目 ID。 - 将
LOCATION
替换为单区域级或多区域级 代码库的位置。如果您设置默认值,则可以省略此标志。查看列表 请运行命令gcloud artifacts locations list
。 - 将
USERNAME
替换为您的 Docker Hub 用户名。 - 将
SECRET_PROJECT_ID
替换为 您在其中创建 Secret 的 Google Cloud 项目。 - 将
SECRET_ID
替换为您为 Secret 指定的名称。 - 将
SECRET_VERSION
替换为您的密钥版本 Docker Hub 访问令牌。
远程代码库下次发送请求时,系统会使用您的凭据 。
后续步骤
- 详细了解 Artifact Registry 代码库。
- 使用 Docker 拉取映像。
- 参阅 Docker Hub 远程代码库快速入门。