TL;DR
Master serverless container deployment with Google Cloud Run. Learn how to build, deploy, and manage containerized applications with automatic scaling and zero infrastructure management.
Deploying Applications on Google Cloud Run
Google Cloud Run is a fully managed compute platform that automatically scales your stateless containers. This guide covers everything you need to know to deploy and manage applications on Cloud Run.
$1
`` graph TB
subgraph CloudRun["Cloud Run Service"]
direction TB
subgraph Containers["Container Instances"]
direction LR
C1["Container 1"]
C2["Container 2"]
C3["Container 3"]
end
subgraph Features["Platform Features"]
direction LR
AS["Auto Scaling"]
LB["Load Balancing"]
SSL["SSL/TLS"]
end
subgraph Integration["Service Integration"]
direction LR
IAM["IAM"]
SM["Secret Manager"]
LOG["Cloud Logging"]
end
end
subgraph Triggers["Event Sources"]
direction TB
HTTP["HTTP Requests"]
PubSub["Cloud Pub/Sub"]
Tasks["Cloud Tasks"]
end
Triggers --> CloudRun
CloudRun --> Integration classDef primary fill:#4285f4,stroke:#666,stroke-width:2px,color:#fff
classDef secondary fill:#34a853,stroke:#666,stroke-width:2px,color:#fff
classDef tertiary fill:#fbbc05,stroke:#666,stroke-width:2px,color:#fff
class CloudRun,Containers primary
class Features,Integration secondary
class Triggers,HTTP,PubSub,Tasks tertiary
mermaid
`
$1
| Feature | Description |
|---------|-------------|
| Automatic Scaling | Scales to zero when not in use |
| Request-based Billing | Pay only for actual usage |
| Container Support | Run any container image |
| HTTPS Endpoints | Automatic SSL/TLS certificates |
| Custom Domains | Map your own domain names |
$1
$1
First, create a simple application and containerize it:
` FROM node:18-slim WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . . ENV PORT=8080
EXPOSE 8080 CMD ["npm", "start"]
dockerfile
`Dockerfile
Example Node.js application:
` // app.js
const express = require('express');
const app = express();
const port = process.env.PORT || 8080; app.get('/', (req, res) => {
res.send('Hello from Cloud Run!');
}); app.listen(port, () => {
console.log( });
javascript
Server running on port ${port});
`
$1
` gcloud builds submit --tag gcr.io/PROJECT_ID/my-app gcloud run deploy my-service \
--image gcr.io/PROJECT_ID/my-app \
--platform managed \
--region us-central1 \
--allow-unauthenticated
bash
`Build the container image
Deploy to Cloud Run
$1
$1
` apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: my-service
spec:
template:
spec:
containers:
- image: gcr.io/PROJECT_ID/my-app
env:
- name: NODE_ENV
value: "production"
resources:
limits:
cpu: "1"
memory: "256Mi"
ports:
- containerPort: 8080
yaml
`service.yaml
$1
` gcloud run services update my-service \
--set-env-vars KEY1=VALUE1,KEY2=VALUE2 gcloud run services update my-service \
--set-secrets="MY_SECRET=my-secret:latest"
bash
`Set environment variables
Use secrets from Secret Manager
$1
$1
` gcloud beta run domain-mappings create \
--service my-service \
--domain www.example.com
bash
`Map custom domain
$1
` gcloud run services update-traffic my-service \
--to-revisions=v1=50,v2=50
bash
`Split traffic between revisions
$1
$1
` gcloud run services update my-service \
--no-allow-unauthenticated gcloud run services add-iam-policy-binding my-service \
--member="user:jane@example.com" \
--role="roles/run.invoker"
bash
`Require authentication
Grant invoker role
$1
` gcloud run services update my-service \
--vpc-connector my-connector gcloud run services update my-service \
--vpc-egress all-traffic
bash
`Configure VPC connector
Configure egress settings
$1
$1
` gcloud logging read "resource.type=cloud_run_revision AND \
resource.labels.service_name=my-service" \
--limit 10
bash
`View service logs
$1
Key metrics to monitor:
$1
$1
` FROM node:18 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build FROM node:18-slim
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY package*.json ./
RUN npm install --production
CMD ["npm", "start"]
dockerfile
`Multi-stage build example
$1
` gcloud run services update my-service \
--memory 512Mi \
--cpu 1 \
--concurrency 80
bash
`Configure resources
$1
$1
` gcloud run services describe my-service gcloud run revisions list \
--service my-service gcloud logging read "resource.type=cloud_run_revision" \
--project=PROJECT_ID
bash
`Check service status
View revision details
Check container logs
$1
` spec:
template:
spec:
containers:
- image: gcr.io/PROJECT_ID/my-app
livenessProbe:
httpGet:
path: /health
port: 8080
readinessProbe:
httpGet:
path: /ready
port: 8080
yaml
``health-check.yaml
$1
1. Optimize Container Resources
- Right-size memory and CPU
- Use minimum instances wisely
- Implement efficient auto-scaling
2. Request Handling
- Implement caching where appropriate
- Optimize response times
- Use compression
$1
Cloud Run provides a powerful platform for running containerized applications with zero infrastructure management. Key takeaways:
For more information, refer to the [official Cloud Run documentation](https://cloud.google.com/run/docs).
Why This Matters
Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.
Trade-offs to Consider
Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.
When NOT to Use This
Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.
Decision Framework
Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.