Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Python Functions Frameworks Examples

## Deployment targets
### Cloud Run
* [`cloud_run_http`](./cloud_run_http/) - Deploying an HTTP function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
* [`cloud_run_event`](./cloud_run_event/) - Deploying a CloudEvent function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
* [`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
* [`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

## Development Tools
* [`docker-compose`](./docker-compose) -
* [`skaffold`](./skaffold) - Developing multiple functions on the same host using Minikube and Skaffold
17 changes: 17 additions & 0 deletions examples/docker-compose/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Use the Python base image
FROM python

# Set a working directory
WORKDIR /func

# Copy all the files from the local directory into the container
COPY . .

# Install the Functions Framework
RUN pip install functions-framework

# Install any dependencies of the function
RUN pip install -r requirements.txt

# Run the function
CMD ["functions-framework", "--target=hello", "--debug"]
67 changes: 67 additions & 0 deletions examples/docker-compose/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Developing functions with Docker Compose

## Introduction

This examples shows you how to develop a Cloud Function locally with Docker Compose, including live reloading.

## Install `docker-compose`:
https://docs.docker.com/compose/install/

## Start the `docker-compose` environment:

In this directory, bring up the `docker-compose` environment with:
```
docker-compose up
```

You should see output similar to:

```
Building function
[+] Building 7.0s (10/10) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 431B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/python:latest 0.6s
=> [1/5] FROM docker.io/library/python@sha256:7a93befe45f3afb6b337 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 2.11kB 0.0s
=> CACHED [2/5] WORKDIR /func 0.0s
=> [3/5] COPY . . 0.0s
=> [4/5] RUN pip install functions-framework 4.7s
=> [5/5] RUN pip install -r requirements.txt 1.1s
=> exporting to image 0.4s
=> => exporting layers 0.4s
=> => writing image sha256:99962e5907e80856af6b032aa96a3130dde9ab6 0.0s
=> => naming to docker.io/library/docker-compose_function 0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Recreating docker-compose_function_1 ... done
Attaching to docker-compose_function_1
function_1 | * Serving Flask app 'hello' (lazy loading)
function_1 | * Environment: production
function_1 | WARNING: This is a development server. Do not use it in a production deployment.
function_1 | Use a production WSGI server instead.
function_1 | * Debug mode: on
function_1 | * Running on all addresses.
function_1 | WARNING: This is a development server. Do not use it in a production deployment.
function_1 | * Running on http://172.21.0.2:8080/ (Press CTRL+C to quit)
function_1 | * Restarting with watchdog (inotify)
function_1 | * Debugger is active!
```
function_1 | * Debugger PIN: 162-882-413

## Call your Cloud Function

Leaving the previous command running, in a **new terminal**, call your functions. To call the `hello` function:

```bash
curl localhost:8080/hello
```

You should see output similar to:

```terminal
Hello, World!
```
8 changes: 8 additions & 0 deletions examples/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "3.9"
services:
function:
build: .
ports:
- "8080:8080"
volumes:
- .:/func
18 changes: 18 additions & 0 deletions examples/docker-compose/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2021 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
#
# http://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.


def hello(request):
"""Return a friendly HTTP greeting."""
return "Hello, World!!!"
1 change: 1 addition & 0 deletions examples/docker-compose/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Add any Python requirements here