How to Deploy a Container to Google Cloud Run
To deploy a container to
Cloud Run, first build and push your container image to Google Container Registry or Artifact Registry. Then run gcloud run deploy with your service name and image URL to create a serverless container service.Syntax
The main command to deploy a container to Cloud Run is:
gcloud run deploy SERVICE_NAME --image IMAGE_URL --platform managed --region REGIONExplanation:
- SERVICE_NAME: Name you want for your Cloud Run service.
- IMAGE_URL: URL of your container image in Google Container Registry or Artifact Registry.
- --platform managed: Specifies Cloud Run fully managed environment.
- --region: The Google Cloud region to deploy your service.
bash
gcloud run deploy SERVICE_NAME --image IMAGE_URL --platform managed --region REGION
Example
This example shows how to deploy a simple container image named gcr.io/my-project/my-app as a Cloud Run service called my-service in the us-central1 region.
bash
gcloud run deploy my-service --image gcr.io/my-project/my-app --platform managed --region us-central1
Output
Deploying container to Cloud Run service [my-service] in project [my-project] region [us-central1]
ā Deploying new service... Done.
ā Creating Revision...
ā Routing traffic...
Done.
Service [my-service] revision [my-service-00001-abc] has been deployed and is serving 100 percent of traffic.
Service URL: https://my-service-abcdefg-uc.a.run.app
Common Pitfalls
Common mistakes when deploying to Cloud Run include:
- Not pushing the container image to a Google Container Registry or Artifact Registry before deploying.
- Using the wrong image URL or forgetting to include the project ID in the image path.
- Not specifying the
--platform managedflag, which is required for fully managed Cloud Run. - Deploying to a region different from where the image is stored, causing access issues.
- Forgetting to enable the Cloud Run API or set up proper permissions.
Example of a wrong command and the fix:
# Wrong: missing --platform managed
gcloud run deploy my-service --image gcr.io/my-project/my-app --region us-central1
# Right:
gcloud run deploy my-service --image gcr.io/my-project/my-app --platform managed --region us-central1Quick Reference
Tips for smooth Cloud Run deployment:
- Always build and push your container image before deploying.
- Use
gcloud auth configure-dockerto authenticate Docker with Google Container Registry. - Choose the region closest to your users for lower latency.
- Use
--allow-unauthenticatedif you want your service to be public. - Check Cloud Run logs with
gcloud logs readfor troubleshooting.
Key Takeaways
Build and push your container image to Google Container Registry or Artifact Registry before deploying.
Use the command 'gcloud run deploy SERVICE_NAME --image IMAGE_URL --platform managed --region REGION' to deploy.
Always specify '--platform managed' to deploy to Cloud Run fully managed environment.
Ensure your Cloud Run API is enabled and you have proper permissions.
Use '--allow-unauthenticated' flag to make your service publicly accessible if needed.