[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],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](/kubernetes-engine/enterprise/knative-serving/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/cloudrun/hello`.\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. The following instructions use Container Registry as the\nimage registry.\n| **Caution:** Container Registry is deprecated. Effective March 18, 2025,\n| Container Registry is shut down, and writing images to Container Registry is unavailable. For\n| details on the deprecation and how to migrate to Artifact Registry, see\n| [Container Registry deprecation](/container-registry/docs/deprecations/container-registry-deprecation).\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/cloudrun/hello`.\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/cloudrun/hello`.\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.\nGoogle 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/cloudrun/hello`.\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](/kubernetes-engine/enterprise/knative-serving/docs/reference/container-contract).\n\n- To deploy your built containers to Knative serving, follow\n [Deploying Services](/kubernetes-engine/enterprise/knative-serving/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](/kubernetes-engine/enterprise/knative-serving/docs/continuous-deployment-with-cloud-build)."]]