How to rollback a Container App revision using the Azure CLI

Posted by Jason on Sunday, March 13, 2022

Earlier this month I posted How to rollback a Container App revision which focused on using the Azure Portal UI. In this entry I want to go over the same demo scenario using the Azure CLI.

Prerequisites for this walkthrough are to follow the steps in the Quickstart first: Deploy your first container app (Azure CLI), and insert the following steps before the last Clean up resources step.

Rollback a container app revision using the Azure CLI

At this point you’ve created the Container App and verified the deployment in the browser, now let’s look at the list of revisions using the containerapp revision list command.

In order to get a list of revisions that shows the same Name, Date Created, Provision Status, Traffic and Active columns shown in the portal, run the folllowing command (NOTE: keep this command handy, you will need to use it later):

az containerapp revision list \
  --name my-container-app \
  --resource-group $RESOURCE_GROUP \
  --query "[].{Name:name,DateCreated:createdTime,ProvisionStatus:provisioningState,Traffic:trafficWeight,Active:active}"

This will return a list of the current revisions:

Name                       DateCreated                ProvisionStatus    Traffic    Active
-------------------------  -------------------------  -----------------  ---------  --------
my-container-app--zdazk8c  2022-03-13T21:40:14+00:00  Provisioned        100        True

You should only see one revision at this time. Now we want to create a new revision, but since we can’t create a new revision using the same settings as the last revision (or at least I didn’t have any luck doing it) we’ll change the image to the ACI hello world image. Having another image also allows you to see which revision the traffic is going to easier.

To create a new revision, run this command:

az containerapp update \
  --name my-container-app \
  --resource-group $RESOURCE_GROUP \
  --image mcr.microsoft.com/azuredocs/aci-helloworld:latest

Once the CLI returns, try running the az containerapp revision list that you ran earlier again. You should now have two revisions:

Name                       DateCreated                ProvisionStatus    Traffic    Active
-------------------------  -------------------------  -----------------  ---------  --------
my-container-app--zdazk8c  2022-03-13T21:40:14+00:00  Provisioned        0          True
my-container-app--oscswcb  2022-03-13T21:45:27+00:00  Provisioned        100        True

This is the point where we determine how we could rollback to the previous version quickly.

Option 1 - Change the traffic quickly to go to the old revision

Just like in the Azure Portal demo, you can modify the traffic percent to the old revision to 100% and leave the new revision active. This will allow you to troubleshoot the revision if you need to.

Change the traffic by running this command (NOTE: you will need to put your revision names in the –traffic-weight argument):

az containerapp update --name my-container-app \
  --resource-group my-container-apps \
  --traffic-weight my-container-app--zdazk8c=100,my-container-app--oscswcb=0

Once the CLI returns, run the az containerapp revision list command from earlier again.

Name                       DateCreated                ProvisionStatus    Traffic    Active
-------------------------  -------------------------  -----------------  ---------  --------
my-container-app--zdazk8c  2022-03-13T21:40:14+00:00  Provisioned        100        True
my-container-app--oscswcb  2022-03-13T21:45:27+00:00  Provisioned        0          True

Option 2 - Change the traffic quickly to go to the old revision and deactivate the new version

This option starts with the same command as Option 1, and also uses a command to deactivate the revision. (NOTE: you will need to put your revision names in the –traffic-weight argument and the deactivate command)

az containerapp update --name my-container-app \
  --resource-group my-container-apps \
  --traffic-weight my-container-app--zdazk8c=100,my-container-app--oscswcb=0

az containerapp revision deactivate \
  --name my-container-app--oscswcb \
  --resource-group $RESOURCE_GROUP 

Just to verify the revision has been deactivated, run the az containerapp revision list command one more time. You should now see the second revision is not active.

Name                       DateCreated                ProvisionStatus    Traffic    Active
-------------------------  -------------------------  -----------------  ---------  --------
my-container-app--zdazk8c  2022-03-13T21:40:14+00:00  Provisioned        100        True
my-container-app--oscswcb  2022-03-13T21:45:27+00:00  Provisioned        0          False

Option 3 - Change the traffic quickly to go to the old revision

One more option with the Azure CLI is to use the az containerapp update --yaml argument.

If you run the az containerapp update -h command, you can see the description of this --yaml argument is:

Path to a .yaml file with the configuration of a containerapp. All other parameters will be ignored.

In order to create a yaml file, run this command:

az containerapp show \
  --name my-container-app \
  --resource-group $RESOURCE_GROUP \
  --output yaml \
  > my-container-app.yaml

This will create a file my-container-app.yaml in the directory where you are running your commands.

Your file should look similiar to the following:

configuration:
  activeRevisionsMode: Multiple
  ingress:
    allowInsecure: false
    external: true
    fqdn: my-container-app.salmonocean-b31745af.eastus2.azurecontainerapps.io
    targetPort: 80
    traffic:
    - latestRevision: null
      revisionName: my-container-app--zdazk8c
      weight: 0
    - latestRevision: null
      revisionName: my-container-app--oscswcb
      weight: 100
    transport: Auto
  registries: null
  secrets: null
id: /subscriptions/.../resourceGroups/my-container-apps/providers/Microsoft.Web/containerApps/my-container-app
kind: null
kubeEnvironmentId: /subscriptions/.../resourceGroups/my-container-apps/providers/Microsoft.Web/kubeenvironments/my-environment
latestRevisionFqdn: my-container-app--oscswcb.salmonocean-b31745af.eastus2.azurecontainerapps.io
latestRevisionName: my-container-app--oscswcb
location: East US 2
name: my-container-app
provisioningState: Succeeded
resourceGroup: my-container-apps
systemData:
  createdAt: '2022-03-13T21:40:12.8227504'
  createdBy: <username>
  createdByType: User
  lastModifiedAt: '2022-03-13T22:06:28.6723518'
  lastModifiedBy: <username>
  lastModifiedByType: User
tags: null
template:
  containers:
  - args: null
    command: null
    env: null
    image: mcr.microsoft.com/azuredocs/aci-helloworld:latest
    name: my-container-app
    resources:
      cpu: 0.5
      memory: 1Gi
  dapr: null
  revisionSuffix: null
  scale:
    maxReplicas: 10
    minReplicas: null
    rules: null
type: Microsoft.Web/containerApps

In order to change the traffic from 100% to 0%, modify the in the traffic numbers in the yaml file and save it.

Screenshot of yaml lines

Now you need to run the following command using the yaml file you just edited:

az containerapp update \
  --name my-container-app \
  --resource-group $RESOURCE_GROUP \
  --yaml my-container-app.yaml

Once the CLI returns, once again run the az containerapp revision list command to see the traffic change reflected:

Name                       DateCreated                ProvisionStatus    Traffic    Active
-------------------------  -------------------------  -----------------  ---------  --------
my-container-app--zdazk8c  2022-03-13T21:40:14+00:00  Provisioned        100        True
my-container-app--oscswcb  2022-03-13T21:45:27+00:00  Provisioned        0          True

This option isn’t perfect, since the container app in the yaml still has a template with the image of the second revision we created (the ACI hello world image), so you also need to change the image name to the original image so any new replicas created will use the correct image. This is something I did not do above.

Summary

You can rollback a Container App revision by changing all the traffic to go to the old revision using a command like the following:

az containerapp update --name <container app name> \
  --resource-group <resource group> \
  --traffic-weight <revision 1 name>=100,<revision 2 name>=0

If you want to deactivate a revision, you can use a command like:

az containerapp revision deactivate \
  --name <revision name> \
  --resource-group <resource group> 

If you leave the revision active, you can get a url to navigate to only that revision (even though no traffic will be going to it from the application url) by running a command like:

az containerapp revision show \
  --app <container app name> \
  --name <revision name> \
  --query fqdn

There is also the option of utilizing the az containerapp update --yaml argument and a yaml file, however you’ll need to modify not only the traffic percentages but also the template’s image if it has changed between revisions.