Skip to content

Commit 48e53b2

Browse files
authored
cloud-sql: add Cloud Run support to sqlalchemy sample (GoogleCloudPlatform#2449)
1 parent 78c213a commit 48e53b2

File tree

6 files changed

+85
-3
lines changed

6 files changed

+85
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Dockerfile
2+
.dockerignore
3+
__pycache__
4+
.pytest_cache

cloud-sql/mysql/sqlalchemy/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__pycache__
2+
.pytest_cache

cloud-sql/mysql/sqlalchemy/Dockerfile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
# http://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 Python image.
16+
# https://hub.docker.com/_/python
17+
FROM python:3.7
18+
19+
# Copy application dependency manifests to the container image.
20+
# Copying this separately prevents re-running pip install on every code change.
21+
COPY requirements.txt ./
22+
23+
# Install production dependencies.
24+
RUN set -ex; \
25+
pip install -r requirements.txt; \
26+
pip install gunicorn
27+
28+
# Copy local code to the container image.
29+
ENV APP_HOME /app
30+
WORKDIR $APP_HOME
31+
COPY . ./
32+
33+
# Run the web service on container startup. Here we use the gunicorn
34+
# webserver, with one worker process and 8 threads.
35+
# For environments with multiple CPU cores, increase the number of workers
36+
# to be equal to the cores available.
37+
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app

cloud-sql/mysql/sqlalchemy/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,40 @@ Next, the following command will deploy the application to your Google Cloud pro
7575
```bash
7676
gcloud app deploy
7777
```
78+
79+
## Deploy to Cloud Run
80+
81+
See the [Cloud Run documentation](https://cloud.google.com/run/docs/configuring/connect-cloudsql)
82+
for more details on connecting a Cloud Run service to Cloud SQL.
83+
84+
1. Build the container image:
85+
86+
```sh
87+
gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/run-mysql
88+
```
89+
90+
2. Deploy the service to Cloud Run:
91+
92+
```sh
93+
gcloud beta run deploy run-mysql --image gcr.io/[YOUR_PROJECT_ID]/run-mysql
94+
```
95+
96+
Take note of the URL output at the end of the deployment process.
97+
98+
3. Configure the service for use with Cloud Run
99+
100+
```sh
101+
gcloud beta run services update run-mysql \
102+
--add-cloudsql-instances [INSTANCE_CONNECTION_NAME] \
103+
--set-env-vars CLOUD_SQL_CONNECTION_NAME=[INSTANCE_CONNECTION_NAME],\
104+
DB_USER=[MY_DB_USER],DB_PASS=[MY_DB_PASS],DB_NAME=[MY_DB]
105+
```
106+
Replace environment variables with the correct values for your Cloud SQL
107+
instance configuration.
108+
109+
This step can be done as part of deployment but is separated for clarity.
110+
111+
4. Navigate your browser to the URL noted in step 2.
112+
113+
For more details about using Cloud Run see http://cloud.run.
114+
Review other [Python on Cloud Run samples](../../../run/).

run/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This directory contains samples for [Google Cloud Run](https://cloud.run). [Clou
1313
| ------------------------------- | ------------------------ | ------------- |
1414
|[Hello World][helloworld]&nbsp;&#10149; | Quickstart | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_helloworld] |
1515
|[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+
|[Cloud SQL (MySQL)[mysql] | Use MySQL with Cloud Run | - |
1617

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

@@ -105,6 +106,7 @@ for more information.
105106
[run_deploy]: https://cloud.google.com/run/docs/deploying
106107
[helloworld]: https://github.com/knative/docs/tree/master/docs/serving/samples/hello-world/helloworld-python
107108
[pubsub]: pubsub/
109+
[mysql]: ../cloud-sql/mysql/sqlalchemy
108110
[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
109111
[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
110112
[testing]: https://cloud.google.com/run/docs/testing/local#running_locally_using_docker_with_access_to_services

run/pubsub/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ RUN pip install -r requirements.txt
2828
# Copy local code to the container image.
2929
ENV APP_HOME /app
3030
WORKDIR $APP_HOME
31-
COPY . .
31+
COPY . ./
3232

33-
# Run the web service on container startup. Here we use the gunicorn
34-
# webserver, with one worker process and 8 threads.
33+
# Run the web service on container startup.
34+
# Use gunicorn webserver with one worker process and 8 threads.
3535
# For environments with multiple CPU cores, increase the number of workers
3636
# to be equal to the cores available.
3737
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app

0 commit comments

Comments
 (0)