Edit

Share via


Build a containerized Python web app in Azure

In this part of the tutorial series, you learn how to build a containerized Python web app directly in Azure Container Registry without installing Docker locally. Building the Docker image in Azure is often faster and easier than creating the image locally and then pushing it to the Azure Container Registry. Additionally, cloud-based image building eliminates the need for Docker to run in your development environment.

App Service enables you to run containerized web apps and deploy them through the continuous integration/continuous deployment (CI/CD) capabilities of Docker Hub, Azure Container Registry, and Visual Studio Team Services. This article is part 3 of a 5-part tutorial series about how to containerize and deploy a Python web app to Azure App Service. In this part of the tutorial, you learn how to build the containerized Python web app in Azure.

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. This article is part 3 of a 5-part tutorial series.

In part 2 of this tutorial series, you built and ran the container image locally. In contrast, in this part of the tutorial, you build (containerize) the same Python web app directly into a Docker image in the Azure Container Registry. Building the image in Azure is typically faster and easier than building locally and then pushing the image to a registry. Also, building in the cloud doesn't require Docker to be running in your dev environment.

Once the Docker image is in Azure Container Registry, it can be deployed to Azure App service.

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 have an existing Azure Container Registry you wish to use, skip this next step and proceed to the next step. Otherwise, create a new Azure Container Registry using the Azure CLI.

Azure CLI commands can be run 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 with 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.
    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.

  2. If you're using the Azure CLI on your local machine, execute the az acr login command to log in to the container registry.

    az acr login -n $REGISTRY_NAME
    

    The command adds "azurecr.io" to the name to create the fully qualified registry name. If successful, you see the message "Login Succeeded".

    Note

    In the Azure Cloud Shell, the az acr login command isn't necessary, as authentication is handled automatically through your Cloud Shell session. However, if you encounter authentication issues, you can still use it.

Build an image in Azure Container Registry

You can generate the container image directly in Azure through various approaches:

  • The Azure Cloud Shell allows 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 it in Azure from your local setup, without needing Docker to be running locally.

Azure CLI commands can be run in your local development environment with the Azure CLI installed or in Azure Cloud Shell.

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

  2. Build the container image using the az acr build command.

    az acr build -r $REGISTRY_NAME -g $RESOURCE_GROUP_NAME -t msdocspythoncontainerwebapp:latest .
    # When using Azure Cloud Shell, run one of the following commands instead:
    # 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
    # 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 last argument in the command is the fully qualified path to the repo. When running in Azure Cloud Shell, use https://github.com/Azure-Samples/msdocs-python-django-container-web-app.git for the Django sample app and https://github.com/Azure-Samples/msdocs-python-flask-container-web-app.git for the Flask sample app.

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

    az acr repository list -n $REGISTRY_NAME
    

Next step