There are two methods for building container images with buildpacks:
- Build locally with the
pack
CLI to locally test your function and rapidly prototype changes before deployment. - Build remotely with Cloud Build. Building with Cloud Build is useful for functions that have a resource-intensive build processes and can also help protect your software supply chain.
Configure your project to build functions
To build functions with buildpacks:
Include the Functions Framework library.
Set the
GOOGLE_FUNCTION_TARGET
environment variable to the name of the function that you use as the entrypoint. You can do this by including aproject.toml
in the same folder as your source code. Theproject.toml
file must have the following configuration:
[[build.env]]
name = "GOOGLE_FUNCTION_TARGET"
value = "ENTRY_POINT"
Replace ENTRY_POINT with the function method.
For details about how to use environment variables with Cloud Run functions, see Configure Cloud Run functions services.
Local builds
Pack
is a CLI tool maintained by the CNB project to support the use of buildpacks. Use
the pack
CLI to locally build your functions into a container image.
Before you begin
- Install Docker Community Edition (CE)
on your workstation. Docker is used by
pack
as an OCI image builder. - Install Pack CLI.
- Install the Git source control tool to fetch the sample application from GitHub.
Build a function locally
You use the pack build
command and specify the default builder
--builder=gcr.io/buildpacks/builder
to build your container images locally.
pack build --builder=gcr.io/buildpacks/builder IMAGE_NAME
Replace IMAGE_NAME with the name of your container image.
You can also customize your container image by extending 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:
git clone https://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
pack
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
- 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 to localhost:8080.
Remote builds
Use Cloud Build to build your function into a container image, and Artifact Registry as the container repository to store 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build and Artifact Registry APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build and Artifact Registry APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- Ensure that your Google Cloud project has access to a container image repository.
To configure access to a Docker repository in Artifact Registry:
- Create a new Docker repository in the same location of your Google Cloud project.
Replace:gcloud artifacts repositories create REPO_NAME \ --repository-format=docker \ --location=REGION --description="DESCRIPTION"
REPO_NAME
with the name that you choose for your Docker repository.REGION
with the location in or nearest to the location of your Google Cloud project.DESCRIPTION
with a description of your choice.
For example, to create a
docker
repository inus-west2
with the description "Docker repository", you run:gcloud artifacts repositories create buildpacks-docker-repo --repository-format=docker \ --location=us-west2 --description="Docker repository"
- Verify that your repository was created:
gcloud artifacts repositories list
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.
Build a function remotely
You use the gcloud builds submit
command to build and upload your container image to your repository.
You can choose to specify your container image in the command itself or use a configuration file.
Build with command
To build without a configuration file, specify the image
flag:
gcloud builds submit --pack image=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 a
configuration file
to define your image repository configuration
details to simply the build command. The configuration file uses the YAML file
format and must include a build step that uses the pack
CLI.
- Create a YAML file name
cloudbuild.yaml
that includes the URI of your container image repository.
options: logging: CLOUD_LOGGING_ONLY pool: {} projectId: PROJECT_ID steps: - name: gcr.io/k8s-skaffold/pack entrypoint: pack args: - build - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME - --builder - gcr.io/buildpacks/builder:latest - --network - cloudbuild images: - 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.
Build the application.
If you named your configuration file
cloudbuild.yaml
, you can run the following command:gcloud builds submit .
Example: Build a sample function remotely
The following examples demonstrate how to build a sample remotely, and verify that the container image was pushed to your repository in Artifact Registry.
- Clone the sample repository to your local machine:
git clone https://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
gcloud
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.
-
Verify that the sample function was successfully published into
REPO_NAME
:gcloud artifacts docker images list LOCATION-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.
What's Next
- Deploy your function image to Cloud Run.
- Set environment variables.
- Configure build images.
- Speed up builds with cache images.