Quickstart: Build and deploy from local source code to Azure Container Apps

This article demonstrates how to build and deploy a microservice to Azure Container Apps from local source code using the programming language of your choice. In this quickstart, you create a backend web API service that returns a static collection of music albums.

The following screenshot shows the output from the album API service you deploy.

Screenshot of response from albums API endpoint.

Prerequisites

To complete this project, you need the following items:

Requirement Instructions
Azure account If you don't have one, create a trial subscription. You need the Contributor or Owner permission on the Azure subscription to proceed.

Refer to Assign Azure roles using the Azure portal for details.
Git Install Git.
Azure CLI Install the Azure CLI.

Setup

To sign in to Azure from the CLI, run the following command and follow the prompts to complete the authentication process.

az cloud set -n AzureChinaCloud
az login
# az cloud set -n AzureCloud   //means return to Public Azure.

To ensure you're running the latest version of the CLI, run the upgrade command.

az upgrade

Next, install or update the Azure Container Apps extension for the CLI.

az extension add --name containerapp --upgrade --allow-preview true

Now that the current extension is installed, register the Microsoft.App and Microsoft.OperationalInsights namespaces.

az provider register --namespace Microsoft.App
az provider register --namespace Microsoft.OperationalInsights

Create environment variables

Now that your CLI setup is complete, you can define the environment variables that are used throughout this article. Now that your CLI setup is complete, you can define the environment variables that are used throughout this article.

Define the following variables in your bash shell.

export RESOURCE_GROUP="album-containerapps"
export LOCATION="canadacentral"
export ENVIRONMENT="env-album-containerapps"
export API_NAME="album-api"

Get the sample code

Run the following command to clone the sample application in the language of your choice and change into the project source folder.

git clone https://github.com/azure-samples/containerapps-albumapi-csharp.git
cd containerapps-albumapi-csharp/src

Build and deploy the container app

First, run the following command to create the resource group that will contain the resources you create in this quickstart.

az group create --name $RESOURCE_GROUP --location $LOCATION

Build and deploy your first container app with the containerapp up command. This command will:

  • Create the resource group
  • Create an Azure Container Registry
  • Build the container image and push it to the registry
  • Create the Container Apps environment with a Log Analytics workspace
  • Create and deploy the container app using the built container image

The up command uses the Dockerfile in project folder to build the container image. The EXPOSE instruction in the Dockerfile defines the target port, which is the port used to send ingress traffic to the container.

In the following code example, the . (dot) tells containerapp up to run in the current directory of the project that also contains the Dockerfile.

az containerapp up \
  --name $API_NAME \
  --resource-group $RESOURCE_GROUP \
  --location $LOCATION \
  --environment $ENVIRONMENT \
  --source .

Note

If the command returns an error with the message "AADSTS50158: External security challenge not satisfied", run az login --scope https://graph.chinacloudapi.cn//.default to log in with the required permissions and then run the az containerapp up command again.

Verify deployment

Locate the container app's URL in the output of the az containerapp up command. Navigate to the URL in your browser. Add /albums to the end of the URL to see the response from the API.

Screenshot of response from albums API endpoint.

Limits

The maximum size for uploading source code is 200MB. If the upload goes over the limit, error 413 is returned.

Clean up resources

If you're not going to continue on to the Deploy a frontend tutorial, you can remove the Azure resources created during this quickstart with the following command.

Caution

The following command deletes the specified resource group and all resources contained within it. If the group contains resources outside the scope of this quickstart, they are also deleted.

az group delete --name $RESOURCE_GROUP

Tip

Having issues? Let us know on GitHub by opening an issue in the Azure Container Apps repo.

Next steps

After completing this quickstart, you can continue to Tutorial: Communication between microservices in Azure Container Apps to learn how to deploy a front end application that calls the API.