Skip to content

Commit 157c93a

Browse files
committed
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) <[email protected]>
1 parent 59c6f47 commit 157c93a

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

docs/cli/build.md

+58-1
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,61 @@ Remeber to add any `ARG` values to the template's Dockerfile:
101101
ARG ARGNAME2
102102
```
103103

104-
For more information about passing build arguments to Docker, please visit the [Docker documentation](https://docs.docker.com/engine/reference/commandline/build/)
104+
For more information about passing build arguments to Docker, please visit the [Docker documentation](https://docs.docker.com/engine/reference/commandline/build/)
105+
106+
## 2.1 Build options examples
107+
108+
Let's see some practical examples with the `build-option` flag.
109+
110+
* Use [Pillow](https://pillow.readthedocs.io/en/5.2.x/) for image processing in your Python function
111+
112+
Create function with
113+
114+
```
115+
$ faas-cli new faas_black_and_white --lang python3 --prefix <your-docker-namespace>
116+
```
117+
118+
Add `pillow` to `requirements.txt` .
119+
120+
Copy some resource images to `faas_black_and_white/`.
121+
122+
Edit `handler.py`:
123+
124+
```python
125+
import os
126+
from PIL import Image
127+
128+
def handle(req):
129+
img = Image.open("function/" + req)
130+
131+
blackAndWhite = img.convert('1')
132+
133+
blackAndWhite.save("function/blackAndWhite.jpeg")
134+
135+
with open("function/blackAndWhite.jpeg", 'rb') as pic:
136+
res = pic.read()
137+
138+
os.write(1, res)
139+
return res
140+
```
141+
142+
What the code does is to open an image, available in resources and convert it to black and white.
143+
144+
Now build the function with build options:
145+
```
146+
faas build -f faas_black_and_white.yml --build-option dev --build-option pillow
147+
```
148+
149+
Push and deploy:
150+
```
151+
faas push -f faas_black_and_white.yml && faas deploy -f faas_black_and_white.yml
152+
```
153+
154+
Test the function with:
155+
156+
```bash
157+
echo "image-name.jpg" | faas invoke pillow-func > blackAndWhite.jpg
158+
```
159+
160+
Or in your web browser by opening http://127.0.0.1:8080/function/pillow-func
161+

0 commit comments

Comments
 (0)