Skip to main content

Cloud and deployment · Intermediate · 22 min

Run a container on Azure Container Apps with scale to zero

By Padding · Updated 18 Nov 2026
Azure Container Apps · Azure CLI 2.93

You will deploy a public container image to Azure Container Apps with the Azure CLI, let it scale to zero, feed it a Key Vault secret through a managed identity, split traffic between two revisions, read its logs and delete everything.

On this page

Azure Container Apps runs your containers without you managing servers or a Kubernetes cluster. You give it an image and a few settings, and it gives you an HTTPS address, scales replicas up and down with traffic, and can scale to zero replicas when nobody is calling, so an idle app has no usage charges. Underneath it runs on Kubernetes with KEDA for scaling and Envoy for ingress, but you never see the cluster.

In this tutorial you work entirely from the Azure CLI. You create an environment that sends logs to a Log Analytics workspace, deploy a public sample image, add an HTTP scale rule and watch it drop to zero, give the app a secret that lives in Key Vault and is read through a managed identity, send 20% of traffic to a new revision, read the logs, and delete the lot.

How it compares with App Service and Cloud Run

Microsoft positions Azure App Service as the home for web apps and APIs: you deploy code or a container to an App Service plan, and you pay for the plan's capacity rather than per request. It has deployment slots, built-in authentication and mature language runtimes. Container Apps suits general containers and groups of small services: it scales to zero, scales on queues and other events as well as HTTP, and splits traffic between revisions. If you have one web app and want a predictable monthly cost, App Service is simpler. If you want per-request scaling, background workers or several services that talk to each other, Container Apps fits better. The App Service with Bicep tutorial shows the other side.

Google Cloud Run is the closest equivalent on another cloud: both take an image, scale to zero, keep immutable revisions and split traffic by percentage. The main difference you notice day to day is grouping. Container Apps puts apps in a shared environment (one network and one log destination), while each Cloud Run service stands alone in a project and region. If you already know Cloud Run from deploying a container to Cloud Run, most ideas here carry over.

This costs money. On the Consumption plan, each subscription gets a monthly free grant of 180,000 vCPU-seconds, 360,000 GiB-seconds and 2 million requests (Azure pricing page, checked 26 November 2026). A small app that scales to zero usually stays inside it. Log Analytics charges for the data it ingests and Key Vault charges per operation, and neither is covered by that grant. Check azure.microsoft.com/pricing/details/container-apps before you start. The last step deletes everything.

Step 1 · Prepare the Azure CLI

Sign in and check your CLI version. This tutorial was written against Azure CLI 2.93:

Bash
az login
az version --query '"azure-cli"' -o tsv

The containerapp commands ship with the CLI, and the containerapp extension adds newer options on top. Install or update it, then register the two resource providers that Container Apps and Log Analytics use. Registration is once per subscription and takes a minute or two:

Bash
az extension add --name containerapp --upgrade
az provider register --namespace Microsoft.App --wait
az provider register --namespace Microsoft.OperationalInsights --wait

Now keep every name you use in one small file, so a new terminal can pick up where you left off. Pick a region first. This command lists the regions where you can create a Container Apps environment:

Bash
az provider show --namespace Microsoft.App \
  --query "resourceTypes[?resourceType=='managedEnvironments'].locations[]" -o tsv

If you serve users in Africa, look for South Africa North (southafricanorth) or South Africa West in that list; a nearby region cuts latency for users on mobile data. Choosing a cloud region in Africa covers the trade-offs. Replace southafricanorth below with a region from your list. The Key Vault name must be unique across all of Azure, so it gets a random number:

Bash
mkdir -p ~/aca-demo
cat > ~/aca-demo/env.sh <<EOF
RG=rg-aca-demo
LOCATION=southafricanorth
LAW=law-aca-demo
ENV_NAME=aca-demo-env
APP=hello-aca
ID_NAME=id-hello-aca
KV=kvacademo$RANDOM$RANDOM
EOF
source ~/aca-demo/env.sh
echo "$RG $LOCATION $KV"

If you open a new terminal later, run source ~/aca-demo/env.sh again before any other command.

Check it worked

az version prints 2.93.0 or later. az provider show --namespace Microsoft.App --query registrationState -o tsv prints Registered. The last command prints your resource group, region and a vault name such as kvacademo1234520831.

Step 2 · Create the resource group, workspace and environment

A resource group holds everything you create here, so deleting it later removes the lot. Create it and a Log Analytics workspace for the logs:

Bash
az group create --name "$RG" --location "$LOCATION" -o table
az monitor log-analytics workspace create \
  --resource-group "$RG" --workspace-name "$LAW" --location "$LOCATION" -o table

The environment needs the workspace's ID and its shared key. The key is a credential, so the command below fetches it inline instead of you pasting it, which keeps it out of your shell history:

Bash
LAW_ID=$(az monitor log-analytics workspace show \
  --resource-group "$RG" --workspace-name "$LAW" --query customerId -o tsv)

az containerapp env create \
  --name "$ENV_NAME" --resource-group "$RG" --location "$LOCATION" \
  --logs-workspace-id "$LAW_ID" \
  --logs-workspace-key "$(az monitor log-analytics workspace get-shared-keys \
      --resource-group "$RG" --workspace-name "$LAW" --query primarySharedKey -o tsv)"

echo "LAW_ID=$LAW_ID" >> ~/aca-demo/env.sh

Creating the environment takes a few minutes. Every app you put in it shares its network and sends its logs to this workspace.

Check it worked

Bash
az containerapp env show --name "$ENV_NAME" --resource-group "$RG" \
  --query properties.provisioningState -o tsv

It prints Succeeded. If it says InProgress, wait a minute and run it again.

Step 3 · Deploy a public image with ingress

Deploy Microsoft's public sample image. --ingress external gives the app a public HTTPS address, and --target-port 80 tells ingress which port the container listens on. Get the port wrong and the revision will start but every request fails, so always match it to your image:

Bash
az containerapp create \
  --name "$APP" --resource-group "$RG" --environment "$ENV_NAME" \
  --image mcr.microsoft.com/k8se/quickstart:latest \
  --ingress external --target-port 80 \
  --cpu 0.25 --memory 0.5Gi \
  --revision-suffix v1 \
  --query properties.configuration.ingress.fqdn -o tsv

The command prints a host name ending in azurecontainerapps.io. Save it:

Bash
FQDN=$(az containerapp show --name "$APP" --resource-group "$RG" \
  --query properties.configuration.ingress.fqdn -o tsv)
echo "FQDN=$FQDN" >> ~/aca-demo/env.sh
curl -s -o /dev/null -w '%{http_code}\n' "https://$FQDN"

--cpu 0.25 --memory 0.5Gi is the smallest size, which uses the free grant most slowly. Ingress terminates TLS for you, and insecure HTTP connections are not allowed unless you pass --allow-insecure true.

Check it worked

curl prints 200. Open https://<your FQDN> in a browser and you see the sample's welcome page.

Step 4 · Add an HTTP scale rule with zero minimum replicas

Without a rule, an app with ingress gets a default HTTP rule with 0 to 10 replicas. Set your own limits so a traffic spike cannot run up a bill, and set how many concurrent requests one replica should handle before Container Apps adds another. Scale settings belong to the revision, so this creates a new one:

Bash
az containerapp update \
  --name "$APP" --resource-group "$RG" \
  --min-replicas 0 --max-replicas 3 \
  --scale-rule-name http-load \
  --scale-rule-type http \
  --scale-rule-http-concurrency 20 \
  -o none

With --min-replicas 0, the first request after an idle spell waits while a replica starts. That cold start is longer for large images than for small ones. If that delay matters, set --min-replicas 1; that replica is billed even when idle, at a lower idle rate.

Send some traffic, then look at the replicas:

Bash
for i in $(seq 1 30); do curl -s -o /dev/null "https://$FQDN"; done
az containerapp replica list --name "$APP" --resource-group "$RG" -o table

Now leave the app alone. Container Apps waits a cool-down period of 300 seconds after the last request before it removes the final replica. After about six minutes, list the replicas again:

Bash
az containerapp replica list --name "$APP" --resource-group "$RG" -o table

Check it worked

The first replica list shows one replica (30 requests in a row are not enough to need a second). The list after six idle minutes is empty: the app is at zero replicas and has no usage charges. Load the page in a browser and it still answers, a little slower the first time, as a replica starts again.

Step 5 · Read a Key Vault secret through a managed identity

Apps need credentials, such as an API token for a payment provider or an SMS gateway. Do not pass them as plain environment variables or bake them into the image. Keep them in Key Vault, give the app a managed identity, and let that identity read the secret. No password for the vault ever leaves Azure.

Create a user-assigned identity and a Key Vault. New vaults use Azure role-based access control (RBAC), so even you need a role before you can write secrets:

Bash
az identity create --name "$ID_NAME" --resource-group "$RG" --location "$LOCATION" -o none
ID_RESOURCE=$(az identity show --name "$ID_NAME" --resource-group "$RG" --query id -o tsv)
ID_PRINCIPAL=$(az identity show --name "$ID_NAME" --resource-group "$RG" --query principalId -o tsv)

az keyvault create --name "$KV" --resource-group "$RG" --location "$LOCATION" -o none
KV_ID=$(az keyvault show --name "$KV" --query id -o tsv)

az role assignment create --role "Key Vault Secrets Officer" --scope "$KV_ID" \
  --assignee-object-id "$(az ad signed-in-user show --query id -o tsv)" \
  --assignee-principal-type User -o none
az role assignment create --role "Key Vault Secrets User" --scope "$KV_ID" \
  --assignee-object-id "$ID_PRINCIPAL" \
  --assignee-principal-type ServicePrincipal -o none

Secrets Officer lets you manage secrets; Secrets User only lets the app's identity read them. Role assignments can take a few minutes to apply. If the next command fails with Forbidden, wait two minutes and run it again.

Generate a demo token straight into a file that only you can read, store it in the vault, and delete the local copy. In your own project, put your real token in that file instead; it never appears on the command line:

Bash
(umask 077; openssl rand -hex 24 > ~/aca-demo/api-token.txt)
az keyvault secret set --vault-name "$KV" --name api-token \
  --file ~/aca-demo/api-token.txt -o none
rm ~/aca-demo/api-token.txt
SECRET_URI="https://$KV.vault.azure.net/secrets/api-token"

Now attach the identity to the app, define an app secret that points at the vault, and expose it to the container as an environment variable. The URI has no version on the end, so the app follows the latest version of the secret:

Bash
az containerapp identity assign --name "$APP" --resource-group "$RG" \
  --user-assigned "$ID_RESOURCE" -o none

az containerapp secret set --name "$APP" --resource-group "$RG" \
  --secrets "api-token=keyvaultref:$SECRET_URI,identityref:$ID_RESOURCE" -o none

az containerapp update --name "$APP" --resource-group "$RG" \
  --set-env-vars "API_TOKEN=secretref:api-token" -o none

Secrets belong to the app, not to a revision, so changing one does not restart anything on its own. The update that adds the environment variable creates a new revision, and that revision reads the secret when it starts.

Check it worked

Bash
az containerapp secret list --name "$APP" --resource-group "$RG" -o table
az containerapp show --name "$APP" --resource-group "$RG" \
  --query "properties.template.containers[0].env" -o table

The first table lists api-token with your vault URL and no value. The second shows API_TOKEN with the secret reference api-token. The app still loads in a browser. If the identity could not read the vault, the new revision fails to provision, and the system logs in the logs step say why.

Step 6 · Release a new revision to 20% of traffic

In the default single revision mode, every update replaces the running revision. To test a change on a share of real traffic first, switch to multiple revision mode. Note the revision now serving traffic, then pin 100% of traffic to it by name. Without that pin, traffic goes to whichever revision is latest, so your new revision would receive everything the moment it starts:

Bash
az containerapp revision set-mode --name "$APP" --resource-group "$RG" --mode multiple -o none

STABLE=$(az containerapp show --name "$APP" --resource-group "$RG" \
  --query properties.latestRevisionName -o tsv)
echo "Stable revision: $STABLE"

az containerapp ingress traffic set --name "$APP" --resource-group "$RG" \
  --revision-weight "$STABLE=100" -o none

Deploy the change as a new revision with a readable suffix. Here the change is one environment variable; in practice it is usually a new image tag:

Bash
az containerapp update --name "$APP" --resource-group "$RG" \
  --revision-suffix canary \
  --set-env-vars "RELEASE=canary" -o none

Now send 20% of requests to the new revision and keep 80% on the stable one. The weights must add up to 100:

Bash
az containerapp ingress traffic set --name "$APP" --resource-group "$RG" \
  --revision-weight "$STABLE=80" latest=20 -o none

When you are happy, move everything over with --revision-weight latest=100. To roll back, set "$STABLE=100" again; the stable revision never stopped running.

Check it worked

Bash
az containerapp ingress traffic show --name "$APP" --resource-group "$RG" -o table
az containerapp revision list --name "$APP" --resource-group "$RG" \
  --query "[].{name:name, active:properties.active, traffic:properties.trafficWeight}" -o table

The traffic table shows two entries, your stable revision at 80 and the latest at 20. The revision list shows both revisions as active, one whose name ends in canary.

Step 7 · Read the logs

az containerapp logs show streams logs straight from the app. It reads one revision, replica and container at a time, the latest revision by default. System logs record what the platform did, such as revisions provisioned and traffic weights set:

Bash
az containerapp logs show --name "$APP" --resource-group "$RG" --type system --tail 30

Console logs are what your container writes to stdout and stderr. Add --follow to keep the stream open while you load the page in a browser, and press Ctrl+C to stop. The command needs a running replica, so if the app has scaled to zero, load the page first:

Bash
curl -s -o /dev/null "https://$FQDN"
az containerapp logs show --name "$APP" --resource-group "$RG" --type console --follow

The workspace keeps the history for every app in the environment, and you can query it across revisions. Logs reach the workspace a few minutes after they are written. The CLI may ask to install an extension the first time you run this; accept it:

Bash
az monitor log-analytics query --workspace "$LAW_ID" \
  --analytics-query "ContainerAppSystemLogs_CL | where ContainerAppName_s == '$APP' | project TimeGenerated, RevisionName_s, Log_s | order by TimeGenerated desc | take 20" \
  -o table

Check it worked

The system log stream shows lines about revisions being created and provisioned, including the canary revision and a traffic weight of 20%. The Log Analytics query returns a table of the same kind of messages with a time and a revision name on each row. An empty table usually means the logs have not arrived yet; wait five minutes and run the query again.

Step 8 · Delete everything

This deletes every resource in the resource group, including anything you added to rg-aca-demo yourself. Check that the group holds only what this tutorial created:

Bash
az resource list --resource-group "$RG" --query "[].{name:name, type:type}" -o table

You should see the workspace, the environment, the container app, the identity and the vault. Then delete the group. It takes several minutes:

Bash
az group delete --name "$RG" --yes

Key Vault keeps a deleted vault in a soft-deleted state for 90 days by default, and its name stays reserved during that time. To remove it now, purge it (this cannot be undone), then remove your local files:

Bash
az keyvault list-deleted --query "[].name" -o tsv
az keyvault purge --name "$KV"
rm -r ~/aca-demo

Check it worked

az group exists --name rg-aca-demo prints false. az keyvault list-deleted --query "[].name" -o tsv no longer lists your vault name. Over the next few days, check Cost Management in the Azure portal to confirm no new charges appear for this group.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.