This example shows how to run a C++ application on Cloud Batch job using C++.
If you are not familiar with the Batch API, we recommend you first read the API overview before starting this guide.
The following steps are included:
- Create a docker image
- Upload it to Artifact registry
- Create the job
- Poll until the job finishes
- Install the gcloud CLI.
- Install docker.
The instructions are here.
- [If it does not already exist] Create the artifact repository
- Build the image locally
- Tag and push the image to the artifact repository
To run this example, replace the [PROJECT ID] placeholder with the id of your
project:
Authorize via gcloud cli
PROJECT_ID=[PROJECT_ID]
LOCATION="us-central1"
REPOSITORY="application-repo"
gcloud auth login
gcloud config set project ${PROJECT_ID}
# Create the repository
gcloud artifacts repositories create ${REPOSITORY} \
--repository-format=docker \
--location=${LOCATION} \
--description="Store the example C++ application" \
--asyncTo verify repo was created
``` gcloud artifacts repositories list ```You should see something like
application-repo DOCKER STANDARD_REPOSITORY Store the example C++ application us-central1 Google-managed key 2024-05-13T20:07:11 2024-05-13T20:07:11 0
cd batch/cpp_application/application
docker build --tag=application-image:latest .
cd batch/cpp_application/application
gcloud builds submit --region=${LOCATION} --tag ${LOCATION}-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY}/application-image:latest
Using docker
To do the same using docker instead of the gcloud CLI:# Tag the image
docker tag application-image:latest ${LOCATION}-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY}/application-image:latest
# Push the image
docker push ${LOCATION}-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY}/application-image:latest
You can do either of the following:
- Use the C++ client libraries to create and poll for the job until completion
- Use the gcloud CLI to create the job
This project uses vcpkg to install its dependencies. Clone vcpkg in your
$HOME:
git clone -C $HOME https://github.com/microsoft/vcpkg.gitInstall the typical development tools, on Ubuntu you would use:
apt update && apt install -y build-essential cmake git ninja-build pkg-config g++ curl tar zip unzipIn this directory compile the dependencies and the code, this can take as long as an hour, depending on the performance of your workstation:
cd cpp-samples/batch/cpp_application
cmake -S . -B .build -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE=$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake
cmake --build .buildRun the example, replace the [PROJECT ID] placeholder with the id of your
project:
.build/driver [PROJECT ID] us-central1 cpp-application-run application.json application-repoThis submits the batch job and then polls until the job is complete.
- Replace the
imageURIfield in application.json
"runnables": [
{
"container": {
"imageUri": "${LOCATION_ID}-docker.pkg.dev/${PROJECT_ID}/{REPOSITORY}/application-image:latest",
}
}
],
- Submit the job
gcloud batch jobs submit cpp-application-cli-run \
--config=application.json \
--location=us-central1
- Check on the job status
gcloud batch jobs describe cpp-application-cli-run --location=us-central1