
Cloud
Learning Level
Cloud Run is a fully managed serverless platform that lets you run containerized applications with automatic scaling. You pay only for the time your code is running, making it cost-effective for variable workloads and perfect for APIs, microservices, and web applications.
By the end of this lesson, you'll understand:
Ensure you have:
Create a new directory and add these files:
package.json:
{
"name": "hello-cloud-run",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^4.18.0"
}
}index.js:
const express = require('express');
const app = express();
const PORT = process.env.PORT || 8080;
app.get('/', (req, res) => {
res.json({
message: 'Hello from Cloud Run!',
timestamp: new Date().toISOString()
});
});
app.get('/health', (req, res) => {
res.json({ status: 'healthy' });
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});The Dockerfile tells Docker how to build your container image.
FROM node:16-alpine
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install production dependencies
RUN npm ci --only=production
# Copy application code
COPY . .
EXPOSE 8080
CMD ["npm", "start"]Key points:
# Navigate to your project directory
cd my-cloud-run-app
# Build the image
docker build -t hello-cloud-run:latest .
# Test locally before deploying (optional)
docker run -p 8080:8080 hello-cloud-run:latest
# Visit http://localhost:8080 in your browserGoogle Container Registry (GCR) stores your Docker images.
# Configure Docker to authenticate with GCR
gcloud auth configure-docker
# Tag your image for GCR
docker tag hello-cloud-run:latest gcr.io/PROJECT_ID/hello-cloud-run:latest
# Push the image
docker push gcr.io/PROJECT_ID/hello-cloud-run:latest
# Verify the image was pushed
gcloud container images listReplace PROJECT_ID with your actual GCP project ID.
gcloud run deploy hello-cloud-run \\
--image gcr.io/PROJECT_ID/hello-cloud-run:latest \\
--platform managed \\
--region us-central1 \\
--allow-unauthenticatedFor more control, specify additional parameters:
gcloud run deploy hello-cloud-run \\
--image gcr.io/PROJECT_ID/hello-cloud-run:latest \\
--platform managed \\
--region us-central1 \\
--allow-unauthenticated \\
--memory 512M \\
--cpu 1 \\
--timeout 600 \\
--max-instances 50 \\
--set-env-vars "LOG_LEVEL=info,ENVIRONMENT=production"Parameters explained:
After deployment completes, you'll see a URL like:
https://hello-cloud-run-xxxxx-us-central1.a.run.app# Get your service URL
gcloud run services describe hello-cloud-run \\
--region us-central1 \\
--format='value(status.url)'
# Test your service
curl https://hello-cloud-run-xxxxx-us-central1.a.run.app/Your container MUST follow these rules:
const PORT = process.env.PORT || 8080;
app.listen(PORT);Cloud Run sets the PORT environment variable automatically. Your app must use it.
process.on('SIGTERM', () => {
console.log('Received SIGTERM, shutting down');
server.close(() => process.exit(0));
});Cloud Run sends SIGTERM when shutting down instances. Respond gracefully.
gcloud run services update hello-cloud-run \\
--set-env-vars DATABASE_URL=postgres://...,LOG_LEVEL=debug \\
--region us-central1For sensitive data like API keys and passwords:
# Create a secret
echo "my-secure-password" | gcloud secrets create db-password --data-file=-
# Grant Cloud Run service account access
gcloud secrets add-iam-policy-binding db-password \\
--member=serviceAccount:PROJECT_ID@appspot.gserviceaccount.com \\
--role=roles/secretmanager.secretAccessor
# Reference in deployment
gcloud run deploy hello-cloud-run \\
--image gcr.io/PROJECT_ID/hello-cloud-run:latest \\
--set-env-vars DB_PASSWORD=/run/secrets/db-password \\
--region us-central1# View last 50 lines of logs
gcloud run logs read hello-cloud-run --limit 50
# Stream logs in real-time
gcloud run logs read hello-cloud-run --follow
# View logs from specific time
gcloud run logs read hello-cloud-run \\
--start-time=2024-01-15T10:00:00Z \\
--end-time=2024-01-15T11:00:00ZService won't start:
Out of memory errors:
Timeout errors:
Container image too large:
1. Deploy your first service
2. Configure environment variables for your application
3. Set up Cloud Secret Manager for sensitive data
4. Explore advanced configurations in the advanced lesson
5. Learn about monitoring and debugging in the monitoring lesson
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward