Build functions into containers

Cloud Run supportsdirectly deploying functions,however, you can also build your function into a container image usingGoogle Cloud's buildpacks and thendeploy this container image to Cloud Run.

Common use cases for building functions into container include:

  • Continuous Integration and Delivery: Developers author and push function codeto a source repository, a CI/CD system automatically builds the function intoa container, runs tests, and automatically deploys it to a staging environment.
  • Infrastructure as Code: Cloud Run resources that are managed usingYAML or Terraform reference a container image URL. The function code writtenby developers need to be built into a container image.

This page details how you can replicate the exact build process ofCloud Run functions using two ways:

  • Usingpack CLI
  • UsingCloud Build as the remote build system

Function entry point

To build functions with buildpacks:

  • Include theFunctions Framework library.

  • Set theGOOGLE_FUNCTION_TARGET environment variable to the nameof the function that you use as the entrypoint. You can do this by includingaproject.toml in the same folder as your source code. Theproject.tomlfile must have the following configuration:

[[build.env]]    name = "GOOGLE_FUNCTION_TARGET"    value =  "ENTRY_POINT"

ReplaceENTRY_POINT with the function method.

For details about how touse environment variables with Cloud Run functions, seeConfigure Cloud Run functions services.

Builders

Cloud Run functions are built on top of base images maintained and published under Google Cloud's buildpacks.

Builders are images that consist of buildpacks andoperating system packages (also known as Stacks). Builders are used to convertyour function's source code into a running container.

You can choose from the list of supported Google Cloud's buildpacksbuilders.

Building withpack

Packis a CLI tool maintained by the CNB project to support the use of buildpacks. Usethepack CLI to locally build your functions into a container image.

Before you begin

  1. InstallDocker Community Edition (CE) on your workstation. Docker is used bypack as an OCI image builder.
  2. InstallPack CLI.
  3. Install theGit source control tool to fetch the sample application from GitHub.

Build a function locally

You use thepack build command and specify the default builder--builder=gcr.io/buildpacks/builder to build your container images locally.

packbuild--builder=gcr.io/buildpacks/builderIMAGE_NAME

ReplaceIMAGE_NAME with the name of your container image.

You can also customize your container image byextending the build and run images.

Build a sample function locally

The following examples demonstrate how to build a sample locally.

  1. Clone the sample repository to your local machine:
    gitclonehttps://github.com/GoogleCloudPlatform/buildpack-samples.git
  2. Change to the directory that contains the application sample code:

    Go

    cd buildpack-samples/sample-functions-framework-go

    Java

    cd buildpack-samples/sample-functions-framework-java-mvn

    Node.js

    cd buildpack-samples/sample-functions-framework-node

    Python

    cd buildpack-samples/sample-functions-framework-python

    Ruby

    cd buildpack-samples/sample-functions-framework-ruby
  3. Usepack to build the sample function:

    Go

    pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-go

    Java

    pack build --builder gcr.io/buildpacks/builder:v1 sample-functions-java-mvn

    Node.js

    pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-node

    Python

    pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-python

    Ruby

    pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-ruby
  4. Run the image usingdocker:

    Go

    docker run -p8080:8080 sample-functions-framework-go

    Java

    docker run -it -ePORT=8080 -p8080:8080 sample-functions-java-mvn

    Node.js

    docker run -it -ePORT=8080 -p8080:8080 sample-functions-framework-node

    Python

    docker run -it -ePORT=8080 -p8080:8080 sample-functions-framework-python

    Ruby

    docker run -it -ePORT=8080 -p8080:8080 sample-functions-framework-ruby
  5. Visit the running function by browsing tolocalhost:8080.

Building with a remote build system

UseCloud Build to build your function into a containerimage, andArtifact Registry as the container repository tostore and deploy each image.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.create permission.Learn how to grant roles.
    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Build and Artifact Registry APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enable permission.Learn how to grant roles.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. Toinitialize the gcloud CLI, run the following command:

    gcloudinit
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.create permission.Learn how to grant roles.
    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Cloud Build and Artifact Registry APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enable permission.Learn how to grant roles.

    Enable the APIs

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. Toinitialize the gcloud CLI, run the following command:

    gcloudinit
  14. If you've already installed Google Cloud CLI previously, make sure you have thelatest available version by runninggcloud components update.

  15. Ensure that your Google Cloud project has access to a container image repository.

    To configure access to aDocker repository in Artifact Registry:

    1. Create a new Docker repository in the same location of your Google Cloud project.
      gcloudartifactsrepositoriescreateREPO_NAME\--repository-format=docker\--location=REGION--description="DESCRIPTION"
      Replace:
      • REPO_NAME with the name that you choose for your Docker repository.
      • REGION with thelocation in or nearest to the location of your Google Cloud project.
      • DESCRIPTION with a description of your choice.

      For example, to create adocker repository inus-west2 with the description "Docker repository", you run:

      gcloudartifactsrepositoriescreatebuildpacks-docker-repo--repository-format=docker\--location=us-west2--description="Docker repository"
    2. Verify that your repository was created:
      gcloudartifactsrepositorieslist

      You should see name that you choose for your Docker repository in the list.

Build a function remotely

You use thegcloud builds submitcommand to build and upload your container image to your repository.

You can choose to specify your container image in the command itself or usea configuration file.

Build with command

To build without a configuration file, specify theimage flag:

gcloudbuildssubmit--packimage=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME

Replace:

  • LOCATION with the region name of your container repository, for example,us-west2
  • PROJECT_ID with the ID of your Google Cloud project.
  • REPO_NAME with the name of your Docker repository.
  • IMAGE_NAME with the name of your container image.

Example:

gcloud builds submit --pack image=us-west2-docker.pkg.dev/my-project-id/my-buildpacks-docker-repo

Build with configuration files

You can use aconfiguration fileto define your image repository configurationdetails to simply the build command. The configuration file uses the YAML fileformat and must include a build step that uses thepack CLI.

  1. Create a YAML file namecloudbuild.yaml that includes the URI of yourcontainer image repository.
options:logging:CLOUD_LOGGING_ONLYpool:{}projectId:PROJECT_IDsteps:-name:gcr.io/k8s-skaffold/packentrypoint:packargs:-build-LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME---builder-gcr.io/buildpacks/builder:latest---network-cloudbuildimages:-LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME

Replace:

  • LOCATION with the region name of your container repository, for example,us-west2.
  • PROJECT_ID with the ID of your Google Cloud project.
  • REPO_NAME with the name of your Docker repository.
  • IMAGE_NAME with the name of your container image.
  1. Build the application.

    If you named your configuration filecloudbuild.yaml, you can runthe following command:

    gcloudbuildssubmit.
    Tip: If you use a unique name for your configuration file, you can use the--config flag.

Example: Build a sample function remotely

The following examples demonstrate how to build a sample remotely, andverify that the container image was pushed to your repository in Artifact Registry.

  1. Clone the sample repository to your local machine:
    gitclonehttps://github.com/GoogleCloudPlatform/buildpack-samples.git
  2. Change to the directory that contains the application sample code:

    Go

    cd buildpack-samples/sample-functions-framework-go

    Java

    cd buildpack-samples/sample-functions-framework-java-mvn

    Node.js

    cd buildpack-samples/sample-functions-framework-node

    Python

    cd buildpack-samples/sample-functions-framework-python

    Ruby

    cd buildpack-samples/sample-functions-framework-ruby
  3. Usegcloud to submit the application source code to Cloud Build:

    Go

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-go

    Java

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-gradle

    Node.js

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-node

    Python

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-python

    Ruby

    gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-functions-framework-ruby

    Replace:

    • LOCATION with the region name of your container repository. Example:us-west2-docker.pkg.dev
    • PROJECT_ID with the ID of your Google Cloud project.
    • REPO_NAME with the name of your Docker repository.

  4. Verify that the sample function was successfully published intoREPO_NAME:
    gcloudartifactsdockerimageslistLOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME

    Replace:

    • LOCATION with the region name of your container repository, for example,us-west2.
    • PROJECT_ID with the ID of your Google Cloud project.
    • REPO_NAME with the name of your Docker repository.

Building a function for automatic base image updates

Function containers can also be built onscratch, allowing them to be used incombination withCloud Run automatic security updates.

Dockerfile

You can use your existing build toolchain to create a function containerimage that is compatible with automatic base image updates. See instructionson how to build acontainer on scratch.

pack CLI

packbuildIMAGE_NAME\--builderLOCATION-docker.pkg.dev/serverless-runtimes/google-24-full/builder/LANGUAGE:latest--run-imageLOCATION-docker.pkg.dev/serverless-runtimes/google-24/scratch/RUNTIME_ID:latest

Replace:

  • LOCATION with the region name of your container repository, for example,us-west2.
  • IMAGE_NAME with the name of your container image.
  • LANGUAGE with the language of your function, for examplenodejs.
  • RUNTIME_ID with the runtime ID, for examplenodejs24.

Cloud Build

options:logging:CLOUD_LOGGING_ONLYpool:{}projectId:PROJECT_IDsteps:-name:gcr.io/k8s-skaffold/packentrypoint:packargs:-build-LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME---builder-LOCATION-docker.pkg.dev/serverless-runtimes/google-24-full/builder/LANGUAGE:latest---run-image-LOCATION-docker.pkg.dev/serverless-runtimes/google-24/scratch/RUNTIME_ID:latest---network-cloudbuildimages:-LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME

Replace:

  • LOCATION with the region name of your container repository, for example,us-west2.
  • PROJECT_ID with the ID of your Google Cloud project.
  • REPO_NAME with the name of your Docker repository.
  • IMAGE_NAME with the name of your container image.
  • LANGUAGE with the language of your function, for examplenodejs.
  • RUNTIME_ID with the runtime ID, for examplenodejs24.

What's next?

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-19 UTC.