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.
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.