Google Cloud Run takes a container image and runs it without you touching a single server. It scales to zero when idle and scales up under load automatically. If your workload is stateless and containerized, Cloud Run is worth evaluating seriously.

What is Google Cloud Run?

Google Cloud Run is a fully managed compute platform built on Knative. You give it a container, set a few configuration parameters, and it handles everything else: provisioning, scaling, load balancing, TLS termination. It bills per 100ms of actual request processing time, so idle services cost nothing.

Why Choose Google Cloud Run?

  • No infrastructure to manage. You write code and build containers; Cloud Run handles the rest.
  • Scales automatically from zero to thousands of concurrent instances based on traffic.
  • Pay only for the resources consumed during active request handling.
  • Accepts any language or binary that runs inside a container.

Getting Started with Google Cloud Run

Prerequisites

  • A Google Cloud account with billing enabled.
  • Docker installed locally.
  • The gcloud CLI installed and authenticated.

Step 1: Write Your Application

Start by writing a simple application. For demonstration purposes, we'll use a basic Node.js app:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, Google Cloud Run!');
});

const port = process.env.PORT || 8080;
app.listen(port, () => {
  console.log(`App listening on port ${port}`);
});

Step 2: Containerize Your Application

Create a Dockerfile for your application:

FROM node:14

WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install

COPY . .

ENV PORT 8080
EXPOSE 8080

CMD [ "node", "index.js" ]

Step 3: Build and Test Your Container Locally

Build your Docker image:

docker build -t gcr.io/your-project-id/your-app .

Run your container locally to test:

docker run -p 8080:8080 gcr.io/your-project-id/your-app

Visit http://localhost:8080 to ensure it's working.

Step 4: Deploy to Google Cloud Run

Authenticate with Google Cloud:

gcloud auth login

Set your project ID:

gcloud config set project your-project-id

Push your Docker image to Google Container Registry:

docker push gcr.io/your-project-id/your-app

Deploy your container to Cloud Run:

gcloud run deploy your-app \
  --image gcr.io/your-project-id/your-app \
  --platform managed \
  --region us-central1 \
  --allow-unauthenticated

Step 5: Test Your Deployed Application

After deployment, Google Cloud Run will provide a URL for your service. Visit the URL to see your application live.

Tips for Optimizing Your Cloud Run Service

  • Use minimal base images. Smaller images start faster and reduce your attack surface.
  • Tune the concurrency setting. By default each instance handles 80 concurrent requests, but CPU-bound workloads should use a lower value.
  • Set explicit CPU and memory limits to avoid unexpected billing spikes.

Integrating with Other Google Cloud Services

Cloud Run connects cleanly to the rest of GCP:

  • Cloud SQL for managed relational databases, using the Cloud SQL Auth Proxy sidecar.
  • Cloud Pub/Sub for event-driven invocation: Pub/Sub pushes messages to your Cloud Run endpoint.
  • Cloud Storage for object storage, accessed via the standard client library.

Conclusion

Cloud Run removes most of the operational overhead associated with running containerized services. The main constraint is statelessness: instances can be created or destroyed at any time, so any state needs to live outside the container in a database or cache. Within that constraint, it's a practical and cost-effective deployment target.

Further Reading


Samuel Fajreldines is a specialist in JavaScript and TypeScript ecosystems, expert in DevOps and Serverless Architecture, and proficient in PHP frameworks.