Skip to content

Commit 3dabdb2

Browse files
authored
Add Dockerfile and instructions for Cloud Run and fix variable (#1745)
1 parent 5623943 commit 3dabdb2

File tree

5 files changed

+98
-13
lines changed

5 files changed

+98
-13
lines changed

cloud-sql/mysql/servlet/Dockerfile

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Use the official maven/Java 11 image to create a build artifact.
16+
# https://hub.docker.com/_/maven
17+
FROM maven:3.6.2-jdk-8-slim as builder
18+
19+
# Copy local code to the container image.
20+
WORKDIR /app
21+
COPY pom.xml .
22+
COPY src ./src
23+
24+
# Build a release artifact.
25+
RUN mvn package -DskipTests
26+
27+
# Use the Official Jetty image for a lean production stage of our multi-stage build.
28+
# https://hub.docker.com/_/jetty
29+
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
30+
FROM jetty:9.4-jre8
31+
32+
# Copy the exploded WAR directory from the builder stage to the Jetty web application directory.
33+
COPY --from=builder /app/target/tabs-vs-spaces-mysql-*/* $JETTY_BASE/webapps/ROOT/
34+
35+
# No CMD needed since Jetty automatically scans, loads, and starts the web app.

cloud-sql/mysql/servlet/README.md

+48-11
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
## Before you begin
44

5-
1. If you haven't already, set up a Java Development Environment (including google-cloud-sdk and
6-
maven utilities) by following the [java setup guide](https://cloud.google.com/java/docs/setup) and
5+
1. If you haven't already, set up a Java Development Environment (including google-cloud-sdk and
6+
maven utilities) by following the [java setup guide](https://cloud.google.com/java/docs/setup) and
77
[create a project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project).
88

9-
1. Create a 2nd Gen Cloud SQL Instance by following these
9+
1. Create a 2nd Gen Cloud SQL Instance by following these
1010
[instructions](https://cloud.google.com/sql/docs/mysql/create-instance). Note the connection string,
1111
database user, and database password that you create.
1212

13-
1. Create a database for your application by following these
13+
1. Create a database for your application by following these
1414
[instructions](https://cloud.google.com/sql/docs/mysql/create-manage-databases). Note the database
15-
name.
15+
name.
1616

17-
1. Create a service account with the 'Cloud SQL Client' permissions by following these
17+
1. Create a service account with the 'Cloud SQL Client' permissions by following these
1818
[instructions](https://cloud.google.com/sql/docs/mysql/connect-external-app#4_if_required_by_your_authentication_method_create_a_service_account).
19-
Download a JSON key to use to authenticate your connection.
19+
Download a JSON key to use to authenticate your connection.
2020

2121
1. Use the information noted in the previous steps:
2222
```bash
@@ -41,9 +41,9 @@ Navigate towards `http://127.0.0.1:8080` to verify your application is running c
4141

4242
## Google App Engine Standard
4343

44-
To run on GAE-Standard, create an AppEngine project by following the setup for these
45-
[instructions](https://cloud.google.com/appengine/docs/standard/java/quickstart#before-you-begin)
46-
and verify that
44+
To run on GAE-Standard, create an AppEngine project by following the setup for these
45+
[instructions](https://cloud.google.com/appengine/docs/standard/java/quickstart#before-you-begin)
46+
and verify that
4747
[appengine-maven-plugin](https://cloud.google.com/java/docs/setup#optional_install_maven_or_gradle_plugin_for_app_engine)
4848
has been added in your build section as a plugin.
4949

@@ -57,10 +57,47 @@ mvn appengine:run
5757

5858
### Deploy to Google Cloud
5959

60-
First, update `src/main/webapp/WEB-INF/appengine-web.xml` with the correct values to pass the
60+
First, update `src/main/webapp/WEB-INF/appengine-web.xml` with the correct values to pass the
6161
environment variables into the runtime.
6262

6363
Next, the following command will deploy the application to your Google Cloud project:
6464
```bash
6565
mvn appengine:deploy
6666
```
67+
68+
### Deploy to Cloud Run
69+
70+
See the [Cloud Run documentation](https://cloud.google.com/run/docs/configuring/connect-cloudsql)
71+
for more details on connecting a Cloud Run service to Cloud SQL.
72+
73+
1. Build the container image:
74+
75+
```sh
76+
gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-mysql
77+
```
78+
79+
2. Deploy the service to Cloud Run:
80+
81+
```sh
82+
gcloud run deploy run-mysql --image gcr.io/[YOUR_PROJECT_ID]/run-mysql
83+
```
84+
85+
Take note of the URL output at the end of the deployment process.
86+
87+
3. Configure the service for use with Cloud Run
88+
89+
```sh
90+
gcloud run services update run-mysql \
91+
--add-cloudsql-instances [INSTANCE_CONNECTION_NAME] \
92+
--set-env-vars CLOUD_SQL_CONNECTION_NAME=[INSTANCE_CONNECTION_NAME],\
93+
DB_USER=[MY_DB_USER],DB_PASS=[MY_DB_PASS],DB_NAME=[MY_DB]
94+
```
95+
Replace environment variables with the correct values for your Cloud SQL
96+
instance configuration.
97+
98+
This step can be done as part of deployment but is separated for clarity.
99+
100+
4. Navigate your browser to the URL noted in step 2.
101+
102+
For more details about using Cloud Run see http://cloud.run.
103+
Review other [Java on Cloud Run samples](../../../run/).

cloud-sql/mysql/servlet/pom.xml

+11-1
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,24 @@
6464
<artifactId>HikariCP</artifactId>
6565
<version>3.4.1</version>
6666
</dependency>
67+
<dependency>
68+
<groupId>org.slf4j</groupId>
69+
<artifactId>slf4j-api</artifactId>
70+
<version>1.7.5</version>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.slf4j</groupId>
74+
<artifactId>slf4j-simple</artifactId>
75+
<version>1.6.4</version>
76+
</dependency>
6777
</dependencies>
6878

6979
<build>
7080
<plugins>
7181
<plugin>
7282
<groupId>org.eclipse.jetty</groupId>
7383
<artifactId>jetty-maven-plugin</artifactId>
74-
<version>9.4.10.v20180503</version>
84+
<version>9.4.22.v20191022</version>
7585
<configuration>
7686
<scanIntervalSeconds>1</scanIntervalSeconds>
7787
</configuration>

cloud-sql/mysql/servlet/src/main/webapp/WEB-INF/appengine-web.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<threadsafe>true</threadsafe>
1919
<runtime>java8</runtime>
2020
<env-variables>
21-
<env-var name="CLOUD_SQL_INSTANCE_NAME" value="my-project:region:instance" />
21+
<env-var name="CLOUD_SQL_CONNECTION_NAME" value="my-project:region:instance" />
2222
<env-var name="DB_USER" value="my-db-user" />
2323
<env-var name="DB_PASS" value="my-db-password" />
2424
<env-var name="DB_NAME" value="my_db" />

run/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This directory contains samples for [Google Cloud Run](https://cloud.run). [Clou
1717
|[Image Magick](image-processing/) | Event-driven image analysis & transformation | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_image] |
1818
|[Manual Logging](logging-manual/) | Structured logging for Stackdriver | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_log] |
1919
|[Local Troubleshooting](hello-broken/) | Broken services for local troubleshooting tutorial | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_broken] |
20+
|[Cloud SQL (MySQL)][mysql] | Use MySQL with Cloud Run | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_sql] |
2021

2122
For more Cloud Run samples beyond Java, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples).
2223

@@ -140,3 +141,5 @@ gcloud beta run deploy $SAMPLE \
140141
[jib-tutorial]: https://github.com/GoogleContainerTools/jib/tree/master/examples/spring-boot
141142
[startup]: https://cwiki.apache.org/confluence/display/TOMCAT/HowTo+FasterStartUp
142143
[testing]: https://cloud.google.com/run/docs/testing/local#running_locally_using_docker_with_access_to_services
144+
[mysql]: ../cloud-sql/mysql/servlet
145+
[run_button_sql]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=cloud-sql/mysql/servlet

0 commit comments

Comments
 (0)