Skip to content

Commit

Permalink
Add build options examples
Browse files Browse the repository at this point in the history
This adds an example of how to practically use `build-option`
with pillow for Python

Signed-off-by: Ivana Yovcheva (VMware) <[email protected]>
  • Loading branch information
ivanayov committed Jul 17, 2018
1 parent 59c6f47 commit 157c93a
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion docs/cli/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
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 <your-docker-namespace>
```
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

0 comments on commit 157c93a

Please sign in to comment.