|
| 1 | +# Google Cloud Run Java 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/java-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 Java, 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/java-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_NAME> |
| 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 for access to GCP services: |
| 61 | +
|
| 62 | + ``` |
| 63 | + # Set the name of the service account key within the container |
| 64 | + export SA_KEY_NAME=my-key-name-123 |
| 65 | +
|
| 66 | + PORT=8080 && docker run --rm \ |
| 67 | + -p 8080:${PORT} \ |
| 68 | + -e PORT=${PORT} \ |
| 69 | + -e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/${SA_KEY_NAME}.json \ |
| 70 | + -v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/${SA_KEY_NAME}.json:ro \ |
| 71 | + -v $PWD:/app $SAMPLE |
| 72 | + ``` |
| 73 | +
|
| 74 | + * Use the --volume (-v) flag to inject the credential file into the container |
| 75 | + (assumes you have already set your `GOOGLE_APPLICATION_CREDENTIALS` |
| 76 | + environment variable on your machine) |
| 77 | +
|
| 78 | + * Use the --environment (-e) flag to set the `GOOGLE_APPLICATION_CREDENTIALS` |
| 79 | + variable inside the container |
| 80 | +
|
| 81 | +Learn more about [testing your container image locally.][testing] |
| 82 | +
|
| 83 | +## Deploying |
| 84 | +
|
| 85 | +1. Set your GCP Project ID as an environment variable: |
| 86 | +``` |
| 87 | +export GOOGLE_CLOUD_PROJECT=<PROJECT_ID> |
| 88 | +``` |
| 89 | +
|
| 90 | +2. Choose to push your image to Google Container Registry or submit a build to |
| 91 | +Cloud Build: |
| 92 | + * Push the docker build to Google Container Registry: |
| 93 | + ``` |
| 94 | + docker tag $SAMPLE gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE} |
| 95 | + docker push gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE} |
| 96 | + ``` |
| 97 | + Learn more about [pushing and pulling images][push-pull]. |
| 98 | +
|
| 99 | + * Submit a build using Google Cloud Build: |
| 100 | + ``` |
| 101 | + gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE} |
| 102 | + ``` |
| 103 | +
|
| 104 | +3. Deploy to Cloud Run: |
| 105 | +``` |
| 106 | +gcloud beta run deploy $SAMPLE \ |
| 107 | + --set-env-vars GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT} \ |
| 108 | + --image gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE} |
| 109 | +``` |
| 110 | +
|
| 111 | +## Next Steps |
| 112 | +* See [building containers][run_build] and [deploying container images][run_deploy] |
| 113 | + for more information. |
| 114 | +
|
| 115 | +* [Dockerize a Spring Boot app][jib-tutorial] without a Dockerfile using [Jib][jib]. |
| 116 | +
|
| 117 | +* To improve [Tomcat startup time][startup], add |
| 118 | + `-Djava.security.egd=file:/dev/./urandom` to the Dockerfile's `CMD` |
| 119 | + instructions. This is acceptable for many applications. However, if your |
| 120 | + application does it's own key generation, such as for SSL, and you require |
| 121 | + greater security, you may prefer to not set `java.security.egd`. |
| 122 | +
|
| 123 | +
|
| 124 | +[run_docs]: https://cloud.google.com/run/docs/ |
| 125 | +[run_build]: https://cloud.google.com/run/docs/building/containers |
| 126 | +[run_deploy]: https://cloud.google.com/run/docs/deploying |
| 127 | +[helloworld]: https://github.com/knative/docs/tree/master/docs/serving/samples/hello-world/helloworld-java |
| 128 | +[pubsub]: pubsub/ |
| 129 | +[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-java |
| 130 | +[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/java-docs-samples&cloudshell_working_dir=run/cloudrun-pubsub |
| 131 | +[push-pull]: https://cloud.google.com/container-registry/docs/pushing-and-pulling |
| 132 | +[jib]: https://github.com/GoogleContainerTools/jib |
| 133 | +[jib-tutorial]: https://github.com/GoogleContainerTools/jib/tree/master/examples/spring-boot |
| 134 | +[startup]: https://cwiki.apache.org/confluence/display/TOMCAT/HowTo+FasterStartUp |
| 135 | +[testing]: https://cloud.google.com/run/docs/testing/local#running_locally_using_docker_with_access_to_services |
0 commit comments