Skip to content

Commit df470fe

Browse files
committed
Add conversion example
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
1 parent 9c8e91b commit df470fe

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

docs/reference/workloads.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,64 @@ faas-cli new --list
4444

4545
Custom binaries can also be used as a function. Just use the `dockerfile` language template and replace the `fprocess` variable with the command you want to run per request. If you would like to pipe arguments to a CLI utility you can prefix the command with `xargs`.
4646

47+
### Running an existing Docker image on OpenFaaS
48+
49+
Let's take a Node.js app which listens on traffic using port 3000, and assume that we can't make any changes to it.
50+
51+
You can view its Dockerfile and code at: [alexellis/expressjs-k8s](https://github.com/alexellis/expressjs-k8s/) and the image is published to the Docker Hub at: `alexellis2/service:0.3.6`
52+
53+
Start by creating a new folder:
54+
55+
```bash
56+
mkdir -p node-service/
57+
```
58+
59+
Write a custom Dockerfile `./node-service/Dockerfile`:
60+
61+
```Dockerfile
62+
# Import the OpenFaaS of-watchdog
63+
FROM openfaas/of-watchdog:0.7.2 as watchdog
64+
65+
# Add a FROM line from your existing image
66+
FROM alexellis2/service:0.3.6
67+
68+
# Let's say that the image listens on port 3000 and
69+
# that we can't change that easily
70+
ENV http_port 3000
71+
72+
# Install the watchdog from the base image
73+
COPY --from=watchdog /fwatchdog /usr/bin/
74+
75+
# Now set the watchdog as the start-up process
76+
# Along with the HTTP mode, and an upstream URL to
77+
# where your HTTP server will be running from the original
78+
# image.
79+
ENV mode="http"
80+
ENV upstream_url="http://127.0.0.1:3000"
81+
82+
# Set fprocess to the value you have in your base image
83+
ENV fprocess="node index.js"
84+
CMD ["fwatchdog"]
85+
```
86+
87+
Now create a stack.yml at the root directory `./stack.yml`:
88+
89+
```yaml
90+
provider:
91+
name: openfaas
92+
functions:
93+
node-service:
94+
handler: ./node-service
95+
image: docker.io/alexellis2/node-service-watchdog:0.1.0
96+
lang: dockerfile
97+
```
98+
99+
Now run `faas-cli up`
100+
101+
Your code will now listen on port 8080 and fulfil the serverless definition including automatic health-checks and a graceful shutdown.
102+
103+
You can then access the service at: `http://127.0.0.1:8080/function/node-service`
104+
47105
### Custom service account
48106

49107
When using Kubernetes, OpenFaaS workloads can assume a ServiceAccount in the namespace in which they are deployed.

0 commit comments

Comments
 (0)