8000 docs: Add docker-compose example (#143) · jinjunnn/google-functions-python@0205c85 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0205c85

Browse files
authored
docs: Add docker-compose example (GoogleCloudPlatform#143)
1 parent 9ea8dd2 commit 0205c85

File tree

6 files changed

+118
-1
lines changed

6 files changed

+118
-1
lines changed

examples/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Python Functions Frameworks Examples
22

3+
## Deployment targets
4+
### Cloud Run
35
* [`cloud_run_http`](./cloud_run_http/) - Deploying an HTTP function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
46
* [`cloud_run_event`](./cloud_run_event/) - Deploying a CloudEvent function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
5-
* [`cloud_run_cloudevents`](./cloud_run_cloudevents/) - Deploying a [CloudEvent](https://github.com/cloudevents/sdk-python) function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
7+
* [`cloud_run_cloudevents`](./cloud_run_cloudevents/) - Deploying a [CloudEvent](https://github.com/cloudevents/sdk-python) function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
8+
9+
## Development Tools
10+
* [`docker-compose`](./docker-compose) -
11+
* [`skaffold`](./skaffold) - Developing multiple functions on the same host using Minikube and Skaffold

examples/docker-compose/Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Use the Python base image
2+
FROM python
3+
4+
# Set a working directory
5+
WORKDIR /func
6+
7+
# Copy all the files from the local directory into the container
8+
COPY . .
9+
10+
# Install the Functions Framework
11+
RUN pip install functions-framework
12+
13+
# Install any dependencies of the function
14+
RUN pip install -r requirements.txt
15+
16+
# Run the function
17+
CMD ["functions-framework", "--target=hello", "--debug"]

examples/docker-compose/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Developing functions with Docker Compose
2+
3+
## Introduction
4+
5+
This examples shows you how to develop a Cloud Function locally with Docker Compose, including live reloading.
6+
7+
## Install `docker-compose`:
8+
https://docs.docker.com/compose/install/
9+
10+
## Start the `docker-compose` environment:
11+
12+
In this directory, bring up the `docker-compose` environment with:
13+
```
14+
docker-compose up
15+
```
16+
17+
You should see output similar to:
18+
19+
```
20+
Building function
21+
[+] Building 7.0s (10/10) FINISHED
22+
=> [internal] load build definition from Dockerfile 0.0s
23+
=> => transferring dockerfile: 431B 0.0s
24+
=> [internal] load .dockerignore 0.0s
25+
=> => transferring context: 2B 0.0s
26+
=> [internal] load metadata for docker.io/library/python:latest 0.6s
27+
=> [1/5] FROM docker.io/library/python@sha256:7a93befe45f3afb6b337 0.0s
28+
=> [internal] load build context 0.0s
29+
=> => transferring context: 2.11kB 0.0s
30+
=> CACHED [2/5] WORKDIR /func 0.0s
31+
=> [3/5] COPY . . 0.0s
32+
=> [4/5] RUN pip install functions-framework 4.7s
33+
=> [5/5] RUN pip install -r requirements.txt 1.1s
34+
=> exporting to image 0.4s
35+
=> => exporting layers 0.4s
36+
=> => writing image sha256:99962e5907e80856af6b032aa96a3130dde9ab6 0.0s
37+
=> => naming to docker.io/library/docker-compose_function 0.0s
38+
39+
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
40+
Recreating docker-compose_function_1 ... done
41+
Attaching to docker-compose_function_1
42+
function_1 | * Serving Flask app 'hello' (lazy loading)
43+
function_1 | * Environment: production
44+
function_1 | WARNING: This is a development server. Do not use it in a production deployment.
45+
function_1 | Use a production WSGI server instead.
46+
function_1 | * Debug mode: on
47+
function_1 | * Running on all addresses.
48+
function_1 | WARNING: This is a development server. Do not use it in a production deployment.
49+
function_1 | * Running on http://172.21.0.2:8080/ (Press CTRL+C to quit)
50+
function_1 | * Restarting with watchdog (inotify)
51+
function_1 | * Debugger is active!
52+
```
53+
function_1 | * Debugger PIN: 162-882-413
54+
55+
## Call your Cloud Function
56+
57+
Leaving the previous command running, in a **new terminal**, call your functions. To call the `hello` function:
58+
59+
```bash
60+
curl localhost:8080/hello
61+
```
62+
63+
You should see output similar to:
64+
65+
```terminal
66+
Hello, World!
67+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: "3.9"
2+
services:
3+
function:
4+
build: .
5+
ports:
6+
- "8080:8080"
7+
volumes:
8+
- .:/func

examples/docker-compose/main.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2021 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+
16+
def hello(request):
17+
"""Return a friendly HTTP greeting."""
18+
return "Hello, World!!!"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Add any Python requirements here

0 commit comments

Comments
 (0)
0