หมายเหตุ
การเข้าถึงหน้านี้ต้องได้รับการอนุญาต คุณสามารถลอง ลงชื่อเข้าใช้หรือเปลี่ยนไดเรกทอรีได้
การเข้าถึงหน้านี้ต้องได้รับการอนุญาต คุณสามารถลองเปลี่ยนไดเรกทอรีได้
This article is part 3 of a 5-part tutorial series about containerizing and deploying a Python web app to Azure App Service. In part 2, you built and ran the container image locally. In this article, you build the same Python web app directly in Azure Container Registry without installing Docker locally. Building the image in Azure is typically faster and easier than building locally and then pushing it to a registry. Cloud-based image building also eliminates the need for Docker to be running in your development environment.
Azure App Service lets you deploy and run containerized web apps using CI/CD pipelines from platforms like Docker Hub, Azure Container Registry, and Azure DevOps. Once the Docker image is in Azure Container Registry, you can deploy it to Azure App Service.
Prerequisites
Before you begin, make sure you completed part 2 of this tutorial series, which covers:
- Cloning the sample repository (Django or Flask).
- Creating a resource group for Azure resources.
- Running the containerized app locally to verify it works.
You also need:
- An active Azure subscription. If you don't have one, create a free account.
- Azure CLI installed locally (for Azure CLI steps) or access to Azure Cloud Shell.
- Visual Studio Code with the Docker extension installed (for VS Code steps).
Important
The sample Dockerfile uses python:3.8-slim as the base image. Python 3.8 reached end-of-life in October 2024 and no longer receives security updates. Update your Dockerfile to use python:3.12-slim or python:3.13-slim for production deployments.
This service diagram highlights the components covered in this article.
Create an Azure Container Registry
If you already have an Azure Container Registry, skip this step and proceed to the next step. Otherwise, create a new Azure Container Registry by using the Azure CLI.
You can run Azure CLI commands in the Azure Cloud Shell or in your local development environment with the Azure CLI installed.
Note
Use the same names as in part 2 of this tutorial series.
Create an Azure container registry by using the az acr create command.
#!/bin/bash # Use the resource group that you created in part 2 of this tutorial series. RESOURCE_GROUP_NAME='msdocs-web-app-rg' # REGISTRY_NAME must be unique within Azure and contain 5-50 alphanumeric characters. # If the name is already taken, you'll receive an error. Choose a different name and retry. REGISTRY_NAME='msdocscontainerregistryname' echo "Creating Azure Container Registry $REGISTRY_NAME..." az acr create -g $RESOURCE_GROUP_NAME -n $REGISTRY_NAME --sku StandardIn the JSON output of the command, locate the
loginServervalue. This value represents the fully qualified registry name (all lowercase) and contains the registry name.Example output:
{ "loginServer": "msdocscontainerregistryname.azurecr.io", "name": "msdocscontainerregistryname", ... }If you're using the Azure CLI on your local machine, run the az acr login command to sign in to the container registry.
az acr login -n $REGISTRY_NAMEThe
-nparameter accepts either the short registry name (for example,msdocscontainerregistryname) or the fully qualified registry name (msdocscontainerregistryname.azurecr.io). The command authenticates Docker with Azure Container Registry using your Azure CLI credentials.Expected output:
Login SucceededNote
If you're using Azure Cloud Shell, you don't need to run the
az acr logincommand because your Cloud Shell session automatically handles authentication.
Build an image in Azure Container Registry
You can generate the container image directly in Azure through various approaches:
- The Azure Cloud Shell enables you to construct the image entirely in the cloud, independent of your local environment.
- Alternatively, you can use VS Code or the Azure CLI to create the image in Azure from your local setup, without needing Docker to be running locally.
You can run Azure CLI commands in your local development environment by using the Azure CLI installed or in Azure Cloud Shell.
In the console, go to the root folder for your cloned repository from part 2 of this tutorial series.
Build the container image by using the az acr build command.
Local development:
az acr build -r $REGISTRY_NAME -g $RESOURCE_GROUP_NAME -t msdocspythoncontainerwebapp:latest .Azure Cloud Shell:
If you're using Azure Cloud Shell, specify the GitHub repository URL instead of the local path (
.):# For Django sample: az acr build -r $REGISTRY_NAME -g $RESOURCE_GROUP_NAME -t msdocspythoncontainerwebapp:latest https://github.com/Azure-Samples/msdocs-python-django-container-web-app.git # For Flask sample: az acr build -r $REGISTRY_NAME -g $RESOURCE_GROUP_NAME -t msdocspythoncontainerwebapp:latest https://github.com/Azure-Samples/msdocs-python-flask-container-web-app.gitThe final argument (
.or the Git URL) is the Docker build context—the directory containing the Dockerfile and application files that Docker uses to build the image.Confirm the container image was created by using the az acr repository list command.
az acr repository list -n $REGISTRY_NAMEExpected output:
[ "msdocspythoncontainerwebapp" ]