OpenWhisk Docker SDK

To install the Docker skeleton, normally we would do the following:

$ wsk -i sdk install docker

But if the file does not exist on your local OpenWhisk, you can download it directly from https://github.com/apache/incubator-openwhisk-runtime-docker/releases/download/sdk%400.1.0/blackbox-0.1.0.tar.gz.

The following steps are to download SDK, extract the SDK, change its directory from dockerSkeleton to docker_c, and change into the docker_c directory to check its contents:

$ curl -sSL -O https://github.com/apache/incubator-openwhisk-runtime-docker/releases/download/sdk%400.1.0/blackbox-0.1.0.tar.gz
$ tar xf blackbox-0.1.0.tar.gz
$ mv dockerSkeleton docker_c
$ cd docker_c
$ ls
buildAndPush.sh Dockerfile example.c README.md

The skeleton contains a Dockerfile, a simple C program, a bash script for building and pushing the finished function to Docker's Hub, and a README.md file.

We start with the content of the C program to see what it is for. The C program that came with the Docker skeleton SDK contains only the main function with a couple of printf statements:

#include <stdio.h>

int main(int argc, char *argv[]) {
printf("This is a log message from an arbitrary C program! ");
printf("{ "msg": "Hello from C program!", "args": %s }",
(argc == 1) ? "undefined" : argv[1]);
}

The last printf line tells us the whole story of OpenWhisk's action. This action returns JSON data by printing it out to STDOUT. The action accepts arguments, also in the form of JSON, through the main function's argv. It is the action's responsibility to decode the arguments and encode the output.

Next, we'll take a look at its Dockerfile.

The file starts by declaring openwhisk/dockerskeleton as the base image. In the next line, the environment variable FLASK_PROXY_PORT is defined as 8080. You may guess here that the framework used as the wrapper of every Docker function is Flask, a Python web framework.

Moving to the next two lines, they add the C program into the building container, install the GCC compiler, and then compile the program. The output binary is named exec. It must be placed at /action/exec. This is the mandatory location of the executable needed by OpenWhisk's actionproxy.

What is actionproxy? It is the OpenWhisk version of a function wrapping server. The server accepts a web request through its exposed port, 8080. As mentioned earlier, it is written in Python with the Flask framework, so every OpenWhisk function requires Python and Flask dependencies in order to start the actionproxy. This kind of setup is already there by inheriting from the base image, openwhisk/dockerskeleton:

# Dockerfile for example whisk docker action
FROM openwhisk/dockerskeleton

ENV FLASK_PROXY_PORT 8080

### Add source file(s)
ADD example.c /action/example.c

RUN apk add --no-cache --virtual .build-deps
bzip2-dev
gcc
libc-dev
### Compile source file(s)
&& cd /action; gcc -o exec example.c
&& apk del .build-deps

CMD ["/bin/bash", "-c", "cd actionProxy && python -u actionproxy.py"]

Instead of using the provided script, we will build it ourselves using the docker build command. Please recall that you need to use your own <DOCKER ID> as the repository name to allow you to push the built image onto Docker Hub:

$ docker build -t chanwit/whisk_c .

Sending build context to Docker daemon 6.656kB
Step 1/5 : FROM openwhisk/dockerskeleton
latest: Pulling from openwhisk/dockerskeleton
...
---> 25d1878c2f31
Step 2/5 : ENV FLASK_PROXY_PORT 8080
---> Running in 932e3e3d6c0b
---> 647789067bf0
Removing intermediate container 932e3e3d6c0b
Step 3/5 : ADD example.c /action/example.c
---> 91eb99956da2
Step 4/5 : RUN apk add --no-cache --virtual .build-deps bzip2-dev gcc
libc-dev && cd /action; gcc -o exec example.c && apk del .build-deps
---> Running in 943930981ac6
fetch http://dl-cdn.alpinelinux.org/alpine/v3.4/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.4/community/x86_64/APKINDEX.tar.gz
(1/19) Upgrading musl (1.1.14-r15 -> 1.1.14-r16)
...
(17/17) Purging libgcc (5.3.0-r0)
Executing busybox-1.24.2-r13.trigger
OK: 32 MiB in 35 packages
---> d1cc0ed0f307
Removing intermediate container 943930981ac6
Step 5/5 : CMD /bin/bash -c cd actionProxy && python -u actionproxy.py
---> Running in fc68fc0ba06f
---> 924277b2a3a0
Removing intermediate container fc68fc0ba06f
Successfully built 924277b2a3a0
Successfully tagged chanwit/whisk_c:latest

If everything was done correctly, don't forget to use the docker push command to store this image on the Hub.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset