Create jobs

This page describes how to create and update Cloud Run jobs from an existing container image. Unlike a Cloud Run service, which listens for and serves requests, a Cloud Run job only runs its tasks and exits when finished. A job does not listen for or serve requests.

After you create or update a job, you can do the following:

You can structure a job as a single task or as multiple, independent tasks (up to 10,000 tasks) that can be executed in parallel. Each task runs one container instance and can be configured to retry in case of failure. Each task is aware of its index, which is stored in the CLOUD_RUN_TASK_INDEX environment variable. The overall count of tasks is stored in the CLOUD_RUN_TASK_COUNT environment variable. If you are processing data in parallel, your code is responsible for determining which task handles which subset of the data.

You can set timeouts on tasks and specify the number of retries in case of task failure. If any task exceeds its maximum number of retries, that task is marked as failed. If any tasks failed, the job execution is marked as failed after Cloud Run has tried all of the tasks.

By default, each task runs for a maximum of 10 minutes. You can modify the default value by changing the task timeout setting, up to 168 hours (7 days). For tasks using GPUs, the maximum available timeout is 1 hour.

There is no explicit timeout for a job execution: after all tasks are complete, the job execution is complete.

Jobs use the second generation execution environment.

Required roles

To get the permissions that you need to create Cloud Run jobs, ask your administrator to grant you the following IAM roles:

For a list of IAM roles and permissions that are associated with Cloud Run, see Cloud Run IAM roles and Cloud Run IAM permissions. If your Cloud Run job interfaces with Google Cloud APIs, such as Cloud Client Libraries, see the service identity configuration guide. For more information about granting roles, see deployment permissions and manage access.

Supported container registries and images

You can directly use container images stored in Artifact Registry, or public images from Docker Hub or GitHub Container Registry. Google recommends the use of Artifact Registry. Public images from GitHub Container Registry and Docker Hub images are cached for up to one hour.

You can use container images from other public or private registries (like JFrog Artifactory or Nexus), or private images from GitHub Container Registry, by setting up an Artifact Registry remote repository.

You should only consider Docker Hub for deploying popular container images such as Docker Official Images or Docker Sponsored OSS images. For higher availability, Google recommends deploying these Docker Hub or GitHub Container Registry images using an Artifact Registry remote repository.

Cloud Run does not support container image layers larger than 9.9 GB when deploying from Docker Hub or an Artifact Registry remote repository with an external registry.

Create a new job

You can create a new job using the Google Cloud console, the gcloud CLI, YAML, Terraform, client libraries, or the REST API:

Console

To create a new job, follow these steps:

  1. In the Google Cloud console, go to the Cloud Run jobs page:

    Go to Cloud Run jobs

  2. Click Deploy container to display the Create job form.

  3. In the form, specify the container image containing the job code or select from a list of containers previously deployed.

  4. The job name is automatically generated from the container image. You can edit the job name as needed on the form. After you submit the form, you can't edit the name of the job.

  5. In the Region field, select the region for your job. The region selector highlights regions with the lowest carbon impact.

  6. Specify the number of tasks to run in the job. All of the tasks must succeed for the job to succeed. By default, the tasks execute in parallel.

  7. Click Containers, Networking, Security to set additional job properties.

  8. In the Edit container section, optionally configure the following settings in the appropriate tabs:

  9. In the Task capacity section, specify the following:

    1. In the Task timeout field, specify the maximum amount of time in seconds that the task can run, up to 168 hours (7 days). For tasks using GPUs, the maximum available timeout is 1 hour. Each task must complete within this time. The default is 10 minutes.

    2. In the Number of retries per failed task field, specify the number of retries in case of task failures. The default is 3 retries.

  10. Under Parallelism, select Run as many tasks concurrently as possible if you need to set a lower limit due to scaling constraints on resources your job accesses, or select Limit the maximum number of concurrent tasks and specify the number of concurrent tasks in the Custom parallelism limit field.

  11. When you are finished configuring your job, click Create to create the job in Cloud Run.

  12. To execute the job, see execute jobs or execute jobs on a schedule.

gcloud

To use the command line, you need to have already set up the gcloud CLI.

To create a new job, follow these steps:

  1. Run the command:

    gcloud run jobs create JOB --image IMAGE_URL OPTIONS
    Alternatively, use the deploy command:
    gcloud run jobs deploy JOB --image IMAGE_URL OPTIONS

    Replace the following:

    • JOB: the name of your Cloud Run job.
    • IMAGE_URL: a reference to the container image—for example, us-docker.pkg.dev/cloudrun/container/job:latest.
    • OPTIONS (Optional): any of the following options:

      Option Description
      --tasks Accepts integers greater or equal to 1. Defaults to 1; maximum is 10,000. Each task is provided the environment variables CLOUD_RUN_TASK_INDEX with a value between 0 and the number of tasks minus 1, along with CLOUD_RUN_TASK_COUNT, which is the number of tasks
      --max-retries The number of times a failed task is retried. Once any task fails beyond this limit, the entire job is marked as failed. For example, if set to 1, a failed task will be retried once, for a total of two attempts. The default is 3. Accepts integers from 0 to 10.
      --task-timeout Accepts a duration like "2s". Defaults to 10 minutes; maximum is 168 hours (7 days). For tasks using GPUs, the maximum available timeout is 1 hour.
      --parallelism The maximum number of tasks that can execute in parallel. By default, tasks will be started as quickly as possible in parallel. Refer to Parallelism for the range of values.
      --execute-now If set, immediately after the job is created, a job execution is started. Equivalent to calling gcloud run jobs create followed by gcloud run jobs execute.

      In addition to the preceding options, you also specify more configuration such as environment variables or memory limits.

      For a full list of available options when creating a job, refer to the gcloud run jobs create command line documentation.

  2. Wait for the job creation to finish. You'll see a success message upon a successful completion.

  3. To execute the job, see execute jobs or execute jobs on a schedule.

YAML

You can store your job specification in a YAML file and then deploy it using the gcloud CLI.

  1. Create a new job.yaml file with this content:

    apiVersion: run.googleapis.com/v1
    kind: Job
    metadata:
      name: JOB
    spec:
      template:
        spec:
          template:
            spec:
              containers:
              - image: IMAGE_URL

    Replace the following:

    • JOB: the name of your Cloud Run job.
    • IMAGE_URL: a reference to the container image—for example, us-docker.pkg.dev/cloudrun/container/job:latest.

    You can also specify more configuration such as environment variables or memory limits.

  2. Deploy the new job by running the following command:

    gcloud run jobs replace job.yaml

Terraform

To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.

Add the following to a google_cloud_run_v2_job resource in your Terraform configuration:
resource "google_cloud_run_v2_job" "default" {
  name     = "cloud-run-job"
  location = "us-central1"

  deletion_protection = false # set to "true" in production

  template {
    template {
      containers {
        image = "us-docker.pkg.dev/cloudrun/container/job:latest"
      }
    }
  }
}

Client libraries

To create a job from code:

REST API

To create a job, send a POST HTTP request to request to the Cloud Run Admin API jobs endpoint.

For example, using curl:

curl -H "Content-Type: application/json" \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -X POST \
  -d '{template: {template: {containers: [{image: "IMAGE_URL"}]}}}' \
  https://run.googleapis.com/v2/projects/PROJECT_ID/locations/REGION/jobs?jobId=JOB

Replace the following:

  • ACCESS_TOKEN: a valid access token for an account that has the IAM permissions to create jobs. For example, if you are logged into gcloud, you can retrieve an access token using gcloud auth print-access-token. From within a Cloud Run container instance, you can retrieve an access token using the container instance metadata server.
  • IMAGE_URL: a reference to the container image—for example, us-docker.pkg.dev/cloudrun/container/job:latest.
  • PROJECT_ID: the Google Cloud project ID.
  • REGION: the Google Cloud region of the job.
  • JOB: the name of the job you want to create.

Cloud Run locations

Cloud Run is regional, which means the infrastructure that runs your Cloud Run services is located in a specific region and is managed by Google to be redundantly available across all the zones within that region.

Meeting your latency, availability, or durability requirements are primary factors for selecting the region where your Cloud Run services are run. You can generally select the region nearest to your users but you should consider the location of the other Google Cloud products that are used by your Cloud Run service. Using Google Cloud products together across multiple locations can affect your service's latency as well as cost.

Cloud Run is available in the following regions:

Subject to Tier 1 pricing

  • asia-east1 (Taiwan)
  • asia-northeast1 (Tokyo)
  • asia-northeast2 (Osaka)
  • asia-south1 (Mumbai, India)
  • asia-southeast3 (Bangkok)
  • europe-north1 (Finland) leaf icon Low CO2
  • europe-north2 (Stockholm) leaf icon Low CO2
  • europe-southwest1 (Madrid) leaf icon Low CO2
  • europe-west1 (Belgium) leaf icon Low CO2
  • europe-west4 (Netherlands) leaf icon Low CO2
  • europe-west8 (Milan) leaf icon Low CO2
  • europe-west9 (Paris) leaf icon Low CO2
  • me-west1 (Tel Aviv)
  • northamerica-south1 (Mexico)
  • us-central1 (Iowa) leaf icon Low CO2
  • us-east1 (South Carolina)
  • us-east4 (Northern Virginia)
  • us-east5 (Columbus)
  • us-south1 (Dallas) leaf icon Low CO2
  • us-west1 (Oregon) leaf icon Low CO2

Subject to Tier 2 pricing

  • africa-south1 (Johannesburg)
  • asia-east2 (Hong Kong)
  • asia-northeast3 (Seoul, South Korea)
  • asia-southeast1 (Singapore)
  • asia-southeast2 (Jakarta)
  • asia-south2 (Delhi, India)
  • australia-southeast1 (Sydney)
  • australia-southeast2 (Melbourne)
  • europe-central2 (Warsaw, Poland) leaf icon Low CO2
  • europe-west10 (Berlin)
  • europe-west12 (Turin) leaf icon Low CO2
  • europe-west2 (London, UK) leaf icon Low CO2
  • europe-west3 (Frankfurt, Germany)
  • europe-west6 (Zurich, Switzerland) leaf icon Low CO2
  • me-central1 (Doha)
  • me-central2 (Dammam)
  • northamerica-northeast1 (Montreal) leaf icon Low CO2
  • northamerica-northeast2 (Toronto) leaf icon Low CO2
  • southamerica-east1 (Sao Paulo, Brazil) leaf icon Low CO2
  • southamerica-west1 (Santiago, Chile) leaf icon Low CO2
  • us-west2 (Los Angeles) leaf icon Low CO2
  • us-west3 (Salt Lake City)
  • us-west4 (Las Vegas)

If you already created a Cloud Run service, you can view the region in the Cloud Run dashboard in the Google Cloud console.

When you create a new job, the Cloud Run service agent needs to be able to access the container, which is the case by default.

Update an existing job

Changing any configuration settings requires you to update the job, even if there is no change to the container image. Note that for any unchanged settings, the previous settings continue to be used.

You can update an existing job using the Google Cloud console, the gcloud CLI, YAML, Terraform, client libraries, or the REST API:

Console

To update an existing job, follow these steps:

  1. In the Google Cloud console, go to the Cloud Run jobs page:

    Go to Cloud Run jobs

  2. Click the job to display the Job details page.

  3. Click View and edit job configuration.

  4. Optional: Change the number of tasks that are in the job if needed.

  5. Optional: Click Containers, Networking, Security to update any additional job properties:

  6. In the Task capacity section, specify the following:

    1. In the Task timeout field, specify the maximum amount of time in seconds that the task can run, up to 168 hours (7 days). For tasks using GPUs, the maximum available timeout is 1 hour. Each task must complete within this time. The default is 10 minutes.

    2. In the Number of retries per failed task field, specify the number of retries in case of task failures. The default is 3 retries.

  7. Under Parallelism, select Run as many tasks concurrently as possible if you need to set a lower limit due to scaling constraints on resources your job accesses, or select Limit the maximum number of concurrent tasks and specify the number of concurrent tasks in the Custom parallelism limit field.

  8. When you are finished configuring your job, click Update to modify the job in Cloud Run and wait for the job creation to finish.

  9. To execute the job, see execute jobs or execute jobs on a schedule.

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. Run the following command:

    gcloud run jobs update JOB

    Replace the following:

    • JOB: the name of your Cloud Run job.
    • OPTIONS (Optional): with the following options:

      Option Description
      --tasks Accepts integers equal or greater than 1. Defaults to 1; maximum is 10,000. Each task is provided the environment variables CLOUD_RUN_TASK_INDEX with a value between 0 and the number of tasks minus 1, along with CLOUD_RUN_TASK_COUNT, which is the number of tasks
      --max-retries The number of times a failed task is retried. Once any task fails beyond this limit, the entire job is marked as failed. For example, if set to 1, a failed task will be retried once, for a total of two attempts. The default is 3. Accepts integers from 0 to 10.
      --task-timeout Accepts a duration like "2s". Defaults to 10 minutes; maximum is 168 hours (7 days).
      --parallelism The maximum number of tasks that can execute in parallel. By default, tasks will be started as quickly as possible, in parallel. Refer to Parallelism for the range of values.

    In addition to the previous options, you can set other optional configuration settings:

    For a full list of available options when creating a job, refer to the gcloud run jobs create command line documentation.

  3. Wait for the job update to finish. Upon successful completion, a success message is displayed, similar to the following:

    Job [JOB] has been successfully updated.
    View details about this job by running `gcloud run jobs describe JOB`.
    See logs for this execution at: https://console.cloud.google.com/logs/viewer?project=PROJECT_ID&resource=cloud_run_revision/service_name/JOB
  4. To execute the job, see execute jobs or execute jobs on a schedule.

YAML

  1. If you need to download or view the configuration of an existing job, run the following command to save results to a YAML file:

    gcloud run jobs describe JOB --format export > job.yaml
  2. Modify any spec.template child attributes as needed, then redeploy by running the following command:

    gcloud run jobs replace job.yaml

    The gcloud run jobs replace command defaults to using job.yaml file if present.

  3. To execute the job, see execute jobs or execute jobs on a schedule.

Terraform

Make changes to your job configuration in your main.tf file using the terraform apply command. Detailed Terraform instructions are available for:

For more information, refer to the terraform apply command line options.

Client libraries

To update an existing job from code:

REST API

To update a job, send a PATCH HTTP request to request to the Cloud Run Admin API jobs endpoint.

For example, using curl:

curl -H "Content-Type: application/json" \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -X PATCH \
  -d '{template: {template: {containers: [{image: "IMAGE_URL"}]}}}' \
  https://run.googleapis.com/v2/projects/PROJECT_ID/locations/REGION/jobs/JOB

Replace the following:

  • ACCESS_TOKEN: a valid access token for an account that has the IAM permissions to update jobs. For example, if you are logged into gcloud, you can retrieve an access token using gcloud auth print-access-token. From within a Cloud Run container instance, you can retrieve an access token using the container instance metadata server.
  • IMAGE_URL: a reference to the container image—for example, us-docker.pkg.dev/cloudrun/container/job:latest.
  • PROJECT_ID: the Google Cloud project ID.
  • REGION: the Google Cloud region of the job.
  • JOB: the name of your job you want to update.

Sample code

For code samples showing jobs, see the language-specific quickstarts.

Deploy multiple containers to a job (sidecars)

In a Cloud Run job deployment with multiple containers (sidecars), there is one main job container that encapsulates the job configuration and one or more sidecar containers.

You can deploy up to 10 containers per instance including the main job container. All containers within an instance share the same network namespace and can share files using an in-memory shared volume.

Use cases

Sidecars are commonly used for the following use cases:

  • Fetching custom metrics from Cloud Run jobs and sending them to a specified backend of your choice using collector agents, such as Prometheus or Opentelemetry.
  • Letting applications without Hashicorp Vault logic built in to use static and dynamic secrets sourced from Vault using Vault sidecar.

Deploy a job with sidecar containers

You can deploy multiple sidecars to a Cloud Run job using the Google Cloud console, the gcloud CLI, YAML, or Terraform:

Console

  1. In the Google Cloud console, go to the Cloud Run jobs page:

    Go to Cloud Run jobs

  2. To deploy to an existing job, click Jobs. Locate the job in the jobs list, and click to open it. Then click View and edit configuration to display the edit job form.

  3. For a new job, click Deploy container. Supply the container image URL and the job name.

  4. Click Containers, Networking, Security

  5. In the Edit container card, configure the main job container as needed.

  6. Click Add container and configure a sidecar container you want to add alongside the main job container. If the sidecar depends on another container in the service, indicate this in the Container start-up order drop-down menu. Repeat this step for each sidecar container you are deploying.

  7. Click Create for a new service or Update for an existing job, then wait for the deployment to finish.

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. To deploy multiple containers to a job, run the following command:

    gcloud run jobs create JOB \
     --container JOB_CONTAINER_NAME \
     --image='IMAGE_URL' \
     --container SIDECAR_CONTAINER_NAME \
     --image='SIDECAR_IMAGE'

    Replace the following:

    • JOB: the name of your Cloud Run job.
    • JOB_CONTAINER_NAME: a name for the main job container.
    • IMAGE_URL: a reference to the container image—for example, us-docker.pkg.dev/cloudrun/container/job:latest.
    • SIDECAR_CONTAINER_NAME: a name for the sidecar container—for example sidecar.
    • SIDECAR_IMAGE: a reference to the sidecar container image.

    If you want to configure each container in the deploy command, supply each container's configuration after the container parameters, for example:

    gcloud run jobs create JOB \
      --container CONTAINER_1_NAME \
      --image='IMAGE_URL' \
      --set-env-vars=KEY=VALUE \
      --container SIDECAR_CONTAINER_NAME \
      --image='SIDECAR_IMAGE' \
      --set-env-vars=KEY_N=VALUE_N
  3. Wait for the jobs deployment to finish. Upon successful completion, a success message is displayed.

YAML

  1. If you are creating a new job, skip this step. If you are updating an existing job, download its YAML configuration:

    gcloud run jobs describe JOB --format export > job.yaml
  2. Update the containers: attributes:

    apiVersion: run.googleapis.com/v1
    kind: Job
    metadata:
      name: JOB
    spec:
      template:
        spec:
          containers:
          - image: IMAGE_URL
          - image: SIDECAR_IMAGE
    

    Replace the following:

    • JOB: the name of your Cloud Run job.
    • IMAGE_URL: a reference to the container image—for example, us-docker.pkg.dev/cloudrun/container/job:latest.
    • SIDECAR_IMAGE: a reference to the sidecar container image.
  3. Create or update the job using the following command:

    gcloud run jobs replace job.yaml

    The gcloud run jobs replace command defaults to using job.yaml file if present.

Terraform

To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.

Add the following to a google_cloud_run_v2_job resource in your Terraform configuration:
resource "google_cloud_run_v2_job" "default" {
  name     = "JOB"
  location = "europe-west1"

  template {
    template {
      containers {
        name = "CONTAINER_NAME"
        image = "IMAGE_URL"
      }
      containers {
        name = "SIDECAR_CONTAINER_NAME"
        image = "SIDECAR_IMAGE_URL"
      }
    }
  }
}

Replace the following:

  • JOB: the name of your Cloud Run job.
  • CONTAINER_NAME: the name of the container.
  • IMAGE_URL: a reference to the container image—for example, us-docker.pkg.dev/cloudrun/container/job:latest.
  • SIDECAR_CONTAINER_NAME: the name of the sidecar container.
  • SIDECAR_IMAGE_URL: a reference to the sidecar container image.

Features available to deployments with sidecars

You can specify the container start up order within a deployment with multiple containers, if you have dependencies that require some containers to start up before other containers in the deployment.

If you have containers that depend on other containers, you must use startup healthchecks in your deployment. If you use healthchecks, Cloud Run follows the container startup order and inspects the health of each container, making sure each passes successfully before Cloud Run starts up the next container in the order. If you don't use healthchecks, healthy containers will start up even if the containers they depend on are not running.

Multiple containers within a single instance can access a shared in-memory volume, which is accessible to each container using mount points that you create.

Limitations

If your job connects to Cloud SQL instances using Cloud Run's built-in feature, refer to the known issue on Built-in Cloud SQL connections.

What's next

After you create or update a job, you can do the following: