How can I update the App Service container registry settings programmatically

MarkWelsh 0 Reputation points
2025-01-07T17:41:26.71+00:00

I would like to programmatically change the container settings for an App Service resource using the Python SDK.

I can see how to update to use code from Github: https://learn.microsoft.com/en-us/azure/developer/python/sdk/examples/azure-sdk-example-web-app?tabs=cmd but not how to do the same thing using a container registry.

Is this possible through the SDK?

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,243 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sampath 15 Reputation points Microsoft Vendor
    2025-02-04T12:45:50.4533333+00:00

    Hi MarkWelsh,

    Thank you for reaching out to MS Q&A.

    Yes, you can update the App Service container registry settings programmatically using the Azure SDK for Python.

    Below is a Python script that configures an Azure App Service to use a container from Azure Container Registry (ACR):

    
    import random
    
    from azure.identity import DefaultAzureCredential
    
    from azure.mgmt.resource import ResourceManagementClient
    
    from azure.mgmt.web import WebSiteManagementClient
    
    SUBSCRIPTION_ID = "SUBSCRIPTION_ID"
    
    RESOURCE_GROUP_NAME = "RESOURCE_GROUP_NAME"
    
    LOCATION = "centralus"
    
    SERVICE_PLAN_NAME = "PythonAzureExample-WebApp-plan"
    
    WEB_APP_NAME = f"PythonContainerApp-{random.randint(1000,9999)}"
    
    ACR_SERVER = "ACRName.azurecr.io"
    
    ACR_USERNAME = "ACRName"
    
    ACR_PASSWORD = "ACR_PASSWORD"
    
    CONTAINER_IMAGE = f"{ACR_SERVER}/flask-demo:v1"
    
    credential = DefaultAzureCredential()
    
    resource_client = ResourceManagementClient(credential, SUBSCRIPTION_ID)
    
    app_service_client = WebSiteManagementClient(credential, SUBSCRIPTION_ID)
    
    print("Creating/updating resource group...")
    
    resource_client.resource_groups.create_or_update(RESOURCE_GROUP_NAME, {"location": LOCATION})
    
    print("Creating/updating App Service Plan...")
    
    app_service_client.app_service_plans.begin_create_or_update(
    
        RESOURCE_GROUP_NAME,
    
        SERVICE_PLAN_NAME,
    
        {
    
            "location": LOCATION,
    
            "reserved": True,  # Linux plan (required for containers)
    
            "sku": {"name": "B1", "tier": "Basic"}
    
        }
    
    ).result()
    
    print(f"Creating/updating Web App: {WEB_APP_NAME}...")
    
    web_app = app_service_client.web_apps.begin_create_or_update(
    
        RESOURCE_GROUP_NAME,
    
        WEB_APP_NAME,
    
        {
    
            "location": LOCATION,
    
            "server_farm_id": f"/subscriptions/{SUBSCRIPTION_ID}/resourceGroups/{RESOURCE_GROUP_NAME}/providers/Microsoft.Web/serverfarms/{SERVICE_PLAN_NAME}",
    
            "site_config": {
    
                "linux_fx_version": f"DOCKER|{CONTAINER_IMAGE}",
    
                "app_settings": [
    
                    {"name": "DOCKER_REGISTRY_SERVER_URL", "value": f"https://{ACR_SERVER}"},
    
                    {"name": "DOCKER_REGISTRY_SERVER_USERNAME", "value": ACR_USERNAME},
    
                    {"name": "DOCKER_REGISTRY_SERVER_PASSWORD", "value": ACR_PASSWORD},
    
                    {"name": "WEBSITES_PORT", "value": "80"},  # Set the exposed port if necessary
    
                ],
    
            },
    
        }
    
    ).result()
    
    print(f"Web App {WEB_APP_NAME} deployed successfully!")
    
    web_app_info = app_service_client.web_apps.get(RESOURCE_GROUP_NAME, WEB_APP_NAME)
    
    print(f"Web App URL: https://{web_app_info.default_host_name}")
    
    

    Steps to Use This Code:

    Create a Container and Push It to ACR . Ensure your container image is built and pushed to ACR:

    az acr login --name youracr
    
    docker tag your-image:v1 youracr.azurecr.io/your-image:v1
    
    docker push youracr.azurecr.io/your-image:v1
    

    Alternatively, refer to this GitHub repository for managing Azure Container Registry using the Python SDK.

    Run the above script and your App Service will be updated with the new container image.

    web app

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it.

    If you have any further questions about this answer, please click Comment.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.