# Cloud Run — Google Cloud Platform

Source: https://www.skillbyai.com/en/gcp/c-run

> Deploy containers that scale with requests, including to zero.

## Serverless containers

**Cloud Run** runs any container that listens on the port given in the `PORT` environment variable. You deploy an image (or source code, which Cloud Build turns into an image) and Google manages servers, TLS, scaling and load balancing. **Services** handle HTTP or gRPC requests; **jobs** run containers to completion for batch work. Each instance handles many requests at once, set by **concurrency** (default 80, up to 1000), and the number of instances scales with traffic, down to **zero** when idle unless you set **minimum instances** to avoid cold starts. Every deploy creates an immutable **revision**, and you can split traffic between revisions for canary releases. Access can be public, or restricted so callers need the **Cloud Run Invoker** role. Containers should be stateless: anything written to the local filesystem lives in memory and disappears with the instance.

## Deploy from source, then a canary

`--no-traffic` deploys a revision without sending users to it.

```bash
gcloud run deploy orders-api --source . --region=asia-south1 \
  --service-account=orders-api@shop-dev-123456.iam.gserviceaccount.com \
  --min-instances=1 --max-instances=20 --concurrency=80 --no-allow-unauthenticated

# next release: deploy with a tag, test, then shift 10% of traffic
gcloud run deploy orders-api --source . --region=asia-south1 --no-traffic --tag=canary
gcloud run services update-traffic orders-api --region=asia-south1 --to-tags=canary=10

# a batch job
gcloud run jobs deploy nightly-report --image=asia-south1-docker.pkg.dev/shop-dev-123456/apps/report:1.0
gcloud run jobs execute nightly-report
```

## Shared autos at a station

Each Cloud Run instance is an auto that takes several passengers (concurrency). When the train arrives more autos appear; at 2 a.m. none wait around unless you pay one driver to stay (minimum instances).

**Quiz:** A Cloud Run service has noticeable cold starts after quiet periods. What is the simplest fix?

- [ ] Lower concurrency to 1
- [ ] Switch to a zonal disk
- [ ] Make the service public
- [x] Set minimum instances to 1 or more

*Answer:* Set minimum instances to 1 or more. Minimum instances keep warm containers ready, at the cost of paying for them while idle.
