This document explains the basics for Batch job creation: how to create and run a job that is based on a script or container image and use predefined and custom variables. To learn more about creating and running jobs, see Job creation and execution overview.
Before you begin
- If you haven't used Batch before, review Get started with Batch and enable Batch by completing the prerequisites for projects and users.
-
To get the permissions that you need to create a job, ask your administrator to grant you the following IAM roles:
- Batch Job Editor (
roles/batch.jobsEditor) on the project - Service Account User (
roles/iam.serviceAccountUser) on the job's service account, which by default is the default Compute Engine service account
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
- Batch Job Editor (
-
Each time you create a job, make sure the job has a valid resource
configuration.
- Unless the default values are sufficient, you must specify the compute
resources per task (
computeResource) fields required for each task to run, as shown in this document. - If you specify any VM resources for a job (any
instances[].policy,instances[].instanceTemplate, orinstanceFlexibilityPolicyfield), you must make sure that these fields are compatible with each other and the job.
- Unless the default values are sufficient, you must specify the compute
resources per task (
-
Each time you create a job, make sure the job has a valid network configuration.
- If you don't have any specific networking requirements for your workload or project and you haven't modified the default network for your project, no action is required.
- Otherwise, you need to configure networking when you create a job. Learn how to configure networking for a job before you create a basic job, so you can modify the examples below to meet your networking requirements.
-
Each time you create a job, make sure the job has a valid VM
operating system (OS) environment.
- If you don't have any specific VM OS image or boot disk requirements for your workload or project, no action is required.
- Otherwise, you need to prepare a valid VM OS environment option. Before creating a basic job, either allow the default configuration for the VM OS environment or learn how to customize the VM OS environment so you can modify the examples below to meet your requirements.
Create a basic job
For information about all the fields you can specify for a job, see the
reference documentation for the
projects.locations.jobs REST resource.
To summarize, a job consists of an array of one or more tasks
that all run one or more
runnables,
which are the executable script(s) and/or container(s) for your job.
To cover the basics, this section explains how to create an example job with
just one runnable, either a script or a container image:
- If you want to use Batch to write jobs that run a container image, see create a container job.
- Otherwise, if you aren't sure if you want to use container images or if you are unfamiliar with containers, creating a script job is recommended.
The example job for both types of jobs contains a task group with an array of 4 tasks. Each task prints a message and its index to the standard output and Cloud Logging. The definition for this job specifies a parallelism of 2, which indicates that the job should run on 2 VMs to allow 2 tasks to run at a time.
Create a basic container job
You can select or create a container image to provide the code and dependencies for your job to run from any compute environment. For more information, see Working with container images and Running containers on VM instances.
You can create a basic container job using the Google Cloud console, gcloud CLI, Batch API, Go, Java, Node.js, Python, or C++.
Console
To create a basic container job using the Google Cloud console, do the following:
In the Google Cloud console, go to the Job list page.
Click Create. The Create batch job page opens. In the left pane, the Job details page is selected.
Configure the Job details page:
Optional: In the Job name field, customize the job name.
For example, enter
example-basic-job.Configure the Task details section:
In the New runnable window, add at least one script or container for this job to run.
For example, to add one container, do the following:
Select Container image URL (default).
In the Container image URL field, enter the URL for a container image that you want to run for each task in this job.
For example, to use the
busyboxDocker container image, enter the following URL:gcr.io/google-containers/busyboxOptional: To override the container image's
ENTRYPOINTcommand, enter a command in the Entry point field.For example, enter the following:
/bin/shOptional: To override the container image's
CMDcommand, do the following:Select the Override container image's CMD command checkbox. A field appears.
In the field, enter one or more commands, separating each command with a new line.
For example, enter the following commands:
-c echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks.Click Done.
In the Task count field, enter the number of tasks for this job. The value must be a whole number between
1and the tasks per task group limit.For example, enter
4.In the Parallelism field, enter the number of tasks to run concurrently. The number cannot be larger than the total number of tasks and must be a whole number between
1and the parallel tasks per job limit.For example, enter
2.
Configure the Resource specifications page:
In the left pane, click