Introduction
Cloud Run lets you run serverless containers on Google infrastructure. This expert tutorial covers deploying a Node.js API with automatic scaling, fine-grained IAM authentication, and CI/CD integration. You will learn to optimize costs and latency in production.
Prerequisites
- Google Cloud account with billing enabled
- gcloud SDK installed (version 450+)
- Docker and Node.js 20+
- Advanced knowledge of containers and IAM
Project Initialization
gcloud config set project mon-projet-12345
gcloud services enable run.googleapis.com cloudbuild.googleapis.com artifactregistry.googleapis.comEnable the required APIs and set the target project. Avoid permission errors by enabling these services before any deployment.
Creating the Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 8080
CMD ["node", "server.js"]Minimal Alpine image to reduce size. Port 8080 is mandatory for Cloud Run. Use npm ci for reproducible builds.
Minimal Node.js Application
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Cloud Run 2026'));
const port = process.env.PORT || 8080;
app.listen(port, () => console.log(`Running on ${port}`));Basic Express application that listens on the PORT provided by Cloud Run. Always use process.env.PORT.
Initial Deployment
gcloud run deploy mon-api \
--source . \
--region europe-west1 \
--allow-unauthenticated \
--memory 512Mi \
--cpu 1Direct deployment from source code using integrated Cloud Build. Specify region, memory, and CPU to control costs and performance.
Advanced service.yaml Configuration
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: mon-api
spec:
template:
metadata:
annotations:
autoscaling.knative.dev/maxScale: "100"
run.googleapis.com/cpu-throttling: "false"
spec:
containerConcurrency: 80
containers:
- image: europe-west1-docker.pkg.dev/mon-projet-12345/cloud-run/mon-api
resources:
limits:
memory: 1Gi
cpu: 2Configuration file for precise scaling, disabling CPU throttling, and resource limits. Use with gcloud run services replace.
CI/CD Pipeline with Cloud Build
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'europe-west1-docker.pkg.dev/$PROJECT_ID/cloud-run/mon-api:$SHORT_SHA', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'europe-west1-docker.pkg.dev/$PROJECT_ID/cloud-run/mon-api:$SHORT_SHA']
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args: ['run', 'deploy', 'mon-api', '--image', 'europe-west1-docker.pkg.dev/$PROJECT_ID/cloud-run/mon-api:$SHORT_SHA', '--region', 'europe-west1']Complete pipeline that builds, pushes, and deploys automatically. Trigger it via a Git push trigger.
Best Practices
- Always use immutable images with SHA digest
- Configure IAM with dedicated service accounts
- Enable scale-to-zero to minimize costs
- Monitor with Cloud Monitoring and alerts
- Test cold starts with synthetic requests
Common Errors
- Forgetting to expose port 8080 in the container
- Insufficient IAM permissions on the service account
- Memory limits too low causing OOM errors
- Not configuring request timeouts for long-running tasks
Going Further
Deepen your knowledge with our advanced Cloud Run courses.