如何使用Azure Container Registry Python API列出所有ACR中镜像的标签?
Got it, let's tackle this problem step by step! Here's how you can list tags for every image in your Azure Container Registry using Python, along with context on why the API might feel tricky:
Option 1: Reuse Azure CLI Commands via Python
Since you already know the working CLI command, a simple approach is to execute it directly in Python using the subprocess module. This skips the SDK boilerplate if you just need a quick solution.
Here's a script that fetches all repositories first, then pulls tags for each:
import subprocess import json def get_all_acr_repositories(registry_name): cmd = f"az acr repository list --name {registry_name} --output json" result = subprocess.run(cmd, shell=True, capture_output=True, text=True) if result.returncode == 0: return json.loads(result.stdout) print(f"Failed to fetch repositories: {result.stderr}") return [] def get_tags_for_repo(registry_name, repo_name): cmd = f"az acr repository show-tags --repository {repo_name} --name {registry_name} --output json" result = subprocess.run(cmd, shell=True, capture_output=True, text=True) if result.returncode == 0: return json.loads(result.stdout) print(f"Failed to fetch tags for {repo_name}: {result.stderr}") return [] # Replace with your registry name REGISTRY_NAME = "your-registry-name" repos = get_all_acr_repositories(REGISTRY_NAME) for repo in repos: print(f"Repository: *{repo}*") tags = get_tags_for_repo(REGISTRY_NAME, repo) if tags: print("Tags:") for tag in tags: print(f" - `{tag}`") else: print(" No tags found.") print("---")
Option 2: Use the Official Azure Python SDK
If you prefer a fully programmatic approach without shell commands, you can use the azure-mgmt-containerregistry package. The API feels complex because it requires explicit authentication and ties into Azure's resource management model (resource groups, subscriptions), which adds boilerplate compared to the CLI's session-based auth.
First, install the required packages:
pip install azure-mgmt-containerregistry azure-identity
Then use this script:
from azure.identity import DefaultAzureCredential from azure.mgmt.containerregistry import ContainerRegistryManagementClient # Replace with your Azure details SUBSCRIPTION_ID = "your-subscription-id" RESOURCE_GROUP = "your-resource-group-name" REGISTRY_NAME = "your-registry-name" # Authenticate using your existing Azure session (CLI, env vars, etc.) credential = DefaultAzureCredential() client = ContainerRegistryManagementClient(credential, SUBSCRIPTION_ID) # Fetch all repositories in the registry repositories = client.repositories.list( resource_group_name=RESOURCE_GROUP, registry_name=REGISTRY_NAME ) for repo in repositories: # Extract just the repo name (the SDK returns a full resource ID) repo_name = repo.name.split("/")[-1] print(f"Repository: *{repo_name}*") # Fetch tags for the repository tags = client.repositories.list_tags( resource_group_name=RESOURCE_GROUP, registry_name=REGISTRY_NAME, repository_name=repo_name ) tag_list = [tag.name for tag in tags] if tag_list: print("Tags:") for tag in tag_list: print(f" - `{tag}`") else: print(" No tags found.") print("---")
Why the Python API feels "complex"
The Azure SDK is built to cover every feature of Azure services, so it requires you to explicitly handle authentication and specify resource context (like resource groups) that the CLI handles automatically via your logged-in session. It's more verbose, but gives you full control for integration with larger Python applications.
内容的提问来源于stack exchange,提问作者Mehdi Khlifi

