From 6f6a0dcbab50d24414a1a4060a9e6af9801c6185 Mon Sep 17 00:00:00 2001 From: "Ivana Yovcheva (VMware)" Date: Tue, 10 Jul 2018 19:20:34 +0300 Subject: [PATCH] Add build options examples This adds an example of how to practically use `build-option` with pillow for Python Signed-off-by: Ivana Yovcheva (VMware) --- docs/cli/build.md | 62 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/docs/cli/build.md b/docs/cli/build.md index d6c7838a..9c1fb108 100644 --- a/docs/cli/build.md +++ b/docs/cli/build.md @@ -70,6 +70,7 @@ ARG ADDITIONAL_PACKAGE RUN apk --no-cache add curl ${ADDITIONAL_PACKAGE} \ ``` +<<<<<<< HEAD ## 2.0 Pass ADDITIONAL_PACKAGE through `--build-arg` There may be scenarios where a single native module need to be added to a build. A single-package build option could be added as described above. Alternatively a package could be specified through a `--build-arg`. @@ -101,4 +102,63 @@ Remeber to add any `ARG` values to the template's Dockerfile: ARG ARGNAME2 ``` - For more information about passing build arguments to Docker, please visit the [Docker documentation](https://docs.docker.com/engine/reference/commandline/build/) \ No newline at end of file + For more information about passing build arguments to Docker, please visit the [Docker documentation](https://docs.docker.com/engine/reference/commandline/build/) +======= + +## 2.1 Build options examples + +Let's see some practical examples with the `build-option` flag. + +* Use [Pillow](https://pillow.readthedocs.io/en/5.2.x/) for image processing in your Python function + +Create function with + +``` +$ faas-cli new faas_black_and_white --lang python3 --prefix +``` + +Add `pillow` to `requirements.txt` . + +Copy some resource images to `faas_black_and_white/`. + +Edit `handler.py`: + +```python +import os +from PIL import Image + +def handle(req): + img = Image.open("function/" + req) + + blackAndWhite = img.convert('1') + + blackAndWhite.save("function/blackAndWhite.jpeg") + + with open("function/blackAndWhite.jpeg", 'rb') as pic: + res = pic.read() + + os.write(1, res) + return res +``` + +What the code does is to open an image, available in resources and convert it to black and white. + +Now build the function with build options: +``` +faas build -f faas_black_and_white.yml --build-option dev --build-option pillow +``` + +Push and deploy: +``` +faas push -f faas_black_and_white.yml && faas deploy -f faas_black_and_white.yml +``` + +Test the function with: + +```bash +echo "image-name.jpg" | faas invoke pillow-func > blackAndWhite.jpg +``` + +Or in your web browser by opening http://127.0.0.1:8080/function/pillow-func + +>>>>>>> Add build options examples