Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 57 ยท Containers

Azure Container Apps

Run containers without managing servers: Azure Container Apps environments, revisions, ingress, KEDA scale rules, scale to zero, Dapr and jobs.

You have a container image in a registry. Now you need somewhere to run it, with HTTPS, a public address, more copies when traffic rises, and ideally no bill when nobody is using it. Azure Container Apps is a serverless container platform built for this. You give it an image and a few settings. Azure runs the containers, gives them an endpoint, scales them up and down, and keeps old versions around so you can shift traffic gradually. Under the hood it runs on Kubernetes, but you never touch Kubernetes objects.

Container Apps sits between App Service (code or a single container, simple) and AKS (full Kubernetes, powerful and demanding). It is a good default for APIs, web front ends, background workers and event-driven services. The real service needs an Azure account, so its commands and YAML are read-only here. The runnable examples are JavaScript stand-ins for two behaviors that are easy to misunderstand: traffic splitting between revisions, and scaling to zero.

Environments and apps

Two resource types matter:

  • A Container Apps environment is the shared boundary. Apps in one environment share a virtual network, can find each other by name, and send logs to the same Log Analytics workspace. You usually create one environment per stage (dev, test, prod) or per team.
  • A container app is one deployable service inside an environment.
Terminal
az containerapp env create \
  --name shop-env \
  --resource-group containers-rg \
  --location westeurope

az containerapp create \
  --name shop-api \
  --resource-group containers-rg \
  --environment shop-env \
  --image contososhopacr.azurecr.io/shop/api:1.0 \
  --registry-server contososhopacr.azurecr.io \
  --registry-identity system \
  --target-port 8080 \
  --ingress external \
  --min-replicas 0 \
  --max-replicas 10

The --registry-identity system flag lets the app pull from your registry with its managed identity, as in Azure Container Registry. You must also give that identity the AcrPull role. Environments run on a Consumption plan (you pay for what you use) and can add workload profiles with dedicated, reserved hardware for steady or demanding workloads.

Revisions

Every time you change what a container app runs, Azure creates a revision: an immutable snapshot of the template (image, environment variables, resources, scale rules). Revisions are named <app>--<suffix>, for example shop-api--v2.

Changes to application-level settings, such as secrets, ingress settings and traffic weights, do not create a new revision. That is what lets you move traffic between revisions without deploying anything.

The app has a revision mode:

  • Single (default): only the latest revision is active, and Azure switches over once the new one is healthy.
  • Multiple: several revisions can be active at once, and you decide how traffic is split between them.
flowchart LR
  U["Users"] --> I["Ingress<br/>HTTPS endpoint"]
  I -- "90%" --> R1["Revision shop-api--v1"]
  I -- "10%" --> R2["Revision shop-api--v2"]

In multiple-revision mode, ingress sends a weighted share of requests to each active revision.

Ingress

Ingress gives the app an HTTPS endpoint. external ingress is reachable from the internet; internal ingress is reachable only from inside the environment's virtual network. targetPort is the port your container listens on, and Azure terminates TLS in front of it. Each app gets a generated address under the environment's domain, and you can add a custom domain with a certificate later.

Services in the same environment can call each other by app name, which makes a small microservice system easy to wire up without exposing every service publicly.

A traffic-splitting YAML

You can describe a whole app in YAML and apply it with az containerapp update --yaml. Here is a shortened example (read-only) that runs two revisions and scales on both HTTP load and queue length:

YAML
properties:
  configuration:
    activeRevisionsMode: Multiple
    ingress:
      external: true
      targetPort: 8080
      traffic:
        - revisionName: shop-api--v1
          weight: 90
        - revisionName: shop-api--v2
          weight: 10
  template:
    revisionSuffix: v2
    containers:
      - name: api
        image: contososhopacr.azurecr.io/shop/api:2.0
        resources:
          cpu: 0.5
          memory: 1Gi
    scale:
      minReplicas: 0
      maxReplicas: 10
      rules:
        - name: http-load
          http:
            metadata:
              concurrentRequests: "50"
        - name: orders-queue
          azureQueue:
            queueName: orders
            queueLength: "20"
            auth:
              - secretRef: queue-connection
                triggerParameter: connection

Traffic weights must add up to 100. With the CLI the same idea is az containerapp ingress traffic set --revision-weight shop-api--v1=90 shop-api--v2=10.

Splitting traffic: a stand-in

Azure's ingress is not something you can run here. The stand-in below keeps only the weighted split: 1,000 requests are assigned to revisions according to their weights, using a repeating pattern of 100 slots so the result is exact and repeatable. It then simulates a rollout, with a canary at 10%, a half-and-half step, a full cutover, and finally a rollback because the new revision turns out to be bad.

JS Weighted traffic between revisions

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The real platform spreads requests by its own balancing logic, so the exact numbers will be close to the weights rather than perfectly exact. The idea to take away is that a rollback is only a weight change: the old revision is still there.

Scale rules and scale to zero

Container Apps scaling is powered by KEDA (Kubernetes Event-driven Autoscaling). You attach one or more scale rules to the template and set minReplicas and maxReplicas. The platform adds replicas when a rule says the load is high and removes them when it falls.

Common rule types:

  • HTTP: target a number of concurrent requests per replica. For example, 50 means "add a replica when the average gets above about 50 concurrent requests per replica".
  • TCP: the same idea for TCP connections.
  • Custom (KEDA scalers): queue length in Storage Queues or Service Bus, event hub lag, a database query result, a cron schedule and many more.
  • CPU or memory: resource-based rules. These cannot scale an app to zero.

With minReplicas: 0 an app with no traffic has no running replicas and costs nothing in compute (you may still pay for things like storage or a reserved profile). When the next request or message arrives, the platform starts a replica. That first request waits for the container to start, called a cold start. If cold starts hurt, set minReplicas: 1. After load disappears, scale-in waits for a cooldown period (about five minutes by default) before going back to zero, so a brief pause in traffic does not cause thrashing.

The scaling idea is the same one you meet in Scaling and Autoscale, with one big difference: the lower bound can be zero.

Scaling with a cooldown: a stand-in

The stand-in below plays the role of the scaler. Traffic arrives per minute; each replica handles 10 concurrent requests (an invented number); v1 keeps one replica warm and v2 is allowed to go to zero after a two-minute cooldown (shorter than the real default so the output stays short). A column marks cold starts.

JS Scale a revision up and to zero

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at minute 2: v2 was at zero, so the first load starts a replica from nothing. Look at the quiet minutes at the end: v2 waits for the cooldown and then drops to zero, while v1 stays at its minimum of one.

Dapr and secrets

Container Apps has built-in support for Dapr, a runtime of sidecar building blocks for distributed apps: service-to-service calls, state stores, publish/subscribe and bindings. You enable it per app (--enable-dapr --dapr-app-id shop-api --dapr-app-port 8080) and your code talks to a local sidecar over HTTP or gRPC instead of to each service's SDK. It is optional; many Container Apps never use it.

Secrets such as connection strings are stored on the app and referenced by name (secretRef). Better still, store them in Key Vault (see Key Vault) and use a managed identity (Managed Identities and Service Principals) so that no secret value lives in your configuration.

Jobs

Not every container is a web server. A Container Apps job runs a container to completion, then stops. There are three trigger types:

Terminal
# Run on demand
az containerapp job create --name reindex --resource-group containers-rg \
  --environment shop-env --trigger-type Manual \
  --image contososhopacr.azurecr.io/shop/reindex:1.0 \
  --replica-timeout 1800 --replica-retry-limit 1

# Run every night at 02:00 UTC
az containerapp job create --name nightly-report --resource-group containers-rg \
  --environment shop-env --trigger-type Schedule --cron-expression "0 2 * * *" \
  --image contososhopacr.azurecr.io/shop/report:1.0

# Run when messages arrive (event-driven, uses scale rules)

Jobs suit migrations, batch processing, scheduled reports and queue workers that should exist only while there is work. You pay only for the time they run.

When to choose Container Apps

Choose it when you have containers and want HTTPS, autoscaling and revision-based releases without running a cluster. Choose App Service for simpler web apps, and Azure Kubernetes Service when you need the full Kubernetes API, custom controllers or very fine control. The wider comparison is in Choosing a Compute Service.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which change does NOT create a new revision?

    Image, environment variables and scale rules belong to the template, so changing them creates a revision. Traffic weights are an application-level setting.
  2. 2. What happens to an app with minReplicas set to 0 when no requests arrive for a while?

    After the cooldown, a revision with no load runs no replicas. The next request starts one, which adds startup delay.
  3. 3. You want to try a new version on 5% of users and roll back instantly if it fails. What do you need?

    Multiple revision mode keeps old and new revisions active, and the weights (for example 95 and 5) control the split. Rolling back means setting the old revision to 100.
  4. 4. What is a Container Apps environment?

    Apps in the same environment share a virtual network and logging destination and can reach each other by name.
  5. 5. Which workload is the best fit for a Container Apps job?

    Jobs run a container to completion on a manual, scheduled or event trigger, so you pay only while they run.

Summary

  • A Container Apps environment is the shared boundary; a container app is one service inside it, run on Kubernetes you never manage.
  • A revision is an immutable snapshot of the template; multiple-revision mode lets you split traffic by weight and roll back by changing weights.
  • Ingress provides HTTPS, either external or internal, in front of your targetPort.
  • KEDA-based scale rules (HTTP, TCP, queue length and more) set the replica count between minReplicas and maxReplicas; HTTP and event rules can scale to zero, CPU and memory rules cannot.
  • Scaling to zero saves money but causes cold starts; use minReplicas: 1 when latency matters.
  • Dapr is an optional sidecar runtime, and jobs run containers to completion on manual, schedule or event triggers.

Next: Azure Kubernetes Service.