Gcp
GcpIntermediate

Deploying Containerized Applications with Cloud Run

4 min read
gcpcomputecloud-runcontainersserverless

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

``mermaid

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

`

$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:

`dockerfile

Dockerfile

FROM node:18-slim

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

ENV PORT=8080

EXPOSE 8080

CMD ["npm", "start"]

`

Example Node.js application:

`javascript

// 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(Server running on port ${port});

});

`

$1

`bash

Build the container image

gcloud builds submit --tag gcr.io/PROJECT_ID/my-app

Deploy to Cloud Run

gcloud run deploy my-service \

--image gcr.io/PROJECT_ID/my-app \

--platform managed \

--region us-central1 \

--allow-unauthenticated

`

$1

$1

`yaml

service.yaml

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

`

$1

`bash

Set environment variables

gcloud run services update my-service \

--set-env-vars KEY1=VALUE1,KEY2=VALUE2

Use secrets from Secret Manager

gcloud run services update my-service \

--set-secrets="MY_SECRET=my-secret:latest"

`

$1

$1

`bash

Map custom domain

gcloud beta run domain-mappings create \

--service my-service \

--domain www.example.com

`

$1

`bash

Split traffic between revisions

gcloud run services update-traffic my-service \

--to-revisions=v1=50,v2=50

`

$1

$1

`bash

Require authentication

gcloud run services update my-service \

--no-allow-unauthenticated

Grant invoker role

gcloud run services add-iam-policy-binding my-service \

--member="user:jane@example.com" \

--role="roles/run.invoker"

`

$1

`bash

Configure VPC connector

gcloud run services update my-service \

--vpc-connector my-connector

Configure egress settings

gcloud run services update my-service \

--vpc-egress all-traffic

`

$1

$1

`bash

View service logs

gcloud logging read "resource.type=cloud_run_revision AND \

resource.labels.service_name=my-service" \

--limit 10

`

$1

Key metrics to monitor:

  • Request count and latency
  • Container instance count
  • Memory and CPU usage
  • Error rates
  • $1

    $1

  • Use multi-stage builds
  • Minimize image size
  • Optimize startup time
  • `dockerfile

    Multi-stage build example

    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"]

    `

    $1

  • Set appropriate memory limits
  • Configure concurrency
  • Optimize cold start times
  • `bash

    Configure resources

    gcloud run services update my-service \

    --memory 512Mi \

    --cpu 1 \

    --concurrency 80

    `

    $1

    $1

    `bash

    Check service status

    gcloud run services describe my-service

    View revision details

    gcloud run revisions list \

    --service my-service

    Check container logs

    gcloud logging read "resource.type=cloud_run_revision" \

    --project=PROJECT_ID

    `

    $1

    `yaml

    health-check.yaml

    spec:

    template:

    spec:

    containers:

    - image: gcr.io/PROJECT_ID/my-app

    livenessProbe:

    httpGet:

    path: /health

    port: 8080

    readinessProbe:

    httpGet:

    path: /ready

    port: 8080

    ``

    $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:

  • Use container best practices
  • Implement proper security measures
  • Monitor performance and costs
  • Optimize for scalability
  • Leverage Cloud Run features
  • 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.