Building Containers

Cloud Run 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 Cloud Run into your container.

In this page, we describe two ways to build container images: using Docker and using Cloud Build.

Before building your sources into a container image ("containerizing"), you need a Dockerfile to be present along your sources. The Build and deploy quickstart contains sample applications and Dockerfiles in many popular languages.

Building using Cloud Build

You can build your image on Google Cloud by using Cloud Build:

  1. Navigate to the folder containing your sources and Dockerfile.

  2. Run the command:

     gcloud builds submit --tag gcr.io/[PROJECT-ID]/[IMAGE]
    

    Replace [PROJECT-ID] with your GCP project ID and replace [IMAGE] with the image name of your choice.

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.

To build your container image using Docker:

  1. Navigate to the folder containing your sources and Dockerfile.

  2. Run the command:

     docker build . --tag gcr.io/[PROJECT-ID]/[IMAGE]
    

    Replace [PROJECT-ID] with your GCP project ID and replace [IMAGE] with the image name of your choice.

  3. If you have not yet configured Docker to use the gcloud command-line tool 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.

  4. Push the container image to Container Registry:

     docker push gcr.io/[PROJECT-ID]/[IMAGE]
    

    Replace [PROJECT-ID] with your GCP project ID and replace [IMAGE] with the image name you chose when you invoked docker build.

To exclude local files from this process, follow the .dockerignore configuration file instructions.

What's next

本頁內容對您是否有任何幫助?請提供意見:

傳送您對下列選項的寶貴意見...

這個網頁
Cloud Run Documentation