From 3dabdb24e41bdf483d76b076b0f42b6c216e1f21 Mon Sep 17 00:00:00 2001 From: Averi Kitsch Date: Tue, 19 Nov 2019 08:44:49 -0800 Subject: [PATCH] Add Dockerfile and instructions for Cloud Run and fix variable (#1745) --- cloud-sql/mysql/servlet/Dockerfile | 35 +++++++++++ cloud-sql/mysql/servlet/README.md | 59 +++++++++++++++---- cloud-sql/mysql/servlet/pom.xml | 12 +++- .../src/main/webapp/WEB-INF/appengine-web.xml | 2 +- run/README.md | 3 + 5 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 cloud-sql/mysql/servlet/Dockerfile diff --git a/cloud-sql/mysql/servlet/Dockerfile b/cloud-sql/mysql/servlet/Dockerfile new file mode 100644 index 00000000000..2890f6c1bc5 --- /dev/null +++ b/cloud-sql/mysql/servlet/Dockerfile @@ -0,0 +1,35 @@ +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Use the official maven/Java 11 image to create a build artifact. +# https://hub.docker.com/_/maven +FROM maven:3.6.2-jdk-8-slim as builder + +# Copy local code to the container image. +WORKDIR /app +COPY pom.xml . +COPY src ./src + +# Build a release artifact. +RUN mvn package -DskipTests + +# Use the Official Jetty image for a lean production stage of our multi-stage build. +# https://hub.docker.com/_/jetty +# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds +FROM jetty:9.4-jre8 + +# Copy the exploded WAR directory from the builder stage to the Jetty web application directory. +COPY --from=builder /app/target/tabs-vs-spaces-mysql-*/* $JETTY_BASE/webapps/ROOT/ + +# No CMD needed since Jetty automatically scans, loads, and starts the web app. diff --git a/cloud-sql/mysql/servlet/README.md b/cloud-sql/mysql/servlet/README.md index c5c23cca993..163d75b1cb7 100644 --- a/cloud-sql/mysql/servlet/README.md +++ b/cloud-sql/mysql/servlet/README.md @@ -2,21 +2,21 @@ ## Before you begin -1. If you haven't already, set up a Java Development Environment (including google-cloud-sdk and -maven utilities) by following the [java setup guide](https://cloud.google.com/java/docs/setup) and +1. If you haven't already, set up a Java Development Environment (including google-cloud-sdk and +maven utilities) by following the [java setup guide](https://cloud.google.com/java/docs/setup) and [create a project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project). -1. Create a 2nd Gen Cloud SQL Instance by following these +1. Create a 2nd Gen Cloud SQL Instance by following these [instructions](https://cloud.google.com/sql/docs/mysql/create-instance). Note the connection string, database user, and database password that you create. -1. Create a database for your application by following these +1. Create a database for your application by following these [instructions](https://cloud.google.com/sql/docs/mysql/create-manage-databases). Note the database -name. +name. -1. Create a service account with the 'Cloud SQL Client' permissions by following these +1. Create a service account with the 'Cloud SQL Client' permissions by following these [instructions](https://cloud.google.com/sql/docs/mysql/connect-external-app#4_if_required_by_your_authentication_method_create_a_service_account). -Download a JSON key to use to authenticate your connection. +Download a JSON key to use to authenticate your connection. 1. Use the information noted in the previous steps: ```bash @@ -41,9 +41,9 @@ Navigate towards `http://127.0.0.1:8080` to verify your application is running c ## Google App Engine Standard -To run on GAE-Standard, create an AppEngine project by following the setup for these -[instructions](https://cloud.google.com/appengine/docs/standard/java/quickstart#before-you-begin) -and verify that +To run on GAE-Standard, create an AppEngine project by following the setup for these +[instructions](https://cloud.google.com/appengine/docs/standard/java/quickstart#before-you-begin) +and verify that [appengine-maven-plugin](https://cloud.google.com/java/docs/setup#optional_install_maven_or_gradle_plugin_for_app_engine) has been added in your build section as a plugin. @@ -57,10 +57,47 @@ mvn appengine:run ### Deploy to Google Cloud -First, update `src/main/webapp/WEB-INF/appengine-web.xml` with the correct values to pass the +First, update `src/main/webapp/WEB-INF/appengine-web.xml` with the correct values to pass the environment variables into the runtime. Next, the following command will deploy the application to your Google Cloud project: ```bash mvn appengine:deploy ``` + +### Deploy to Cloud Run + +See the [Cloud Run documentation](https://cloud.google.com/run/docs/configuring/connect-cloudsql) +for more details on connecting a Cloud Run service to Cloud SQL. + +1. Build the container image: + + ```sh + gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-mysql + ``` + +2. Deploy the service to Cloud Run: + + ```sh + gcloud run deploy run-mysql --image gcr.io/[YOUR_PROJECT_ID]/run-mysql + ``` + + Take note of the URL output at the end of the deployment process. + +3. Configure the service for use with Cloud Run + + ```sh + gcloud run services update run-mysql \ + --add-cloudsql-instances [INSTANCE_CONNECTION_NAME] \ + --set-env-vars CLOUD_SQL_CONNECTION_NAME=[INSTANCE_CONNECTION_NAME],\ + DB_USER=[MY_DB_USER],DB_PASS=[MY_DB_PASS],DB_NAME=[MY_DB] + ``` + Replace environment variables with the correct values for your Cloud SQL + instance configuration. + + This step can be done as part of deployment but is separated for clarity. + +4. Navigate your browser to the URL noted in step 2. + + For more details about using Cloud Run see http://cloud.run. + Review other [Java on Cloud Run samples](../../../run/). diff --git a/cloud-sql/mysql/servlet/pom.xml b/cloud-sql/mysql/servlet/pom.xml index 99ea7c7a80b..286c625d501 100644 --- a/cloud-sql/mysql/servlet/pom.xml +++ b/cloud-sql/mysql/servlet/pom.xml @@ -64,6 +64,16 @@ HikariCP 3.4.1 + + org.slf4j + slf4j-api + 1.7.5 + + + org.slf4j + slf4j-simple + 1.6.4 + @@ -71,7 +81,7 @@ org.eclipse.jetty jetty-maven-plugin - 9.4.10.v20180503 + 9.4.22.v20191022 1 diff --git a/cloud-sql/mysql/servlet/src/main/webapp/WEB-INF/appengine-web.xml b/cloud-sql/mysql/servlet/src/main/webapp/WEB-INF/appengine-web.xml index 5ce3ec0199f..0a49b0e5609 100644 --- a/cloud-sql/mysql/servlet/src/main/webapp/WEB-INF/appengine-web.xml +++ b/cloud-sql/mysql/servlet/src/main/webapp/WEB-INF/appengine-web.xml @@ -18,7 +18,7 @@ true java8 - + diff --git a/run/README.md b/run/README.md index 9cc7b0e7c5b..a660e20c1f0 100644 --- a/run/README.md +++ b/run/README.md @@ -17,6 +17,7 @@ This directory contains samples for [Google Cloud Run](https://cloud.run). [Clou |[Image Magick](image-processing/) | Event-driven image analysis & transformation | [Run on Google Cloud][run_button_image] | |[Manual Logging](logging-manual/) | Structured logging for Stackdriver | [Run on Google Cloud][run_button_log] | |[Local Troubleshooting](hello-broken/) | Broken services for local troubleshooting tutorial | [Run on Google Cloud][run_button_broken] | +|[Cloud SQL (MySQL)][mysql] | Use MySQL with Cloud Run | [Run on Google Cloud][run_button_sql] | For more Cloud Run samples beyond Java, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples). @@ -140,3 +141,5 @@ gcloud beta run deploy $SAMPLE \ [jib-tutorial]: https://github.com/GoogleContainerTools/jib/tree/master/examples/spring-boot [startup]: https://cwiki.apache.org/confluence/display/TOMCAT/HowTo+FasterStartUp [testing]: https://cloud.google.com/run/docs/testing/local#running_locally_using_docker_with_access_to_services +[mysql]: ../cloud-sql/mysql/servlet +[run_button_sql]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=cloud-sql/mysql/servlet