|
| 1 | +# Google Cloud Run Python Samples |
| 2 | + |
| 3 | +[![Open in Cloud Shell][shell_img]][shell_link] |
| 4 | + |
| 5 | +[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png |
| 6 | +[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=blog/README.md |
| 7 | + |
| 8 | +This directory contains samples for [Google Cloud Run](https://cloud.run). [Cloud Run][run_docs] runs stateless [containers](https://cloud.google.com/containers/) on a fully managed environment or in your own GKE cluster. |
| 9 | + |
| 10 | +## Samples |
| 11 | + |
| 12 | +| Sample | Description | Deploy | |
| 13 | +| ------------------------------- | ------------------------ | ------------- | |
| 14 | +|[Hello World][helloworld] ➥ | Quickstart | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_helloworld] | |
| 15 | +|[Cloud Pub/Sub][pubsub] | Handling Pub/Sub push messages | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_pubsub] | |
| 16 | + |
| 17 | +For more Cloud Run samples beyond Python, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples). |
| 18 | + |
| 19 | +## Setup |
| 20 | + |
| 21 | +1. [Set up for Cloud Run development](https://cloud.google.com/run/docs/setup) |
| 22 | + |
| 23 | +2. Clone this repository: |
| 24 | + |
| 25 | + ``` |
| 26 | + git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git |
| 27 | + ``` |
| 28 | +
|
| 29 | + Note: Some samples in the list above are hosted in other repositories. They are noted with the symbol "➥". |
| 30 | +
|
| 31 | +
|
| 32 | +## How to run a sample locally |
| 33 | +
|
| 34 | +1. [Install docker locally](https://docs.docker.com/install/) |
| 35 | +
|
| 36 | +2. [Build the sample container](https://cloud.google.com/run/docs/building/containers#building_locally_and_pushing_using_docker): |
| 37 | +
|
| 38 | + ``` |
| 39 | + export SAMPLE=$sample |
| 40 | + cd $SAMPLE |
| 41 | + docker build --tag $sample . |
| 42 | + ``` |
| 43 | +
|
| 44 | +3. [Run containers locally](https://cloud.google.com/run/docs/testing/local) |
| 45 | +
|
| 46 | + With the built container: |
| 47 | +
|
| 48 | + ``` |
| 49 | + PORT=8080 && docker run --rm -p 8080:${PORT} -e PORT=${PORT} $SAMPLE |
| 50 | + ``` |
| 51 | +
|
| 52 | + Overriding the built container with local code: |
| 53 | +
|
| 54 | + ``` |
| 55 | + PORT=8080 && docker run --rm \ |
| 56 | + -p 8080:${PORT} -e PORT=${PORT} \ |
| 57 | + -v $PWD:/app $SAMPLE |
| 58 | + ``` |
| 59 | +
|
| 60 | + Injecting your service account key: |
| 61 | +
|
| 62 | + ``` |
| 63 | + export SA_KEY_NAME=my-key-name-123 |
| 64 | + PORT=8080 && docker run --rm \ |
| 65 | + -p 8080:${PORT} -e PORT=${PORT} \ |
| 66 | + -e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/${SA_KEY_NAME}.json \ |
| 67 | + -v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/${SA_KEY_NAME}.json:ro \ |
| 68 | + -v $PWD:/app $SAMPLE |
| 69 | + ``` |
| 70 | +
|
| 71 | +## Deploying |
| 72 | +
|
| 73 | +``` |
| 74 | +gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE} |
| 75 | +gcloud beta run deploy $SAMPLE \ |
| 76 | + # Needed for Manual Logging sample. |
| 77 | + --set-env-var GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT} |
| 78 | + --image gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE} |
| 79 | +``` |
| 80 | +
|
| 81 | +See [Building containers][run_build] and [Deploying container images][run_deploy] |
| 82 | +for more information. |
| 83 | +
|
| 84 | +[run_docs]: https://cloud.google.com/run/docs/ |
| 85 | +[run_build]: https://cloud.google.com/run/docs/building/containers |
| 86 | +[run_deploy]: https://cloud.google.com/run/docs/deploying |
| 87 | +[helloworld]: https://github.com/knative/docs/tree/master/docs/serving/samples/hello-world/helloworld-python |
| 88 | +[pubsub]: pubsub/ |
| 89 | +[run_button_helloworld]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/knative/docs&cloudshell_working_dir=docs/serving/samples/hello-world/helloworld-python |
| 90 | +[run_button_pubsub]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&cloudshell_working_dir=run/pubsub |
0 commit comments