Skip to content

Commit d18cc2e

Browse files
authored
Merge pull request #235 from fnproject/marleung/update-doc
Update tutorial and example
2 parents 4247b96 + 669111e commit d18cc2e

13 files changed

Lines changed: 123 additions & 71 deletions

File tree

ContainerAsFunction/README.md

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,19 @@ images are as small as possible--which is beneficial for a number of reasons.
171171

172172
## Custom Node.js Function Dockerfile
173173

174-
The `fnproject/node` container image is built on Alpine so we'll need to install
174+
The `fnproject/node` container image is built on OracleLinux 9 so we'll need to install
175175
the
176-
[ImageMagick Alpine package](https://pkgs.alpinelinux.org/packages?name=imagemagick&branch=edge)
177-
using the `apk` package management utility. You can do this with a Dockerfile
176+
[ImageMagick package in OracleLinux9 EPEL repository](https://yum.oracle.com/repo/OracleLinux/OL9/developer/EPEL/x86_64/index.html)
177+
using the `microdnf` package management utility. You can do this with a Dockerfile
178178
`RUN` command:
179179

180180
```Dockerfile
181-
RUN apk add --no-cache imagemagick
181+
RUN microdnf update && \
182+
microdnf install -y oracle-epel-release-el9 && \
183+
microdnf install -y yum-utils && \
184+
yum-config-manager --enable ol9_developer_EPEL && \
185+
microdnf install -y ImageMagick && \
186+
microdnf clean all
182187
```
183188

184189
We want to install ImageMagick into the runtime image, not the build image,
@@ -189,17 +194,23 @@ so we need to add the `RUN` command after the `FROM fnproject/node` command.
189194
`Dockerfile` and copy/paste the following as its content:
190195

191196
```Dockerfile
192-
FROM fnproject/node:dev as build-stage
197+
FROM fnproject/node:22-dev as build-stage
193198
WORKDIR /function
194199
ADD package.json /function/
195200
RUN npm install
196201
197-
FROM fnproject/node
198-
RUN apk add --no-cache imagemagick
202+
FROM fnproject/node:22
203+
RUN microdnf update && \
204+
microdnf install -y oracle-epel-release-el9 && \
205+
microdnf install -y yum-utils && \
206+
yum-config-manager --enable ol9_developer_EPEL && \
207+
microdnf install -y ImageMagick && \
208+
microdnf clean all
199209
WORKDIR /function
200210
ADD . /function/
201211
COPY --from=build-stage /function/node_modules/ /function/node_modules/
202212
ENTRYPOINT ["node", "func.js"]
213+
203214
```
204215

205216
With this Dockerfile, the Node.js function, it's dependencies
@@ -220,20 +231,18 @@ your function. Give it a try:
220231
You should see output similar to:
221232

222233
```shell
223-
Building image imagedims:0.0.1
224-
Current Context: default
225-
Sending build context to Docker daemon 27.65kB
226-
Step 1/10 : FROM fnproject/node:dev as build-stage
234+
Building image node-imagemagick:0.0.1
235+
Dockerfile content
236+
-----------------------------------
237+
FROM fnproject/node:22-dev as build-stage
227238
---> 016382f39a51
228239
...
229-
Step 6/10 : RUN apk add --no-cache imagemagick
230-
---> Using cache
231-
---> 5c4a1e19767c
240+
[2/2] STEP 2/6: RUN microdnf update && microdnf install -y oracle-epel-release-el9 &&
232241
...
233-
Successfully built 8f199b0bef00
234-
Successfully tagged imagedims:0.0.1
242+
Successfully tagged localhost/node-imagemagick:0.0.1
243+
1b78040d2468be947d08db185a684a756e2b329a769ee07f35f4e93e60cd2d22
235244
236-
Function imagedims:0.0.1 built successfully.
245+
Function node-imagemagick:0.0.1 built successfully.
237246
```
238247

239248
Just like with a default build, the output is a container image. From this

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Each of the tutorials below provides a step by step examination and walkthrough of a specific Fn feature or component. Check back soon as new tutorials are being added regularly.
44

55
## Introduction to Fn
6-
Before deploying your first function, you need to [install the Fn cli and start an Fn Server](install/README.md).
6+
Before deploying your first function, you need to [install the Fn CLI and start an Fn Server](install/README.md).
77

88
Now that the Fn Server is up and running, you can deploy your first function. Select your preferred language:
99

grafana/README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,23 @@ Check the output of the following docker command. This is used in subsequent doc
9494
9595
<div>
9696
<blockquote>
97-
<code>docker network inspect bridge -f '&#123;&#123;range .IPAM.Config&#125;&#125;&#123;&#123;.Gateway&#125;&#125;&#123;&#123;end&#125;&#125;'</code>
97+
<code>docker network ls</code>
9898
</blockquote>
9999
</div>
100100

101+
For Podman
102+
<div>
103+
<blockquote>
104+
<code>docker network inspect &lt;network name&gt; -f "&#123;&#123;range .Subnets&#125;&#125;&#123;&#123;.Gateway&#125;&#125;&#123;&#123;end&#125;&#125;"</code>
105+
</blockquote>
106+
</div>
107+
108+
For Docker
109+
<div>
110+
<blockquote>
111+
<code>docker network inspect &lt;network name&gt; -f "&#123;&#123;range .IPAM.Config&#125;&#125;&#123;&#123;.Gateway&#125;&#125;&#123;&#123;end&#125;&#125;"</code>
112+
</blockquote>
113+
</div>
101114
<!-- Original command
102115
docker network inspect bridge -f '{{range .IPAM.Config}}{{.Gateway}}{{end}}'
103116
-->
@@ -116,7 +129,7 @@ Now start Prometheus, specifying the above config file.
116129
<blockquote>
117130
<code>docker run --rm --name=prometheus -d -p 9090:9090
118131
-v &#96;pwd&#96;/prometheus.yml:/etc/prometheus/prometheus.yml
119-
--add-host=&quot;fnserver:&#96;docker network inspect bridge -f &apos;&#123;&#123;range .IPAM.Config&#125;&#125;&#123;&#123;.Gateway&#125;&#125;&#123;&#123;end&#125;&#125;&apos;&#96;&quot; prom/prometheus</code>
132+
--add-host=&quot;fnserver:&lt;gateway IP&gt;&quot; prom/prometheus</code>
120133
</blockquote>
121134
</div>
122135

@@ -151,7 +164,7 @@ From the same terminal window and the same directory as above, start Grafana on
151164
<div>
152165
<blockquote>
153166
<code>docker run --name=grafana -d -p 5000:3000
154-
--add-host=&quot;prometheus:&#96;docker network inspect bridge -f &apos;&#123;&#123;range .IPAM.Config&#125;&#125;&#123;&#123;.Gateway&#125;&#125;&#123;&#123;end&#125;&#125;&apos;&#96;&quot; grafana/grafana</code>
167+
--add-host=&quot;prometheus:&lt;gateway IP&gt;&quot; grafana/grafana</code>
155168
</blockquote>
156169
</div>
157170

@@ -181,8 +194,7 @@ Create a datasource to obtain metrics from Prometheus:
181194
>In the form that opens:
182195
>* Set **Name** to `PromDS`
183196
>* Set **Type** to `Prometheus`
184-
>* Set **URL** to `http://prometheus:9090`
185-
>* Set **Access** to `proxy`
197+
>* Set **URL** to `http://<your host IP address>:9090`
186198
>* Click **Add**
187199
>* Click **Save and test**
188200
@@ -275,7 +287,7 @@ To view the available `Fn docker stats` metrics:
275287
276288
>![user input](../images/userinput.png)
277289
>```shell
278-
>curl --silent http://localhost:8080/metrics | grep 'Metric fn_docker_stats'
290+
>curl --silent http://localhost:8080/metrics | grep 'fn_docker_stats'
279291
>```
280292
281293
And you will see the following metrics:

grafana/myfunc/func.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
package main
22

33
import (
4-
"fmt"
5-
"time"
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"log"
9+
"time"
10+
11+
fdk "github.com/fnproject/fdk-go"
612
)
713

814
func main() {
9-
fmt.Println("Before sleep for 500 ms")
10-
time.Sleep(time.Duration(500) * time.Millisecond)
11-
fmt.Println("After sleep for 500 ms")
15+
fdk.Handle(fdk.HandlerFunc(myHandler))
16+
}
17+
18+
type Person struct {
19+
Name string `json:"name"`
20+
}
21+
22+
func myHandler(ctx context.Context, in io.Reader, out io.Writer) {
23+
p := &Person{Name: "World"}
24+
json.NewDecoder(in).Decode(p)
25+
msg := struct {
26+
Msg string `json:"message"`
27+
}{
28+
Msg: fmt.Sprintf("Hello %s", p.Name),
29+
}
30+
log.Print("Inside Go Hello World function")
31+
32+
fmt.Println("Before sleep for 500 ms")
33+
time.Sleep(time.Duration(500) * time.Millisecond)
34+
fmt.Println("After sleep for 500 ms")
35+
json.NewEncoder(out).Encode(&msg)
1236
}

grafana/myfunc/func.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
schema_version: 20180708
12
name: myfunc
2-
version: 0.0.1
3+
version: 0.0.7
34
runtime: go
5+
build_image: fnproject/go:1.24-dev
6+
run_image: fnproject/go:1.24
47
entrypoint: ./func
8+
triggers:
9+
- name: myfunc
10+
type: http
11+
source: /myfunc

grafana/myfunc/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
module func
3+
4+
require github.com/fnproject/fdk-go v0.0.66

grafana/myfunc/run.bash

100644100755
File mode changed.

install/README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ Setting up a working Fn installation involves these three simple steps:
1313

1414
Before we can install Fn you'll need:
1515

16-
1. A computer running Linux or MacOS. If you have a Windows machine the
17-
easiest thing to do is install [VirtualBox](https://www.virtualbox.org/)
18-
and run a free Linux virtual machine.
19-
2. [Docker](https://www.docker.com/) 17.10 (or higher) needs to be
16+
1. A computer running Linux, MacOS or Windows.
17+
2. [Docker](https://www.docker.com/) 17.10 (or higher), Podman (5.7.0 or higher) or Rancher Desktop (1.22.0 or higher) needs to be
2018
installed and running.
21-
* **Note:** Version 0.6.12+ of the Fn CLI added Podman support. To use Podman to build Fn images, see: [Using Fn with Podman instead of Docker](https://github.com/fnproject/docs/blob/master/fn/develop/podman.md)
19+
* **Note:** For Fn to work with Podman, please check out: [Using Fn with Podman instead of Docker](https://github.com/fnproject/docs/blob/master/fn/develop/podman.md)
2220

2321
> As you make your way through this tutorial, look out for this icon.
2422
![](images/userinput.png) Whenever you see it, it's time for you to
@@ -38,12 +36,14 @@ Or, alternatively for Linux/Unix/MacOS from a terminal type the following:
3836
> curl -LSs https://raw.githubusercontent.com/fnproject/cli/master/install | sh
3937
>```
4038
39+
For Windows, you could download the binary from the release page: https://github.com/fnproject/cli/releases
40+
4141
Once installed you'll see the Fn CLI version printed out. You should see
4242
something similar to the following displayed (although likely with a later
4343
version number):
4444
4545
```txt
46-
fn version 0.5.86
46+
fn version 0.6.48
4747
______
4848
/ ____/___
4949
/ /_ / __ \
@@ -145,8 +145,8 @@ You should see the version of the Fn CLI (client) and server displayed (your
145145
version will likely differ):
146146
147147
```txt
148-
Client version: 0.5.86
149-
Server version: 0.3.728
148+
Client version: 0.6.48
149+
Server version: 0.3.768
150150
```
151151
152152
**Note:**
@@ -320,10 +320,6 @@ You can confirm using the `which fn` command, the output should look like `/User
320320
(Alternatively, you can use `~/lbin/fn` if you don't want to add to your `PATH` environment variable.)
321321
322322
323-
#### For Windows Systems
324-
The best way for Windows users is to run Fn inside a Linux virtual machine either in the cloud or using [VirtualBox](https://www.virtualbox.org/) locally.
325-
326-
327323
## Learn More
328324
329325
Congratulations! You've installed Fn and started up an Fn server. Now you are

node/custom-db/README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ As a first step, let's create and run our initial Docker image.
3333
Here is the dockerfile for the application.
3434

3535
```txt
36-
FROM oraclelinux:7-slim
36+
FROM oraclelinux:9-slim
3737
38-
RUN yum -y install oracle-release-el7 oracle-nodejs-release-el7 && \
39-
yum-config-manager --disable ol7_developer_EPEL && \
40-
yum -y install oracle-instantclient19.3-basiclite nodejs && \
41-
rm -rf /var/cache/yum
38+
RUN microdnf update && \
39+
microdnf install -y oracle-instantclient-release-26ai-el9 && \
40+
microdnf install -y oracle-instantclient-basiclite nodejs && \
41+
microdnf clean all
4242
4343
WORKDIR /myapp
4444
ADD package.json /myapp/
@@ -65,7 +65,7 @@ The application is made of two parts. First, the `package.json` file defines any
6565
"myapp"
6666
],
6767
"dependencies": {
68-
"oracledb" : "^3.1"
68+
"oracledb" : "^6.10.0"
6969
},
7070
"author": "Fn Example",
7171
"license": "MIT"
@@ -100,7 +100,7 @@ We are now ready to build the Docker image.
100100
101101
(4) This returns the version of the Oracle DB driver:
102102
```txt
103-
3.1.2
103+
6.10.0
104104
```
105105
106106
That's it. You have a working Docker image.
@@ -114,12 +114,12 @@ Now let's convert that working Docker image into a function.
114114
The first step is some changes to the docker file.
115115

116116
```txt
117-
FROM oraclelinux:7-slim
117+
FROM oraclelinux:9-slim
118118
119-
RUN yum -y install oracle-release-el7 oracle-nodejs-release-el7 && \
120-
yum-config-manager --disable ol7_developer_EPEL && \
121-
yum -y install oracle-instantclient19.3-basiclite nodejs && \
122-
rm -rf /var/cache/yum
119+
RUN microdnf update && \
120+
microdnf install -y oracle-instantclient-release-26ai-el9 && \
121+
microdnf install -y oracle-instantclient-basiclite nodejs && \
122+
microdnf clean all
123123
124124
WORKDIR /function
125125
ADD . /function/
@@ -146,12 +146,12 @@ Next, we update the `package.json` file.
146146
```js
147147
{
148148
"name": "hellodb",
149-
"version": "1.0.0",
149+
"version": "1.0.0",
150150
"description": "Node DB Test function",
151151
"main": "func.js",
152152
"dependencies": {
153-
"@fnproject/fdk": ">=0.0.13",
154-
"oracledb" : "^3.1"
153+
"@fnproject/fdk": ">=0.0.88",
154+
"oracledb" : "^6.10.0"
155155
},
156156
"author": "Fn Example",
157157
"license": "Apache-2.0"
@@ -206,7 +206,7 @@ This deploys the function locally.
206206
This returns the version of the Oracle DB driver in JSON format:
207207

208208
```js
209-
{"version":"3.1.2"}
209+
{"version":"6.10.0"}
210210
```
211211

212212
That's it. You have converted a Node Docker image into a function.

node/custom-db/docker-only/dockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
FROM oraclelinux:7-slim
1+
FROM oraclelinux:9-slim
22

3-
RUN yum -y install oracle-release-el7 oracle-nodejs-release-el7 && \
4-
yum-config-manager --disable ol7_developer_EPEL && \
5-
yum -y install oracle-instantclient19.3-basiclite nodejs && \
6-
rm -rf /var/cache/yum
3+
RUN microdnf update && \
4+
microdnf install -y oracle-instantclient-release-26ai-el9 && \
5+
microdnf install -y oracle-instantclient-basiclite nodejs && \
6+
microdnf clean all
77

88
WORKDIR /myapp
99
ADD package.json /myapp/

0 commit comments

Comments
 (0)