Azure container registry detail using Azure SDK for Python

218 views Asked by At

As we know that to create a environment in Azure Machine Learning Workspace we need Conda dependencies file and base image. In my case you can see.

Build process using this conda file and base image will result into create of Image in Azure Container Registry whose url you can see here in portal.

I just want to know how to get Azure container registry url for this environment as azure portal is showing this information dashboard but when I am using Azure SKD for Python this is information is missing but all remaining information we are getting, even I have tried to print dict of Environment Object but no information is present. Any idea is appreciated.

Azure SDK for Python Azure ML Workspace

Using this code you can print or get the information of particular environment.

environment = ml_client.environments.get(name=env_name, version="1")
print(environment)

But the information related to Azure Container Registry. I know that code is not implemented by Microsoft as I was going through source code of SDK. If you have another way of implementation i just want that.

2

There are 2 answers

0
Davinder Singh On BEST ANSWER

I was searching a lot around Azure SDK for Python v2 under Environment class but this object does contain any information about registered ACR image.

Then I tried AzureML SDK under Environment class we can get the ACR image information and here is the code;

from azureml.core import Workspace
from azureml.core.environment import Environment
from azureml.core.authentication import AzureCliAuthentication

credential = AzureCliAuthentication()

workspace = Workspace.get(
    name=workspace_name,
    subscription_id=subscription_id,
    resource_group=resource_group_name,
    auth=credential)

environment = Environment.get(workspace=workspace, name="longformer", version="1")
details = environment.get_image_details(workspace)
container_repository = details["dockerImage"]["name"]
print(container_repository)

This will print you acr image for given environment name (for my case longformer) and environment version (for my case 1) and output will look like this:

azureml/azureml_33d36abd731aa265b0dce682632fc38b

Output

---

I did one Custom Implementation to get ACR hash this the same Microsoft is trying to do under the hood I just concise it all credits goes to Microsoft Team:

def get_image_details(env_name, env_version, subscription_id, resource_group_name, providers, workspace_name, headers):

    cluster_address = "https://centralus.experiments.azureml.net"
    workspace_address = f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/{providers}/workspaces/{workspace_name}"

    environment_url = cluster_address + "/environment/v1.0" + workspace_address + f"/environments/{env_name}/versions/{env_version}" + "/image"

    response = requests.get(environment_url, headers=headers)

    if response.status_code == 200:
        return response.json().get("dockerImage").get("name")
    else:
        message = "Error getting image details. Code: {}\n: {}".format(response.status_code,import requests
from azureml.core.authentication import AzureCliAuthentication

env_name = "longformer"
env_version = "1"

subscription_id = "c13d3595-ec03-436d-b30a-b21c2b16d804"
resource_group_name = "enlp-rg-ml-lowerenv"
providers = "Microsoft.MachineLearningServices"
workspace_name = "enlp-ml-workspace-lowerenv"response.text)
        raise Exception(message)
2
Rishabh Meshram On

Based on the documentation, the Azure ML Python SDK Environment properties does not provide a direct way to get the Azure Container Registry URL for the environment.

However, one possible alternate can be with azure-containerregistry package.

You can use this to list all the URLs for a particular container registry.

import asyncio
import os
from azure.containerregistry.aio import ContainerRegistryClient
from azure.identity.aio import DefaultAzureCredential

# Create a new ContainerRegistryClient      
audience = ""
account_url = "<-URL->"
credential = DefaultAzureCredential()
client = ContainerRegistryClient(account_url, credential, audience=audience)
async for repository_name in client.list_repository_names():
    print(repository_name)

enter image description here

enter image description here

For more details you can check this documentation.