Knative serving accepts container images built with any tool capable of
building container images, as long as they respect the container contract.
In particular, your code must listen for HTTP requests on the port defined by
the PORT
environment variable. This PORT
environment variable is
automatically injected by Knative serving into your container.
This page describes several ways to build container images:
- Using a Dockerfile
- Using Buildpacks
Building using a Dockerfile
If you use Dockerfiles, you can use either of the following methods to build:
- Build using Cloud Build
- Build locally using Docker
Building using Cloud Build
To build your image on Google Cloud:
Navigate to the folder containing your sources and Dockerfile.
Run the command:
gcloud builds submit --tag IMAGE_URL
Replace IMAGE_URL with a reference to the container image, for example,
gcr.io/cloudrun/hello
.
For detailed Cloud Build instructions, see Deploying to Knative serving.
For tips on improving build performance, see Speeding up your Builds
Building locally and pushing using Docker
If you have Docker installed locally, you
can use docker build
instead of using Cloud Build or Buildpacks.
Before building your sources into a container image ("containerizing") using Docker, you need a Dockerfile to be present along your sources. The following instructions use Container Registry as the image registry.
To build your container image using Docker:
Navigate to the folder containing your sources and
Dockerfile
.Run the command:
docker build . --tag IMAGE_URL
Replace IMAGE_URL with a reference to the container image, for example,
gcr.io/cloudrun/hello
.If you have not yet configured Docker to use the Google Cloud CLI to authenticate requests to Container Registry, do so now using the command:
gcloud auth configure-docker
You need to do this before you can push or pull images using Docker. You only need to do it once.
Push the container image to Container Registry:
docker push IMAGE_URL
Replace IMAGE_URL with a reference to the container image, for example,
gcr.io/cloudrun/hello
.
To exclude local files from this process, follow the
.dockerignore
configuration file
instructions.
Building using Buildpacks
For complete information on Buildpacks, see the Buildpacks website. Google provides a set of CNCF-compatible Buildpacks that build source code into container images designed to run on Google Cloud container platforms, including Knative serving.
These platforms are currently supported:
- Go
- Node.js
- Python
- Java
- .NET Core
Building with Buildpacks using Cloud Build
To build with a Buildpack:
Navigate to the folder containing your sources.
Run the command:
gcloud builds submit --pack image=IMAGE_URL
Replace IMAGE_URL with the container image URL, for example,
gcr.io/myproject/myservice
.Wait for the build to complete.
Building with Buildpack using the pack
command line
To build using the pack command:
If you haven't already done so, install Docker.
If you have not yet configured Docker to use the Google Cloud CLI to authenticate requests to Container Registry, do so now using the command:
gcloud auth configure-docker
You need to do this before you can push or pull images using Docker. You only need to do it once.
If you haven't already done so, install
pack
.Navigate to the folder containing your sources.
Run the following command to build and push to your container registry:
pack build --publish IMAGE_URL
Replace IMAGE_URL with a reference to the container image, for example,
gcr.io/cloudrun/hello
.Wait for
pack
to finish.
For more information, read the instructions under Building an Application.
What's next
To learn more about the contract your containers must respect to be deployed to Knative serving, see Container Contract.
To deploy your built containers to Knative serving, follow Deploying Services.
To automate the builds and deployments of your Knative serving services using Cloud Build Triggers, set up Continuous Deployment.