Deploying to Google Cloud Run
Serverless containers on GCP with scale-to-zero, pay-per-request, and automatic HTTPS.
PlatformsIntermediate
Why
Cloud Run runs any container serverlessly — scale to zero when idle, pay only for requests. It handles HTTPS, autoscaling, and logging automatically, making it the simplest way to deploy containers on GCP.
Platform-as-a-Service adoption grew 24% in 2024 as teams shift from VM management to git-push deploys. the cloud infrastructure market reached $330B in 2024, growing 22% year-over-year (Synergy Research), and PaaS layers capture the majority of new workloads.
When
Use Cloud Run for stateless containerized APIs, web apps, and background workers. Avoid for stateful apps or WebSocket-heavy workloads.
How
1. Build image — docker build -t gcr.io/PROJECT/app .
2. Push — docker push gcr.io/PROJECT/app
3. Deploy — gcloud run deploy app --image gcr.io/PROJECT/app --region us-central1
4. Allow unauthenticated — --allow-unauthenticated for public APIs
5. Env vars — --set-env-vars KEY=VALUE
6. Autoscaling — --min-instances 1 --max-instances 10
7. Cloud SQL — --add-cloudsql-instances for Postgres/MySQL
Example
# Deploy from source (auto-builds with Cloud Build)
gcloud run deploy myapp --source . --region us-central1 --allow-unauthenticated --set-env-vars DATABASE_URL=postgres://... --min-instances 0 --max-instances 10 --memory 512Mi --cpu 1
# Dockerfile (any container works)
FROM node:20-alpine
WORKDIR /app
COPY . .
RUN npm ci && npm run build
EXPOSE 8080
CMD ["node", "dist/server.js"] # must listen on $PORT
Common Problems
- ⚠Container doesn't listen on PORT — Cloud Run injects PORT env, must bind to 0.0.0.0:$PORT
- ⚠Cold start 5-10s — container pulled and started on first request, set --min-instances 1 to avoid
- ⚠Configuration drift between environments — use infrastructure-as-code (Terraform/Pulumi) to keep dev/staging/prod identical
Best Practices
- ✓Set --min-instances 1 for latency-sensitive APIs — eliminates cold starts at small cost
- ✓Use --source . for simple deploys — Cloud Build auto-builds and pushes the image
- ✓Set up health checks and graceful shutdown — handle SIGTERM to drain in-flight requests before exit