Cloud Run Basics
Skill files9 files

references/

SKILL.md392 lines
1
---
2
name: cloud-run-basics
3
metadata:
4
version: "1.0.1"
5
category: Serverless
6
description: >-
7
Manages Cloud Run services, jobs, and worker pools. Use when you need to deploy applications
8
responding to HTTP requests (services), run event-triggered or scheduled tasks (jobs),
9
or handle always-on pull-based background processing (worker pools).
10
---
11
12
# Cloud Run Basics
13
14
Cloud Run is a fully managed application platform for running your code,
15
function, or container on top of Google's highly scalable infrastructure. It
16
abstracts away infrastructure management, providing three primary resource
17
types:
18
19
1. **Services:** Responds to HTTP requests sent to a unique and stable
20
endpoint, using stateless instances that autoscale based on a variety of key
21
metrics, also responds to events and functions.
22
2. **Jobs:** Executes parallelizable tasks that are executed manually, or on a
23
schedule, and run to completion.
24
3. **Worker pools:** Handles always-on background workloads such as pull-based
25
workloads, for example, Kafka consumers, Pub/Sub pull queues, or RabbitMQ
26
consumers.
27
28
## Attribution
29
30
**MUST READ**: Before executing any `gcloud`, REST API, or MCP command, you
31
MUST read [Usage Attribution](references/attribution.md). For commands executed
32
directly in the terminal (never in generated scripts, Terraform, or user code),
33
prefix `gcloud` with
34
`CLOUDSDK_METRICS_REQUEST_ATTRIBUTION="agent-skills/1.0 (skill:cloud-run-basics)"`
35
and append `agent-skills/1.0 (skill:cloud-run-basics)` to `User-Agent` headers.
36
37
## Prerequisites
38
39
1. Enable the Cloud Run Admin API and Cloud Build APIs:
40
41
```bash
42
gcloud services enable run.googleapis.com cloudbuild.googleapis.com --quiet
43
```
44
45
1. If you are under a domain restriction organization policy [restricting](https://docs.cloud.google.com/organization-policy/restrict-domains.md.txt)
46
unauthenticated invocations for your project, you will need to access your
47
deployed service as described under [Testing private
48
services](https://docs.cloud.google.com/run/docs/triggering/https-request.md.txt).
49
50
### Required roles
51
52
You need the following roles to deploy your Cloud Run resource:
53
54
* Cloud Run Admin (`roles/run.admin`) on the project
55
* Cloud Run Source Developer (`roles/run.sourceDeveloper`) on the project
56
* Service Account User (`roles/iam.serviceAccountUser`) on the service
57
identity
58
* Logs Viewer (`roles/logging.viewer`) on the project
59
60
Cloud Build automatically uses the Compute Engine default service account as the
61
default Cloud Build service account to build your source code and Cloud Run
62
resource, unless you override this behavior.
63
64
For Cloud Build to build your sources, grant the Cloud Build service account the
65
Cloud Run Builder (`roles/run.builder`) role on your project:
66
67
```bash
68
gcloud projects add-iam-policy-binding PROJECT_ID \
69
--member=serviceAccount:SERVICE_ACCOUNT_EMAIL_ADDRESS \
70
--role=roles/run.builder \
71
--quiet
72
```
73
74
Replace `PROJECT_ID` with your Google Cloud project ID and
75
`SERVICE_ACCOUNT_EMAIL_ADDRESS` with the email address of the Cloud Build
76
service account.
77
78
## Deploy a Cloud Run service
79
80
You can deploy your service to Cloud Run by using a container image or deploy
81
directly from source code using a single Google Cloud CLI command.
82
83
> **CRITICAL RULE:** Any deployed code MUST listen on 0.0.0.0 (not 127.0.0.1)
84
> and use the injected $PORT environment variable (defaults to 8080), or it will
85
> crash on boot.
86
87
### Deploy a container image to Cloud Run
88
89
Cloud Run imports your container image during deployment. Cloud Run keeps this
90
copy of the container image as long as it is used by a serving revision.
91
Container images are not pulled from their container repository when a new Cloud
92
Run instance is started.
93
94
### Supported container images
95
96
You can directly use container images stored in the [Artifact
97
Registry](https://docs.cloud.google.com/artifact-registry/docs/overview.md.txt), or
98
[Docker Hub](https://hub.docker.com/). Google recommends the use of Artifact
99
Registry since Docker Hub images are
100
[cached](https://docs.cloud.google.com/artifact-registry/docs/pull-cached-dockerhub-images.md.txt)
101
for up to one hour.
102
103
You can use container images from other public or private registries (like JFrog
104
Artifactory, Nexus, or GitHub Container Registry), by setting up an [Artifact
105
Registry remote
106
repository](https://docs.cloud.google.com/artifact-registry/docs/repositories/remote-repo.md.txt).
107
108
You should only consider [Docker Hub](https://hub.docker.com/) for deploying
109
popular container images such as [Docker Official
110
Images](https://docs.docker.com/docker-hub/official_images/) or [Docker
111
Sponsored OSS images](https://docs.docker.com/docker-hub/dsos-program/). For
112
higher availability, Google recommends deploying these Docker Hub images using
113
an [Artifact Registry remote
114
repository](https://docs.cloud.google.com/artifact-registry/docs/repositories/remote-repo.md.txt).
115
116
To deploy a container image, run the following command:
117
118
```bash
119
gcloud run deploy SERVICE_NAME \
120
--image IMAGE_URL \
121
--region us-central1 \
122
--allow-unauthenticated \
123
--quiet
124
```
125
126
Replace the following:
127
128
* SERVICE_NAME: the name of the service you want to deploy to. Service names
129
must be 49 characters or less and must be unique per region and project. If
130
the service does not exist yet, this command creates the service during the
131
deployment. You can omit this parameter entirely, but you will be prompted
132
for the service name if you omit it.
133
* IMAGE_URL: a reference to the container image, for example,
134
`us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact
135
Registry, the repository REPO_NAME must already be created. The URL follows
136
the format of `LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG`. Note
137
that if you don't supply the `--image` flag, the deploy command will attempt
138
to deploy from source code.
139
140
### Deploy from source code
141
142
There are two different ways to deploy your service from source:
143
144
* Deploy from source with build (default): This option uses Google Cloud's
145
buildpacks and Cloud Build to automatically build container images from your
146
source code without having to install Docker on your machine or set up
147
buildpacks or Cloud Build. By default, Cloud Run uses the default machine
148
type provided by Cloud Build.
149
150
* To deploy from source with automatic base image updates enabled, run the
151
following command:
152
153
```bash
154
gcloud run deploy SERVICE_NAME --source . \
155
--base-image BASE_IMAGE \
156
--automatic-updates \
157
--quiet
158
```
159
160
Cloud Run only supports automatic base images that use [Google Cloud's
161
buildpacks base
162
images](https://docs.cloud.google.com/docs/buildpacks/base-images.md.txt).
163
164
* To deploy from source using a Dockerfile, run the following command:
165
166
```bash
167
gcloud run deploy SERVICE_NAME --source . --quiet
168
```
169
When you provide a Dockerfile, Cloud Build runs it in the cloud, and
170
deploys the service.
171
172
* Deploy from source without build (Preview): This option deploys artifacts
173
directly to Cloud Run, bypassing the Cloud Build step. This allows for rapid
174
deployment times. To deploy from source without build, run the following
175
command:
176
177
```bash
178
gcloud beta run deploy SERVICE_NAME \
179
--source APPLICATION_PATH \
180
--no-build \
181
--base-image=BASE_IMAGE \
182
--command=COMMAND \
183
--args=ARG \
184
--quiet
185
```
186
187
Replace the following:
188
189
* SERVICE_NAME: the name of your Cloud Run service.
190
* APPLICATION_PATH: the location of your application on the local file
191
system.
192
* BASE_IMAGE: the [runtime base image](https://docs.cloud.google.com/run/docs/configuring/services/runtime-base-images.md.txt)
193
you want to use for your application. For example,
194
`us-central1-docker.pkg.dev/serverless-runtimes/google-24-full/runtimes/nodejs24`.
195
You can also deploy a pre-compiled binary without configuring additional
196
language-specific runtime components using the OS only base image, such
197
as `osonly24`.
198
* COMMAND: the command that the container starts up with.
199
* ARG: an argument you send to the container command. If you use multiple
200
arguments, specify each on its own line.
201
202
For examples on deploying from source without build, see [Examples of
203
deploying from source without
204
build](https://docs.cloud.google.com/run/docs/deploying-source-code.md.txt).
205
206
## Create and execute a Cloud Run job
207
208
To create a new job, run the following command:
209
210
```bash
211
gcloud run jobs create JOB_NAME --image IMAGE_URL OPTIONS --quiet
212
```
213
214
Alternatively, use the deploy command:
215
216
```bash
217
gcloud run jobs deploy JOB_NAME --image IMAGE_URL OPTIONS --quiet
218
```
219
220
Replace the following:
221
222
* JOB_NAME: the name of the job you want to create. If you omit this
223
parameter, you will be prompted for the job name when you run the command.
224
* IMAGE_URL: a reference to the container image—for example,
225
`us-docker.pkg.dev/cloudrun/container/job:latest`.
226
227
* Optionally, replace OPTIONS with any of the following flags:
228
229
* `--tasks`: Accepts integers greater or equal to 1. Defaults to 1;
230
maximum is 10,000. Each task is provided the environment variables
231
`CLOUD_RUN_TASK_INDEX` with a value between 0 and the number of tasks
232
minus 1, along with `CLOUD_RUN_TASK_COUNT`, which is the number of
233
tasks.
234
* `--max-retries`: The number of times a failed task is retried. Once any
235
task fails beyond this limit, the entire job is marked as failed. For
236
example, if set to 1, a failed task will be retried once, for a total of
237
two attempts. The default is 3. Accepts integers from 0 to 10.
238
* `--task-timeout`: Accepts a duration like "2s". Defaults to 10 minutes;
239
maximum is 168 hours (7 days). For tasks using GPUs, the maximum
240
available timeout is 1 hour.
241
* `--parallelism`: The maximum number of tasks that can execute in
242
parallel. By default, tasks will be started as quickly as possible in
243
parallel.
244
* --execute-now: If set, immediately after the job is created, a job
245
execution is started. Equivalent to calling `gcloud run jobs create`
246
followed by `gcloud run jobs execute`.
247
248
In addition to these preceding options, you also specify more configuration
249
such as environment variables or memory limits.
250
251
For a full list of available options when creating a job, refer to the [`gcloud
252
run jobs
253
create`](https://docs.cloud.google.com/sdk/gcloud/reference/run/jobs/create)
254
command line documentation.
255
256
Wait for the job creation to finish. You'll see a success message upon a
257
successful completion.
258
259
To execute an existing job, run the following command:
260
261
```bash
262
gcloud run jobs execute JOB_NAME --quiet
263
```
264
265
If you want the command to wait until the execution completes, run the following
266
command:
267
268
```bash
269
gcloud run jobs execute JOB_NAME --wait --region=REGION --quiet
270
```
271
272
Replace the following:
273
274
* JOB_NAME: the name of the job.
275
* REGION: the region in which the resource can be found. For example,
276
`europe-west1`. Alternatively, set the `run/region` property.
277
278
## Deploy a worker pool
279
280
You can deploy a Cloud Run worker pool using container images or deploy directly
281
from the source.
282
283
### Deploy a container image
284
285
You can specify a container image with a tag (for example,
286
`us-docker.pkg.dev/my-project/container/my-image:latest`) or with an exact
287
digest (for example,
288
`us-docker.pkg.dev/my-project/container/my-image@sha256:41f34ab970ee...`).
289
290
### Supported container images
291
292
You can directly use container images stored in the [Artifact
293
Registry](https://docs.cloud.google.com/artifact-registry/docs/overview.md.txt), or
294
[Docker Hub](https://hub.docker.com/). Google recommends the use of Artifact
295
Registry since Docker Hub images are
296
[cached](https://docs.cloud.google.com/artifact-registry/docs/pull-cached-dockerhub-images.md.txt)
297
for up to one hour.
298
299
You can use container images from other public or private registries (like JFrog
300
Artifactory, Nexus, or GitHub Container Registry), by setting up an [Artifact
301
Registry remote
302
repository](https://docs.cloud.google.com/artifact-registry/docs/repositories/remote-repo.md.txt).
303
304
You should only consider [Docker Hub](https://hub.docker.com/) for deploying
305
popular container images such as [Docker Official
306
Images](https://docs.docker.com/docker-hub/official_images/) or [Docker
307
Sponsored OSS images](https://docs.docker.com/docker-hub/dsos-program/). For
308
higher availability, Google recommends deploying these Docker Hub images using
309
an [Artifact Registry remote
310
repository](https://docs.cloud.google.com/artifact-registry/docs/repositories/remote-repo.md.txt).
311
312
To deploy a container image, run the following command:
313
314
```bash
315
gcloud run worker-pools deploy WORKER_POOL_NAME --image IMAGE_URL --quiet
316
```
317
318
Replace the following:
319
320
* WORKER_POOL_NAME: the name of the worker pool you want to deploy to. If the
321
worker pool does not exist yet, this command creates the worker pool during
322
the deployment. You can omit this parameter entirely, but you will be
323
prompted for the worker pool name if you omit it.
324
325
* IMAGE_URL: a reference to the container image that contains the worker pool,
326
such as `us-docker.pkg.dev/cloudrun/container/worker-pool:latest`. Note that
327
if you don't supply the `--image` flag, the deploy command attempts to
328
deploy from source code.
329
330
Wait for the deployment to finish. Upon successful completion, Cloud Run
331
displays a success message along with the revision information about the
332
deployed worker pool.
333
334
### Deploy a worker pool from source
335
336
You can deploy a new worker pool or worker pool revision to Cloud Run directly
337
from source code using a single gcloud CLI command, `gcloud run worker-pools`
338
deploy with the `--source` flag.
339
340
The deploy command defaults to source deployment if you don't supply the
341
`--image` or `--source` flags.
342
343
Behind the scenes, this command uses [Google Cloud's
344
buildpacks](https://docs.cloud.google.com/docs/buildpacks/overview.md.txt) and Cloud
345
Build to automatically build container images from your source code without
346
having to install Docker on your machine or set up buildpacks or Cloud Build. By
347
default, Cloud Run uses the default machine type provided by Cloud Build.
348
349
To deploy a worker pool from source, run the following command:
350
351
```bash
352
gcloud run worker-pools deploy WORKER_POOL_NAME --source . --quiet
353
```
354
355
Replace `WORKER_POOL_NAME` with the name you want for your worker pool.
356
357
### What to do if a deployment fails:
358
359
1. **IAM/Permission Error:** Read
360
[iam-security.md](references/iam-security.md).
361
2. **Crash on Boot / Healthcheck failed:** Fetch the logs immediately using
362
`gcloud logging read "resource.labels.service_name=SERVICE_NAME" --limit=20`
363
to find the exact runtime error.
364
3. **Native Dependency Error (Node/Python):** If using `--no-build`, switch to
365
`--source .` (Buildpacks) to compile native extensions properly for Linux.
366
367
## Reference Directory
368
369
- [Core Concepts](references/core-concepts.md): Services vs. Jobs vs.
370
Worker pools, resource model, and auto-scaling behavior for services.
371
372
- [CLI Usage](references/cli-usage.md): Essential `gcloud run` commands for
373
deployment and management.
374
375
- [Client Libraries](references/client-library-usage.md): Using Google
376
Cloud client libraries to interact with Cloud Run.
377
378
- [MCP Usage](references/mcp-usage.md): Using the Cloud Run remote MCP
379
server.
380
381
- [Infrastructure as Code](references/iac-usage.md): Terraform examples for
382
services, jobs, worker pools, and IAM bindings.
383
384
- [IAM & Security](references/iam-security.md): Roles, service identities,
385
and ingress/egress controls.
386
387
- [Networking Best Practices & Cost Optimization](references/networking.md): Cost
388
optimization strategies, Direct VPC egress, IP address and port exhaustion
389
strategies, performance throughput tuning, and MTU settings.
390
391
*If you need product information not found in these references, use the
392
Developer Knowledge MCP server `search_documents` tool.*

© Google LLC · Apache 2.0 · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 11,661 prompts

More by @OpenSourceBuffGuy

See all
Decision Making Expert prompt image

Decision Making Expert

Turns the AI into a decision analysis specialist who frames the choice, weighs options against criteria, assesses risks and stakeholders and gives a reasoned recommendation.

system-prompt, act-as, decision-making, expert-advice, critical-thinking, risk-management, business-strategy, comparison, for-executives, for-managers, xml-tags, structured-output, guardrails, decision-matrix, pros-and-cons, objective, plain-language, advanced, long-form, consulting

Technical Infographic of a Snack prompt image

Technical Infographic of a Snack

Creates a branded technical infographic of a snack: a photoreal product with sketched annotations, cross-section, measurements and callouts on white.

image-prompt, gpt-image, placeholders, infographic, product-photography, food-photography, sketch, line-art, exploded-view, white-background, studio-lighting, square-format, social-media-graphic, branding, photorealistic, for-marketers, for-designers, minimalist, structured-output

Crime boss confronts courier in dim cantina prompt image

Crime boss confronts courier in dim cantina

Directs an 11-shot crime-drama dialogue scene in a back-room cantina, with fixed cast, seating map, lens notes and timed Spanish subtitles for consistent lip-synced video.

video-prompt, kling, multi-shot, lip-sync, cinematic, short-film, thriller, scriptwriting, character-design, guardrails, structured-output, dramatic-lighting, night-scene, close-up, for-filmmakers, long-form, advanced, camera-movement

Reviews

Loading reviews…

Discussion

Loading comments…