Quickstart with curl

This quickstart explains how to use the Container Builder API with curl to package a simple Node.js application as a Docker image and push it to Container Registry.

You can also complete Quickstart with gcloud command-line tool. If you already have a Dockerfile to build, see Quickstart with a Dockerfile.

Before you begin

  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. In the Cloud Platform Console, go to the Projects page and select or create a new project.

    Go to the Projects page

  3. Enable billing for your project.

    Enable billing

  4. Enable the Container Builder API.

    Enable the API

  5. Install and initialize the Cloud SDK.

Log in to Google Cloud

  1. If you are not using Cloud Shell, authorize gcloud to access your project:

    gcloud auth login
    
  2. Configure your project for gcloud, where [PROJECT-ID] is your Cloud Platform project ID:

    gcloud config set project [PROJECT_ID]
    

    If you don't know your project ID, run the following command:

    gcloud projects list
    

Create a build request

Build requests are JSON or YAML documents which instruct Container Builder to perform tasks, and to package, build, and push Docker images, based on your specifications.

Build requests are composed of build steps, which are Docker containers that execute code on your behalf. Calling build steps is analagous to calling scripts or shell commands.

The Build API resource describes the build request model.

To create the build request, copy-and-paste the JSON below into a file named request.json:

    {
      "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"
      ]
    }

In this build request, Container Builder calls the docker build step with the arguments build -t gcr.io/$PROJECT_ID/cb-demo-img ..

The full name of the image to be built is gcr.io/$PROJECT_ID/cb-demo-img. Images pushed to Container Registry use the registry name format.

The code source for the build is in a compressed tar archive, node-docker-example.tar.gz. The file is stored in a Cloud Storage bucket named container-builder-examples.

Submit the build request

To submit the build request, run the following command:

curl -X POST -T request.json -H "Authorization: Bearer $(gcloud config config-helper --format='value(credential.access_token)')" \
  https://cloudbuild.googleapis.com/v1/projects/[PROJECT_ID]/builds

where [PROJECT-ID] is your Cloud Platform project ID.

In this command, curl sends request.json in a POST call to the builds endpoint for the projects.builds.create API method.

The command displays details about your build in your shell or terminal window. The output is a JSON response, and appears similar to the following:

{
  "name": "operations/build/$PROJECT-ID/NmZhZW...",
  "metadata": {
    "@type": "type.googleapis.com/google.devtools.cloudbuild.v1.BuildOperationMetadata",
    "build": {
      "id": $BUILD-ID,
      "status": "QUEUED",
      "source": {
        "storageSource": {
          "bucket": "container-builder-examples",
          "object": "node-docker-example.tar.gz"
        }
      },
      "createTime": "2017-05-12T18:58:07.341526Z",
      "steps": [
        {
          "name": "gcr.io/cloud-builders/docker",
          "args": [
            "build",
            "-t",
            "gcr.io/$PROJECT-ID/cb-demo-img",
            "."
          ]
        }
      ],
      "timeout": "600s",
      "images": [
        "gcr.io/$PROJECT-ID/cb-demo-img"
      ],
      "projectId": $PROJECT-ID,
      "logsBucket": "gs://...",
      "sourceProvenance": {
        "resolvedStorageSource": {
          "bucket": "container-builder-examples",
          "object": "node-docker-example.tar.gz",
          "generation": "..."
        }
      },
      "logUrl": "https://console.cloud.google.com/gcr/builds/...?project=$PROJECT-ID"
    }
  }
}

The JSON response is modeled using the Operation resource in the Container Builder API. The metadata field is modeled using the Build resource. The QUEUED status indicates that the build is awaiting execution.

Check the build status and view the build logs

The build might take several minutes to complete.

To check the status of your build and view its logs, visit the URL displayed in the logUrl field of the JSON response. The URL opens Container Registry's build history menu in your Cloud Platform project.

Locate your Docker image

When you build a Docker image using Container Builder, your built image is stored in Container Registry, a private repository for Docker images.

To locate your built image run the following command:

gcloud container images list

list displays all images stored in Container Registry.

You can also:

Clean up

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

Delete your Docker image

To delete your Docker image, run the following command:

gcloud container images delete [HOSTNAME]/[YOUR-PROJECT-ID]/[IMAGE]

where [HOSTNAME]/[YOUR-PROJECT-ID]/[IMAGE] is your image's registry name.

If you don't know your image's registry name, run the following command:

gcloud container images list

Delete your project

  1. Go to the Cloud Platform Console.
  2. Click the check box beside your project, then click Delete Project. This schedules the project for deletion.

What's next

Send feedback about...

Cloud Container Builder