Cloud Run runs stateless containers on a fully managed environment or in your own GKE cluster.
| Sample | Description | Deploy |
|---|---|---|
| Hello World | Quickstart | |
| System Packages | Use system-installed binaries in your service. | |
| Pub/Sub | Processing messages from a Pub/Sub push subscription | |
| Image Processing | Cloud Storage & Pub/Sub-driven image analysis & transformation | |
| Manual Logging | Structured logging without client library | |
| Cloud SQL (MySQL) | Use MySQL with Cloud Run | - |
| Cloud SQL (Postgres) | Use Postgres with Cloud Run | - |
| Service to Service Requests ➥ | Create requests to authenticated-only services | - |
| Hello Broken | Something is wrong, how do you fix it? | |
| Pub/Sub - Events | Event-driven service with Events for Cloud Run for Pub/Sub | - |
| Pub/Sub - Anthos Events | Event-driven service with Events for Cloud Run on Anthos for Pub/Sub | - |
| Cloud Storage - Events | Event-driven service with Events for Cloud Run for GCS | - |
| Cloud Storage - Anthos Events | Event-driven service with Events for Cloud Run on Anthos for GCS | - |
For more Cloud Run samples beyond Node.js, see the main list in the Cloud Run Samples repository.
-
Clone this repository:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Note: Some samples in the list above are hosted in other repositories. They are noted with the symbol "➥".
-
export SAMPLE=$sample cd $SAMPLE docker build --tag $sample .
-
With the built container:
PORT=8080 && docker run --rm -p 8080:${PORT} -e PORT=${PORT} $SAMPLE
Overriding the built container with local code:
PORT=8080 && docker run --rm \ -p 8080:${PORT} -e PORT=${PORT} \ -v $PWD:/usr/src/app $SAMPLE
Injecting your service account key:
export SA_KEY_NAME=my-key-name-123 PORT=8080 && docker run --rm \ -p 8080:${PORT} -e PORT=${PORT} \ -e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/${SA_KEY_NAME}.json \ -v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/${SA_KEY_NAME}.json:ro \ -v $PWD:/usr/src/app $SAMPLE
Opening a shell in the container (e.g., updating the
package-lock.json):-
Build the container.
-
Run the container with a shell:
PORT=8080 && docker run --rm \ --interactive --tty \ -p 8080:${PORT} -e PORT=${PORT} \ -v $PWD:/usr/src/app $SAMPLE \ /bin/bash
- Re-generate the
package-lock.json:
rm package-lock.json npm install
Because we're using a read/write volume mount, the revised file will be written to the host's local filesystem. Once you exit the container you can add the file to version control.
- Exit the container:
Ctrl-D
-
# Run unit & integration tests.
npm test
# Run system tests.
SAMPLE=[SAMPLE_TO_TEST]
CONTAINER_IMAGE=gcr.io/$GOOGLE_CLOUD_PROJECT/${SAMPLE}:manual
gcloud builds submit --tag $CONTAINER_IMAGE
SERVICE_NAME=${SAMPLE} npm run system-test
gcloud container images delete gcr.io/$GOOGLE_CLOUD_PROJECT/${SAMPLE}:manualgcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
gcloud run deploy ${SAMPLE} \
# Needed for Manual Logging sample.
--set-env-var GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT} \
--image gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}See Building containers and Deploying container images for more information.