正在加载,请稍候…

Google Cloud Run: Serverless Containers That Scale to Zero

Deploy containers on Cloud Run — autoscaling, min instances, traffic splitting, Secret Manager integration, and cost optimization.

Cloud Run: Any Container, Fully Managed

Scales to zero when idle, scales to 1000+ instances in seconds.

Deploy from Source

gcloud run deploy my-api   --source .   --region us-central1   --allow-unauthenticated   --memory 512Mi   --min-instances 0   --max-instances 100   --concurrency 80

Optimized Multi-Stage Dockerfile

FROM node:20-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production

FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json tsconfig.json ./
RUN npm ci
COPY src/ ./src/
RUN npm run build

FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production PORT=8080
RUN addgroup -S app && adduser -S app -G app
COPY --from=deps /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
USER app
EXPOSE 8080
CMD ["node", "dist/main.js"]

Traffic Splitting for Safe Deploys

# Deploy without traffic
gcloud run deploy my-api --image gcr.io/project/v2 --no-traffic

# Canary: 10% to new version
gcloud run services update-traffic my-api --to-revisions=my-api-v2=10,my-api-v1=90

# Full rollout
gcloud run services update-traffic my-api --to-latest

Minimum Instances

gcloud run services update my-api --min-instances 2
# ~$0.10/day per instance — prevents cold starts for key services

-> Encode secrets with the Base64 Converter.