แก้ไข

Build a containerized Python web app in Azure

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:

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.

A screenshot of the services using in the Tutorial - Containerized Python App on Azure with the build-in-cloud path highlighted.

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.

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

    In the JSON output of the command, locate the loginServer value. This value represents the fully qualified registry name (all lowercase) and contains the registry name.

    Example output:

    {
      "loginServer": "msdocscontainerregistryname.azurecr.io",
      "name": "msdocscontainerregistryname",
      ...
    }
    
  2. 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_NAME
    

    The -n parameter 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 Succeeded
    

    Note

    If you're using Azure Cloud Shell, you don't need to run the az acr login command 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.

  1. In the console, go to the root folder for your cloned repository from part 2 of this tutorial series.

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

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

  3. Confirm the container image was created by using the az acr repository list command.

    az acr repository list -n $REGISTRY_NAME
    

    Expected output:

    [
      "msdocspythoncontainerwebapp"
    ]
    

Next step