Stay organized with collections
Save and categorize content based on your preferences.
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:
gcloudbuildssubmit--tagIMAGE_URL
Replace IMAGE_URL with a reference to the container image, for
example, gcr.io/myproject/my-image:latest.
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
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eKnative serving supports container images built with any tool that adheres to the container contract, requiring the code to listen for HTTP requests on the \u003ccode\u003ePORT\u003c/code\u003e environment variable.\u003c/p\u003e\n"],["\u003cp\u003eContainer images can be built using either a Dockerfile or Buildpacks, offering flexibility in the building process.\u003c/p\u003e\n"],["\u003cp\u003eWhen using a Dockerfile, builds can be performed either through Cloud Build using the \u003ccode\u003egcloud builds submit\u003c/code\u003e command or locally with Docker using the \u003ccode\u003edocker build\u003c/code\u003e command.\u003c/p\u003e\n"],["\u003cp\u003eBuildpacks, which are compatible with CNCF standards, allow building code in Go, Node.js, Python, Java, and .NET Core into container images for Google Cloud.\u003c/p\u003e\n"],["\u003cp\u003eBuilding with Buildpacks can be done via Cloud Build with \u003ccode\u003egcloud builds submit --pack\u003c/code\u003e or using the \u003ccode\u003epack\u003c/code\u003e command line tool with the \u003ccode\u003epack build --publish\u003c/code\u003e command.\u003c/p\u003e\n"]]],[],null,["# Building containers\n\nKnative serving accepts container images built with any tool capable of\nbuilding container images, as long as they respect the [container contract](/anthos/run/archive/docs/reference/container-contract).\nIn particular, your code must listen for HTTP requests on the port defined by\nthe `PORT` environment variable. This `PORT` environment variable is\nautomatically injected by Knative serving into your container.\n\nThis page describes several ways to build container images:\n\n- Using a Dockerfile\n- Using Buildpacks\n\nBuilding using a Dockerfile\n---------------------------\n\nIf you use Dockerfiles, you can use either of the following methods to build:\n\n- Build using Cloud Build\n- Build locally using Docker\n\n### Building using Cloud Build\n\nTo build your image on Google Cloud:\n\n1. Navigate to the folder containing your sources and Dockerfile.\n\n2. Run the command:\n\n ```bash\n gcloud builds submit --tag IMAGE_URL\n ```\n\n Replace \u003cvar translate=\"no\"\u003eIMAGE_URL\u003c/var\u003e with a reference to the container image, for\n example, `gcr.io/myproject/my-image:latest`.\n\nFor detailed Cloud Build instructions, see\n[Deploying to Knative serving](/build/docs/deploying-builds/deploy-cloud-run#building_and_deploying_a_container).\n\nFor tips on improving build performance, see\n[Speeding up your Builds](/build/docs/speeding-up-builds)\n\n### Building locally and pushing using Docker\n\nIf you have Docker [installed locally](https://docs.docker.com/install/), you\ncan use [`docker build`](https://docs.docker.com/engine/reference/commandline/build/)\ninstead of using Cloud Build or Buildpacks.\n\nBefore building your sources into a container image (\"containerizing\") using\nDocker, you need a\n[Dockerfile](https://docs.docker.com/engine/reference/builder/) to be present\nalong your sources.\n\nTo build your container image using Docker:\n\n1. Navigate to the folder containing your sources and `Dockerfile`.\n\n2. Run the command:\n\n ```bash\n docker build . --tag IMAGE_URL\n ```\n\n Replace \u003cvar translate=\"no\"\u003eIMAGE_URL\u003c/var\u003e with a reference to the container image, for\n example, `gcr.io/myproject/my-image:latest`.\n3. If you have not yet configured Docker to use the Google Cloud CLI to\n authenticate requests to Container Registry, do so now using the command:\n\n ```bash\n gcloud auth configure-docker\n ```\n\n You need to do this before you can push or pull images using Docker. You\n only need to do it once.\n4. Push the container image to Container Registry:\n\n ```bash\n docker push IMAGE_URL\n ```\n\n Replace \u003cvar translate=\"no\"\u003eIMAGE_URL\u003c/var\u003e with a reference to the container image, for\n example, `gcr.io/myproject/my-image:latest`.\n\nTo exclude local files from this process, follow the\n[`.dockerignore` configuration file](https://docs.docker.com/engine/reference/builder/#dockerignore-file)\ninstructions.\n\nBuilding using Buildpacks\n-------------------------\n\nFor complete information on Buildpacks, see the\n[Buildpacks](https://buildpacks.io/) website. Google provides a set of\n[CNCF-compatible Buildpacks](/docs/buildpacks/overview)\nthat build source code into container images designed to run\non Google Cloud container platforms, including Knative serving.\n\nThese platforms are currently supported:\n\n- Go\n- Node.js\n- Python\n- Java\n- .NET Core\n\n### Building with Buildpacks using Cloud Build\n\nTo build with a Buildpack:\n\n1. Navigate to the folder containing your sources.\n\n2. Run the command:\n\n ```\n gcloud builds submit --pack image=IMAGE_URL\n ```\n\n Replace \u003cvar translate=\"no\"\u003eIMAGE_URL\u003c/var\u003e with the container image URL, for example,\n `gcr.io/myproject/myservice`.\n3. Wait for the build to complete.\n\n### Building with Buildpack using the `pack` command line\n\nTo build using the [pack command](https://github.com/buildpacks/pack):\n\n1. If you haven't already done so, [install Docker](https://docs.docker.com/install/).\n\n2. If you have not yet configured Docker to use the Google Cloud CLI to\n authenticate requests to Container Registry, do so now using the command:\n\n ```bash\n gcloud auth configure-docker\n ```\n\n You need to do this before you can push or pull images using Docker. You\n only need to do it once.\n3. If you haven't already done so, [install `pack`](https://buildpacks.io/docs/tools/pack/cli/install/).\n\n4. Navigate to the folder containing your sources.\n\n5. Run the following command to build and push to your container registry:\n\n ```\n pack build --publish IMAGE_URL\n ```\n\n Replace \u003cvar translate=\"no\"\u003eIMAGE_URL\u003c/var\u003e with a reference to the container image, for\n example, `gcr.io/myproject/my-image:latest`.\n6. Wait for `pack` to finish.\n\nFor more information, read the instructions under\n[Building an Application](/docs/buildpacks/build-application).\n\nWhat's next\n-----------\n\n- To learn more about the contract your containers must respect to be deployed\n to Knative serving, see\n [Container Contract](/anthos/run/archive/docs/reference/container-contract).\n\n- To deploy your built containers to Knative serving, follow\n [Deploying Services](/anthos/run/archive/docs/deploying).\n\n- To automate the builds and deployments of your Knative serving\n services using Cloud Build Triggers,\n [set up Continuous Deployment](/anthos/run/archive/docs/continuous-deployment-with-cloud-build)."]]