Skip to content

Commit 6f6a0dc

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 6f6a0dc

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

docs/cli/build.md

+61-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ ARG ADDITIONAL_PACKAGE
7070
RUN apk --no-cache add curl ${ADDITIONAL_PACKAGE} \
7171

7272
```
73+
<<<<<<< HEAD
7374
## 2.0 Pass ADDITIONAL_PACKAGE through `--build-arg`
7475

7576
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:
101102
ARG ARGNAME2
102103
```
103104

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

0 commit comments

Comments
 (0)