Build functions into containers Stay organized with collections Save and categorize content based on your preferences.
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:
- Using
packCLI - UsingCloud Build as the remote build system
Function entry point
To build functions with buildpacks:
Include theFunctions Framework library.
Set the
GOOGLE_FUNCTION_TARGETenvironment variable to the nameof the function that you use as the entrypoint. You can do this by includingaproject.tomlin 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
- InstallDocker Community Edition (CE) on your workstation. Docker is used by
packas an OCI image builder. - InstallPack CLI.
- 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_NAMEReplaceIMAGE_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.
- Clone the sample repository to your local machine:
gitclonehttps://github.com/GoogleCloudPlatform/buildpack-samples.git
- 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
- Use
packto 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
- Run the image using
docker: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
- 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
- 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.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
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.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.createpermission.Learn how to grant roles.
Verify that billing is enabled for your Google Cloud project.
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.enablepermission.Learn how to grant roles.Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Toinitialize the gcloud CLI, run the following command:
gcloudinit
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
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.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.createpermission.Learn how to grant roles.
Verify that billing is enabled for your Google Cloud project.
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.enablepermission.Learn how to grant roles.Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Toinitialize the gcloud CLI, run the following command:
gcloudinit
- Ensure that your Google Cloud project has access to a container image repository.
To configure access to aDocker repository in Artifact Registry:
- Create a new Docker repository in the same location of your Google Cloud project.
Replace:gcloudartifactsrepositoriescreateREPO_NAME\--repository-format=docker\--location=REGION--description="DESCRIPTION"
REPO_NAMEwith the name that you choose for your Docker repository.REGIONwith thelocation in or nearest to the location of your Google Cloud project.DESCRIPTIONwith a description of your choice.
For example, to create a
dockerrepository inus-west2with the description "Docker repository", you run:gcloudartifactsrepositoriescreatebuildpacks-docker-repo--repository-format=docker\--location=us-west2--description="Docker repository"
- Verify that your repository was created:
gcloudartifactsrepositorieslist
You should see name that you choose for your Docker repository in the list.
- Create a new Docker repository in the same location of your Google Cloud project.
If you've already installed Google Cloud CLI previously, make sure you have thelatest available version by runninggcloud components update.
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:
LOCATIONwith the region name of your container repository, for example,us-west2PROJECT_IDwith the ID of your Google Cloud project.REPO_NAMEwith the name of your Docker repository.IMAGE_NAMEwith 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.
- Create a YAML file name
cloudbuild.yamlthat 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:
LOCATIONwith the region name of your container repository, for example,us-west2.PROJECT_IDwith the ID of your Google Cloud project.REPO_NAMEwith the name of your Docker repository.IMAGE_NAMEwith the name of your container image.
Build the application.
If you named your configuration file
cloudbuild.yaml, you can runthe following command: Tip: If you use a unique name for your configuration file, you can use thegcloudbuildssubmit.--configflag.
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.
- Clone the sample repository to your local machine:
gitclonehttps://github.com/GoogleCloudPlatform/buildpack-samples.git
- 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
- Use
gcloudto 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:
LOCATIONwith the region name of your container repository. Example:us-west2-docker.pkg.devPROJECT_IDwith the ID of your Google Cloud project.REPO_NAMEwith the name of your Docker repository.
- Verify that the sample function was successfully published into
REPO_NAME:gcloudartifactsdockerimageslistLOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME
Replace:
LOCATIONwith the region name of your container repository, for example,us-west2.PROJECT_IDwith the ID of your Google Cloud project.REPO_NAMEwith 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:latestReplace:
LOCATIONwith the region name of your container repository, for example,us-west2.IMAGE_NAMEwith the name of your container image.LANGUAGEwith the language of your function, for examplenodejs.RUNTIME_IDwith 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_NAMEReplace:
LOCATIONwith the region name of your container repository, for example,us-west2.PROJECT_IDwith the ID of your Google Cloud project.REPO_NAMEwith the name of your Docker repository.IMAGE_NAMEwith the name of your container image.LANGUAGEwith the language of your function, for examplenodejs.RUNTIME_IDwith the runtime ID, for examplenodejs24.
What's next?
- After you build your function into a container, test it locally before deploying toCloud Run. SeeTesting a Cloud Run service locallyto learn more.
- To deploy your built containers to Cloud Run, followDeploying services.
- To automate the builds and deployments of your Cloud Run servicesusing Cloud Build triggers, seeset up continuous deployment.
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.