diff --git a/docs/cli/build.md b/docs/cli/build.md index d6c7838a..c95b39ea 100644 --- a/docs/cli/build.md +++ b/docs/cli/build.md @@ -101,4 +101,61 @@ 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 +