Quickstart: Create a job in Azure Container Apps

In this quickstart, you create an Azure Container Apps job. In Container Apps, jobs are used to start containerized tasks that run for a finite duration and then exit. Jobs are best suited for tasks such as data processing, machine learning, resource cleanup, or any scenario that requires on-demand processing.

You can trigger a job manually, schedule its run, or trigger its run based on events. This quickstart shows you how to create a manual or scheduled job. To find out how to create an event-driven job, see Deploy an event-driven job with Azure Container Apps.

Prerequisites

Setup

  1. 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.
    
  2. Ensure you're running the latest version of the CLI via the upgrade command.

    az upgrade
    
  3. Install the latest version of the Azure Container Apps CLI extension.

    az extension add --name containerapp --upgrade
    
  4. Register the Microsoft.App, Microsoft.OperationalInsights, and Microsoft.Storage namespaces if you haven't already registered them in your Azure subscription.

    az provider register --namespace Microsoft.App
    az provider register --namespace Microsoft.OperationalInsights
    az provider register --namespace Microsoft.Storage
    
  5. Now that your Azure CLI setup is complete, you can define the environment variables that are used throughout this article.

    $RESOURCE_GROUP="jobs-quickstart"
    $LOCATION="chinanorth3"
    $ENVIRONMENT="env-jobs-quickstart"
    $JOB_NAME="my-job"
    

Create a Container Apps environment

The Azure Container Apps environment acts as a secure boundary around container apps and jobs so they can share the same network and communicate with each other.

  1. Create a resource group using the following command.

    az group create --name $RESOURCE_GROUP  --location $LOCATION
    
  2. Create the Container Apps environment using the following command.

    az containerapp env create --name $ENVIRONMENT --resource-group $RESOURCE_GROUP --location $LOCATION
    

Create and run a manual job

To use manual jobs, you first create a job with a trigger type of Manual and then start its run. You can start multiple runs of the same job, and multiple job executions can run concurrently.

  1. Create a job in the Container Apps environment by using the following command.

    az containerapp job create `
        --name $JOB_NAME --resource-group $RESOURCE_GROUP  --environment $ENVIRONMENT `
        --trigger-type "Manual" `
        --replica-timeout 1800   `
        --image "mcr.microsoft.com/k8se/quickstart-jobs:latest" `
        --cpu "0.25" --memory "0.5Gi"
    

    Manual jobs don't execute automatically. You must start an execution of the job.

  2. Start an execution of the job using the following command.

    az containerapp job start --name $JOB_NAME --resource-group $RESOURCE_GROUP
    

    The command returns detailed information about the job run, including its name.

Create and run a scheduled job

To use scheduled jobs, you create a job with a trigger type of Schedule and a cron expression that defines the schedule.

Use the following command to create a Container Apps job that starts every minute.

az containerapp job create `
--name $JOB_NAME --resource-group $RESOURCE_GROUP  --environment $ENVIRONMENT `
    --trigger-type "Schedule" `
    --replica-timeout 1800  `
    --image "mcr.microsoft.com/k8se/quickstart-jobs:latest" `
    --cpu "0.25" --memory "0.5Gi" `
    --cron-expression "*/1 * * * *"

Job executions start automatically based on the schedule.

Container Apps jobs use cron expressions to define schedules. It supports the standard cron expression format with five fields for minute, hour, day of month, month, and day of week.

List recent job run history

Container Apps jobs maintain a history of recent runs. You can list the runs of a job.

az containerapp job execution list `
    --name $JOB_NAME `
    --resource-group $RESOURCE_GROUP `
    --output table `
    --query '[].{Status: properties.status, Name: name, StartTime: properties.startTime}'

Jobs appear in the list as they run.

Status     Name            StartTime
--------- --------------  -------------------------
Succeeded  my-job-jvsgub6  2023-05-08T21:21:45+00:00

Query job execution logs

Job executions output logs to the logging provider that you configured for the Container Apps environment. By default, logs are stored in Azure Log Analytics.

  1. Save the Log Analytics workspace ID for the Container Apps environment to a variable.

    $LOG_ANALYTICS_WORKSPACE_ID = az containerapp env show --name $ENVIRONMENT --resource-group $RESOURCE_GROUP --query "properties.appLogsConfiguration.logAnalyticsConfiguration.customerId" --output tsv
    
  2. Save the name of the most recent job execution to a variable.

    $JOB_EXECUTION_NAME=az containerapp job execution list --name $JOB_NAME --resource-group $RESOURCE_GROUP --query "[0].name" --output tsv
    

Clean up resources

If you're not going to continue to use this job, run the following command to delete the resource group and all the resources from this quickstart.

Caution

The following command deletes the specified resource group and all resources contained within it. If resources outside the scope of this quickstart exist in the specified resource group, they're 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 step