From abf9a7a063a5ee83795008aab86f33a7fcbfe0ab Mon Sep 17 00:00:00 2001 From: Dustin Ingram Date: Mon, 9 Aug 2021 11:31:41 -0400 Subject: [PATCH] docs: Add docker-compose example --- examples/README.md | 8 ++- examples/docker-compose/Dockerfile | 17 ++++++ examples/docker-compose/README.md | 67 ++++++++++++++++++++++ examples/docker-compose/docker-compose.yml | 8 +++ examples/docker-compose/main.py | 18 ++++++ examples/docker-compose/requirements.txt | 1 + 6 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 examples/docker-compose/Dockerfile create mode 100644 examples/docker-compose/README.md create mode 100644 examples/docker-compose/docker-compose.yml create mode 100644 examples/docker-compose/main.py create mode 100644 examples/docker-compose/requirements.txt diff --git a/examples/README.md b/examples/README.md index 343e19c9..dfc93131 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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 \ No newline at end of file +* [`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 diff --git a/examples/docker-compose/Dockerfile b/examples/docker-compose/Dockerfile new file mode 100644 index 00000000..a7abbfbe --- /dev/null +++ b/examples/docker-compose/Dockerfile @@ -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"] diff --git a/examples/docker-compose/README.md b/examples/docker-compose/README.md new file mode 100644 index 00000000..68ce73b6 --- /dev/null +++ b/examples/docker-compose/README.md @@ -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! +``` diff --git a/examples/docker-compose/docker-compose.yml b/examples/docker-compose/docker-compose.yml new file mode 100644 index 00000000..b801ce17 --- /dev/null +++ b/examples/docker-compose/docker-compose.yml @@ -0,0 +1,8 @@ +version: "3.9" +services: + function: + build: . + ports: + - "8080:8080" + volumes: + - .:/func diff --git a/examples/docker-compose/main.py b/examples/docker-compose/main.py new file mode 100644 index 00000000..cf91e512 --- /dev/null +++ b/examples/docker-compose/main.py @@ -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!!!" diff --git a/examples/docker-compose/requirements.txt b/examples/docker-compose/requirements.txt new file mode 100644 index 00000000..3601409f --- /dev/null +++ b/examples/docker-compose/requirements.txt @@ -0,0 +1 @@ +# Add any Python requirements here