Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
O Cloud Run aceita imagens de contêiner criadas com qualquer ferramenta capaz de compilar imagens de contêiner, desde que elas respeitem o contrato do contêiner.
Especificamente, seu código precisa detectar solicitações HTTP na porta definida pela variável de ambiente PORT. Essa variável de ambiente PORT é injetada automaticamente pelo Cloud Run no contêiner.
Nesta página, você verá várias maneiras de criar imagens de contêiner:
Como usar um Dockerfile
Como usar os buildpacks do Google Cloud
Antes de começar
Você precisa da CLI do Google Cloud para executar alguns dos comandos desta página.
Antes de criar as origens em uma imagem de contêiner ("conteinerização") usando
o Docker ou o Cloud Build, é preciso que um
Dockerfile esteja presente
nas origens.
As amostras do Hello World
contêm aplicativos de amostra e Dockerfiles em muitas linguagens conhecidas.
Se você usa Dockerfiles, use um dos seguintes métodos para criar:
Criar usando o Cloud Build.
Criar localmente usando o Docker.
Como criar usando o Cloud Build
É possível compilar sua imagem no Google Cloud usando o Cloud Build:
Navegue até a pasta que contém suas fontes e o Dockerfile.
Execute o comando:
gcloudbuildssubmit--tagIMAGE_URL
Substitua IMAGE_URL por uma referência à imagem de contêiner, por
exemplo, us-docker.pkg.dev/cloudrun/container/hello:latest. Se você usa o Artifact Registry,
o repositórioREPO_NAME já
precisará ter sido criado. O URL tem o formato LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
Se você tiver o Docker instalado localmente,
pode usar docker build
em vez de usar o Cloud Build ou os buildpacks do Google Cloud.
Para criar a imagem de contêiner usando o Docker, siga estas etapas:
Navegue até a pasta que contém suas origens e Dockerfile.
Execute o comando:
dockerbuild.--tagIMAGE_URL
Substitua IMAGE_URL por uma referência à imagem de contêiner, por
exemplo, us-docker.pkg.dev/cloudrun/container/hello:latest. Se você usa o Artifact Registry,
o repositórioREPO_NAME já
precisará ter sido criado. O URL tem o formato LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
Se você estiver usando um Mac com o Apple Silicon, precisará especificar --platform linux/amd64
na linha de comando.
Envie a imagem do contêiner para um Container Registry com suporte:
dockerpushIMAGE_URL
Substitua IMAGE_URL por uma referência à imagem de contêiner, por
exemplo, us-docker.pkg.dev/cloudrun/container/hello:latest. Se você usa o Artifact Registry,
o repositórioREPO_NAME já
precisará ter sido criado. O URL tem o formato LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
Os buildpacks do Google Cloud são um conjunto de
Buildpacks compatíveis com CNCF que criam o código-fonte em
imagens de contêiner projetadas para serem executadas em plataformas de contêiner do Google Cloud,
incluindo o Cloud Run.
Como criar com os buildpacks do Google Cloud usando o Cloud Build
Para criar com os buildpacks do Google Cloud:
Navegue até a pasta que contém as origens.
Execute o comando:
gcloud builds submit --pack image=IMAGE_URL
Substitua IMAGE_URL por uma referência à imagem de contêiner, por
exemplo, us-docker.pkg.dev/cloudrun/container/hello:latest. Se você usa o Artifact Registry,
o repositórioREPO_NAME já
precisará ter sido criado. O URL tem o formato LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
Aguarde a conclusão da criação.
Como criar com os buildpacks do Google Cloud usando a linha de comando pack
Execute o seguinte comando para criar e enviar por push para o Container Registry com suporte:
pack build --publish IMAGE_URL
Substitua IMAGE_URL por uma referência à imagem de contêiner, por
exemplo, us-docker.pkg.dev/cloudrun/container/hello:latest. Se você usa o Artifact Registry,
o repositórioREPO_NAME já
precisará ter sido criado. O URL tem o formato LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
Para automatizar as compilações e implantações dos serviços do Cloud Run
usando os gatilhos do Cloud Build, consulte Configurar a implantação contínua.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2024-12-05 UTC."],[],[],null,["# Build sources to containers\n\nCloud Run supports [directly deploying source code](/run/docs/deploying-source-code),\nhowever, you can also build your source code into a container image and then\ndeploy this container image to Cloud Run. You can use container images\nfor any Cloud Run resource.\n\nCommon use cases for decoupling build and deploy operations:\n\n- **Continuous Integration and Delivery**: Developers author and push code to a source repository, a CI/CD system automatically builds this source code into a container, runs tests, and automatically deploys it to a staging environment.\n- **Infrastructure as Code**: Cloud Run resources that are managed using YAML or Terraform reference a container image URL. The source code written by developers need to be built into a container image.\n\nYou can use any system you want to build a container. This page describes\nthe following ways to use Cloud Build to build container images:\n\n- [Using a Dockerfile](#use-dockerfile)\n- [Using Google Cloud's buildpacks](#buildpacks)\n\nRequirements for Cloud Run services\n-----------------------------------\n\nFor Cloud Run services, you can use container images built with any tool capable of\nbuilding container images, as long as they respect the [container contract](/run/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 Cloud Run into your container.\n\nBefore you begin\n----------------\n\n- You need the [Google Cloud CLI](/run/docs/setup) to run some of the commands in this page.\n\n- Create a repository at a [supported container registry](/run/docs/deploying#images). To\n [create an Artifact Registry repository](/artifact-registry/docs/repositories/create-repos#create),\n run:\n\n gcloud artifacts repositories create \u003cvar translate=\"no\"\u003eREPOSITORY\u003c/var\u003e \\\n --repository-format=docker \\\n --location=\u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e \\\n --description=\"\u003cvar translate=\"no\"\u003eDESCRIPTION\u003c/var\u003e\" \\\n --immutable-tags \\\n --async\n\n- You can configure Docker to get access to Artifact Registry using the\n [gcloud CLI credential helper](/artifact-registry/docs/docker/authentication#gcloud-helper):\n\n ```bash\n gcloud auth configure-docker LOCATION-docker.pkg.dev\n ```\n Replace \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e with the region name of your container repository, for example, `us-west2`.\n\n \u003cbr /\u003e\n\nBuild using a Dockerfile\n------------------------\n\n| **Caution:** Don't include the file `~/.config/gcloud/gce` in your image. This file is generated if you run Google Cloud CLI commands as part of your image build. It can cause authentication issues when the image runs in Cloud Run.\n\nBefore building your sources into a container image (\"containerizing\") locally\nusing Docker or using Cloud Build, you need a\n[Dockerfile](https://docs.docker.com/engine/reference/builder/) to be present\nalong with your sources.\nThe [Hello World samples](https://github.com/GoogleCloudPlatform/cloud-run-samples/blob/main/README.md)\ncontain sample applications and Dockerfiles in many popular languages.\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### Build using Cloud Build\n\nYou can build your image on Google Cloud by using [Cloud Build](/build/docs):\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, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry,\n the [repository](/artifact-registry/docs/repositories/create-repos#docker) \u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e must\n already be created. The URL follows the format of \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e`-docker.pkg.dev/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ePATH\u003c/var\u003e`:`\u003cvar translate=\"no\"\u003eTAG\u003c/var\u003e\n .\n\nFor tips on improving build performance, see\n[Speeding up your builds](/build/docs/speeding-up-builds)\n\n### Build locally and push 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 Google Cloud's buildpacks.\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, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry,\n the [repository](/artifact-registry/docs/repositories/create-repos#docker) \u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e must\n already be created. The URL follows the format of \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e`-docker.pkg.dev/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ePATH\u003c/var\u003e`:`\u003cvar translate=\"no\"\u003eTAG\u003c/var\u003e\n .\n\n Note that if you are using a Mac with Apple silicon, you must specify `--platform linux/amd64`\n in the command line.\n3. Push the container image to a supported 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, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry,\n the [repository](/artifact-registry/docs/repositories/create-repos#docker) \u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e must\n already be created. The URL follows the format of \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e`-docker.pkg.dev/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ePATH\u003c/var\u003e`:`\u003cvar translate=\"no\"\u003eTAG\u003c/var\u003e\n .\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\nBuild using Google Cloud's buildpacks\n-------------------------------------\n\n[Google Cloud's buildpacks](/docs/buildpacks/builders) is a set of\n[CNCF-compatible Buildpacks](https://buildpacks.io) that build source code into\ncontainer images designed to run on Google Cloud container platforms,\nincluding Cloud Run.\n\nFor a list of supported languages, refer to the [Google Cloud's buildpacks documentation](/docs/buildpacks/builders)\n\n### Build with Google Cloud's buildpacks using Cloud Build\n\nTo build with a Google Cloud's buildpacks:\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 a reference to the container image, for\n example, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry,\n the [repository](/artifact-registry/docs/repositories/create-repos#docker) \u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e must\n already be created. The URL follows the format of \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e`-docker.pkg.dev/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ePATH\u003c/var\u003e`:`\u003cvar translate=\"no\"\u003eTAG\u003c/var\u003e\n .\n3. Wait for the build to complete.\n\n### Build with Google Cloud's buildpacks using the `pack` command line\n\nTo build using the [pack command](/docs/buildpacks/build-application):\n\n1. If you haven't already done so, [install Docker](https://docs.docker.com/install/).\n\n2. If you haven't already done so, [install `pack`](https://buildpacks.io/docs/tools/pack/cli/install/).\n\n3. Navigate to the folder containing your sources.\n\n4. Run the following command to build and push to your supported 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, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry,\n the [repository](/artifact-registry/docs/repositories/create-repos#docker) \u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e must\n already be created. The URL follows the format of \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e`-docker.pkg.dev/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003eREPO_NAME\u003c/var\u003e`/`\u003cvar translate=\"no\"\u003ePATH\u003c/var\u003e`:`\u003cvar translate=\"no\"\u003eTAG\u003c/var\u003e\n .\n5. 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 deploy your built containers to Cloud Run, follow [Deploying services](/run/docs/deploying).\n\n- Learn how to [create and update Cloud Run jobs](/run/docs/create-jobs)\n from your built container image.\n\n- After your container has been built, you can test locally before deploying to\n Cloud Run; see [Testing a Cloud Run service locally](/run/docs/testing/local) to learn more.\n\n- After you create or update a job using the built container, see how to\n [execute the job](/run/docs/execute/jobs) as a one-off, on a schedule, or as\n part of a [workflow](/run/docs/triggering/invoke-jobs-with-workflows).\n\n- To automate the builds and deployments of your Cloud Run services\n using Cloud Build Triggers, [set up continuous deployment](/run/docs/continuous-deployment).\n\n- To perform optimal container builds for Java application, see\n [Building Java containers with Jib](/java/getting-started/jib)."]]