Quickstart with curl

This quickstart shows how to use the Google Cloud Container Builder API to package a simple Node.js application as a Docker container. This quickstart uses curl; alternatively, you can use the quickstart with the gcloud command-line tool.

Before you begin

Take the following steps to enable the Container Builder API:

  1. Enable the Container Builder API in the Google Cloud Platform Console.
  2. Create or select a project.
  3. Click Continue to enable the API and any related services. This step may take a few minutes.
  4. Enable billing for your project.

    Enable billing

For this tutorial, you can use Cloud Shell or any other environment where the Google Cloud SDK is installed.

Log in to Google Cloud

If you are not using Cloud Shell, make sure to authorize access to Cloud resources using the following at a command prompt before you perform the tutorial steps:

  1. Authorize access to Cloud resources (if you are using Cloud Shell you can skip this step):

    gcloud auth login
    
  2. Set the active project for the gcloud command-line tool:

    gcloud config set project PROJECT_ID
    

    To see the IDs for all of your projects:

    gcloud projects list
    

Create a build request

The build request is a JSON document containing instructions that Container Builder needs to create the container image. Build requests are modeled using the Build resource in the Container Builder API. See Build Requests for more information.

  1. Create the build request by pasting the following code into a text file:

    {
      "source": {
        "storageSource": {
          "bucket": "container-builder-examples",
          "object": "node-docker-example.tar.gz"
        }
      },
      "steps": [
        {
          "name": "gcr.io/cloud-builders/docker",
          "args": [
            "build", "-t", "gcr.io/$PROJECT_ID/cb-demo-img", "."
          ]
        }
      ],
      "images": [
        "gcr.io/$PROJECT_ID/cb-demo-img"
      ]
    }
    
  2. Save the file as request.json.

  3. Note the length (number of bytes) of the above file. You'll use it in the build request you send in the next section.

Send the build request

Send the build request to Container Builder using curl (substitute [PROJECT_ID] with your actual project ID, and [NUM_OF_BYTES_IN_REQUEST] to the number of bytes in your request.json file):

curl -X POST -T request.json -H "Content-Type: application/json" \
  -H "Content-length: [NUM_OF_BYTES_IN_REQUEST]" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  https://cloudbuild.googleapis.com/v1/projects/[PROJECT_ID]/builds

The URL here is the endpoint for the projects.builds.create method in the API. You pass the JSON build request that you assembled previously in the tutorial using the -T request.json argument.

The API returns JSON-formatted data similar to the following:

{
  "name": "operations/build/container-builder/05f9e215-88ec-48ca-ba9c-8223471099a2",
  "metadata": {
    "@type": "type.googleapis.com/google.devtools.cloudbuild.v1.BuildOperationMetadata",
    "build": {
      "id": "05f9e215-88ec-48ca-ba9c-8223471099a2",
      "status": "QUEUED",
      "source": {
        "storageSource": {
          "bucket": "container-builder-examples",
          "object": "node-docker-example.tar.gz"
        }
      },
      "createTime": "2016-01-13T23:23:49.110614108Z",
      "userId": "256630332032",
      "steps": [
        {
          "name": "gcr.io/cloud-builders/docker",
          "args": [
            "build", "-t", "gcr.io/container-builder/cb-demo-img", "."
          ]
        }
      ],
      "timeout": "600.000s",
      "images": [
        "gcr.io/container-builder/cb-demo-img"
      ],
      "projectId": "container-builder",
      "projectNum": "167295375610"
    }
  }
}

This response is modeled using the Operation resource and its metadata field is modeled using the Build resource. The QUEUED status indicates that the build is awaiting execution.

Note: The gcloud auth print-access-token command gets an authorization token that allows you to access the Container Builder API. Use of this command is not recommended for real-world API interactions. For information about how to set perform authorization on Google Cloud Platform, see the Google Cloud Platform Auth Guide.

Check the build status

Check the status of your build (substitute [PROJECT_ID] with your actual project ID):

curl -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  https://cloudbuild.googleapis.com/v1/projects/[PROJECT_ID]/builds/[BUILD_ID]

where [BUILD_ID] is the value of the build id field returned from the build request.

The HTTP address here is the endpoint for the projects.builds.get method in the API. The response to this request is modeled using the Build resource.

Find the container image

When the status of your build is SUCCESS, you can find your container image in Google Container Registry.

List builds

To list all builds for your project (substitute [PROJECT_ID] with your actual project ID):

curl -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  https://cloudbuild.googleapis.com/v1/projects/[PROJECT_ID]/builds

The HTTP address here is the endpoint for the projects.builds.list method in the API. The request returns an array of Build resources.

View build logs

To view build logs, open Cloud Logging in the Google Cloud Platform Console and select Container Builder > Build ID.

For more information, see Google Cloud Logging.

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. Go to the Cloud Platform Console.
  2. Click the trash can icon next to the project you want to shut down. This schedules the project for deletion.

What's next

Here are some topics to help you to continue learning about Google Container Builder.

Send feedback about...

Cloud Container Builder Documentation