Skip to content

Cloud Run

Cloud Run is a fully managed serverless platform that runs stateless containers. You bring a container image; GCP handles scaling, load balancing, TLS, and infrastructure.

  • Serverless — no VMs or clusters to manage
  • Container-native — any language or runtime that fits in a Docker image
  • Auto-scaling — scales from 0 to thousands of instances based on traffic
  • Pay-per-use — billed only when requests are being processed
Terminal window
# Build and push to Google Artifact Registry
gcloud builds submit --tag gcr.io/PROJECT_ID/my-app
# Deploy to Cloud Run
gcloud run deploy my-app \
--image gcr.io/PROJECT_ID/my-app \
--platform managed \
--region europe-west2 \
--allow-unauthenticated

The --allow-unauthenticated flag makes the service publicly accessible.

Terminal window
# Set memory and CPU
gcloud run deploy my-app \
--image gcr.io/PROJECT_ID/my-app \
--memory 512Mi \
--cpu 1 \
--min-instances 0 \
--max-instances 100
# Set concurrency (requests per instance)
gcloud run deploy my-app \
--image gcr.io/PROJECT_ID/my-app \
--concurrency 80
# Set request timeout
gcloud run deploy my-app \
--image gcr.io/PROJECT_ID/my-app \
--timeout 300
Terminal window
gcloud run deploy my-app \
--image gcr.io/PROJECT_ID/my-app \
--set-env-vars="NODE_ENV=production,LOG_LEVEL=info"

Or from a file:

Terminal window
gcloud run deploy my-app \
--env-vars-file=.env.yaml
Terminal window
# Mount a secret as an environment variable
gcloud run deploy my-app \
--image gcr.io/PROJECT_ID/my-app \
--set-secrets="DB_PASSWORD=db-password:latest"
# Mount as a volume
gcloud run deploy my-app \
--set-secrets="/secrets/db=db-password:latest"

Cloud Run supports splitting traffic between revisions — useful for canary deployments:

Terminal window
gcloud run services update-traffic my-app \
--to-revisions my-app-00001-abc=90,my-app-00002-def=10

Cloud Run vs Cloud Functions vs App Engine

Section titled “Cloud Run vs Cloud Functions vs App Engine”
FeatureCloud RunCloud FunctionsApp Engine
RuntimeAny containerEvent-driven functionsLanguage-specific
Scaling0 to N instances0 to N instances0 to N instances
Max request timeout60 min9 min (HTTP)10 min
Stateful warm-upYes (min-instances)LimitedYes
Best forAPIs, web appsLightweight event handlersTraditional web apps
cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-app', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/my-app']
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args:
- 'run'
- 'deploy'
- 'my-app'
- '--image=gcr.io/$PROJECT_ID/my-app'
- '--region=europe-west2'
- '--platform=managed'
Terminal window
# List services
gcloud run services list
# Describe a service (URL, revisions, config)
gcloud run services describe my-app --region=europe-west2
# View logs
gcloud logging read "resource.type=cloud_run_revision AND resource.labels.service_name=my-app" --limit 50
# Delete a service
gcloud run services delete my-app --region=europe-west2