用途
Azure Container Registry SDK for Python 用于在 Python 中管理 Azure 容器注册表(Azure Container Registry)中的容器镜像、制品和仓库。它提供对仓库、标签、清单(manifest)和 blob 的查询、属性更新与删除能力,并支持下载清单内容和层 blob。
安装与环境变量
安装命令:
pip install azure-containerregistry
环境变量示例:
AZURE_CONTAINERREGISTRY_ENDPOINT=https://<registry-name>.azurecr.io
认证方式
Entra ID(推荐)
from azure.containerregistry import ContainerRegistryClient
from azure.identity import DefaultAzureCredential
client = ContainerRegistryClient(
endpoint=os.environ["AZURE_CONTAINERREGISTRY_ENDPOINT"],
credential=DefaultAzureCredential()
)
匿名访问(公共注册表)
from azure.containerregistry import ContainerRegistryClient
client = ContainerRegistryClient(
endpoint="https://mcr.microsoft.com",
credential=None,
audience="https://mcr.microsoft.com"
)
主要能力
列出仓库
client = ContainerRegistryClient(endpoint, DefaultAzureCredential())
for repository in client.list_repository_names():
print(repository)
仓库操作
获取仓库属性:
properties = client.get_repository_properties("my-image")
print(f"Created: {properties.created_on}")
print(f"Modified: {properties.last_updated_on}")
print(f"Manifests: {properties.manifest_count}")
print(f"Tags: {properties.tag_count}")
更新仓库属性:
from azure.containerregistry import RepositoryProperties
client.update_repository_properties(
"my-image",
properties=RepositoryProperties(
can_delete=False,
can_write=False
)
)
删除仓库:
client.delete_repository("my-image")
标签操作
列出标签:
for tag in client.list_tag_properties("my-image"):
print(f"{tag.name}: {tag.created_on}")
按顺序过滤:
from azure.containerregistry import ArtifactTagOrder
# Most recent first
for tag in client.list_tag_properties(
"my-image",
order_by=ArtifactTagOrder.LAST_UPDATED_ON_DESCENDING
):
print(f"{tag.name}: {tag.last_updated_on}")
获取标签属性:
tag = client.get_tag_properties("my-image", "latest")
print(f"Digest: {tag.digest}")
print(f"Created: {tag.created_on}")
删除标签:
client.delete_tag("my-image", "old-tag")
清单操作
列出清单:
from azure.containerregistry import ArtifactManifestOrder
for manifest in client.list_manifest_properties(
"my-image",
order_by=ArtifactManifestOrder.LAST_UPDATED_ON_DESCENDING
):
print(f"Digest: {manifest.digest}")
print(f"Tags: {manifest.tags}")
print(f"Size: {manifest.size_in_bytes}")
获取清单属性:
manifest = client.get_manifest_properties("my-image", "latest")
print(f"Digest: {manifest.digest}")
print(f"Architecture: {manifest.architecture}")
print(f"OS: {manifest.operating_system}")
更新清单属性:
from azure.containerregistry import ArtifactManifestProperties
client.update_manifest_properties(
"my-image",
"latest",
properties=ArtifactManifestProperties(
can_delete=False,
can_write=False
)
)
删除清单:
# Delete by digest
client.delete_manifest("my-image", "sha256:abc123...")
# Delete by tag
manifest = client.get_manifest_properties("my-image", "old-tag")
client.delete_manifest("my-image", manifest.digest)
上传与下载制品
from azure.containerregistry import ContainerRegistryClient
client = ContainerRegistryClient(endpoint, DefaultAzureCredential())
# Download manifest
manifest = client.download_manifest("my-image", "latest")
print(f"Media type: {manifest.media_type}")
print(f"Digest: {manifest.digest}")
# Download blob
blob = client.download_blob("my-image", "sha256:abc123...")
with open("layer.tar.gz", "wb") as f:
for chunk in blob:
f.write(chunk)
异步客户端
from azure.containerregistry.aio import ContainerRegistryClient
from azure.identity.aio import DefaultAzureCredential
async def list_repos():
credential = DefaultAzureCredential()
client = ContainerRegistryClient(endpoint, credential)
async for repo in client.list_repository_names():
print(repo)
await client.close()
await credential.close()
清理旧镜像
from datetime import datetime, timedelta, timezone
cutoff = datetime.now(timezone.utc) - timedelta(days=30)
for manifest in client.list_manifest_properties("my-image"):
if manifest.last_updated_on < cutoff and not manifest.tags:
print(f"Deleting {manifest.digest}")
client.delete_manifest("my-image", manifest.digest)
客户端操作一览
| 操作 | 说明 |
|---|---|
| list_repository_names | 列出所有仓库 |
| get_repository_properties | 获取仓库元数据 |
| delete_repository | 删除仓库及其所有镜像 |
| list_tag_properties | 列出仓库中的标签 |
| get_tag_properties | 获取标签元数据 |
| delete_tag | 删除指定标签 |
| list_manifest_properties | 列出仓库中的清单 |
| get_manifest_properties | 获取清单元数据 |
| delete_manifest | 按 digest 删除清单 |
| download_manifest | 下载清单内容 |
| download_blob | 下载层 blob |
最佳实践
- 生产环境使用 Entra ID 进行认证
- 按 digest 而非标签删除,避免产生孤立镜像
- 使用 can_delete=False 锁定生产镜像
- 定期清理未打标签的清单
- 高吞吐操作使用异步客户端
- 按 last_updated 排序以查找最新或最旧的镜像
- 删除前检查 manifest.tags,避免删除已打标签的镜像
适用场景
该技能适用于执行上述概述中描述的工作流或操作。
限制
- 仅在任务明确匹配上述范围时使用此技能。
- 不要将输出视为环境特定验证、测试或专家审查的替代品。
- 如果缺少必需的输入、权限、安全边界或成功标准,请停止并请求澄清。