How to Deploy to Cloud Run: Simple Steps for GCP Deployment
To deploy to
Cloud Run, first build a container image of your app and push it to Google Container Registry or Artifact Registry. Then run gcloud run deploy with your service name and image URL to launch your app as a serverless container.Syntax
The basic command to deploy an app to Cloud Run is:
gcloud run deploy SERVICE_NAME --image IMAGE_URL --platform managed --region REGIONHere’s what each part means:
- SERVICE_NAME: The name you want for your Cloud Run service.
- IMAGE_URL: The full path to your container image in Google Container Registry or Artifact Registry.
- --platform managed: Specifies you want to use fully managed Cloud Run.
- --region: The Google Cloud region where you want to deploy.
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...
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
Some common mistakes when deploying to Cloud Run include:
- Not building and pushing the container image before deploying. You must have your image in a registry accessible by Cloud Run.
- Forgetting to specify the
--platform managedflag, which is required for fully managed Cloud Run. - Using the wrong region or a region where Cloud Run is not available.
- Not setting correct permissions for the Cloud Run service account to access the container image.
Always verify your image URL and region before deploying.
bash
Wrong: gcloud run deploy my-service --image my-app-image Right: gcloud run deploy my-service --image gcr.io/my-project/my-app --platform managed --region us-central1
Quick Reference
Remember these key points when deploying to Cloud Run:
- Build your container image locally or with Cloud Build.
- Push the image to Google Container Registry or Artifact Registry.
- Use
gcloud run deploywith--platform managedand specify your region. - Check your service URL after deployment to access your app.
Key Takeaways
Build and push your container image before deploying to Cloud Run.
Use the command 'gcloud run deploy SERVICE_NAME --image IMAGE_URL --platform managed --region REGION' to deploy.
Always specify the platform as managed and choose a supported region.
Verify permissions so Cloud Run can access your container image.
Check the service URL after deployment to access your running app.