Creating Docker Actions with OpenWhisk

DZone 's Guide to

Creating Docker Actions with OpenWhisk

A brief intro to OpenWhisk with a simple JavaScript example.

· Web Dev Zone ·
Free Resource

OpenWhisk is an event-driven computer platform that executes code in response to events or direct invocations. The advantage is that you only have to pay for actual usage, not for peaked projections. Developers can implement actions via JavaScript, Swift, and, as described below, Docker.

Via Docker, programming languages other than JavaScript and Swift can be used. Plus, with Docker you can write JavaScript actions that require more Node modules than are currently supported by OpenWhisk JavaScript actions.

There is documentation available describing how to use Docker in this context. When I first read it, I was a little confused because the documentation describes how to build an image with JavaScript code that calls C code. The C part, however, is only a sample and you don’t have to use anything related to C if you want to build a Docker image with JavaScript.

Here is a simple hello world JavaScript sample. The following snippet shows the file app.js and how to read input parameters and return output:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');

app.post('/init', function (req, res) {

app.post('/run', function (req, res) {
   var meta = (req.body || {}).meta;
   var value = (req.body || {}).value;
   var payload = value.payload; 
   if (typeof payload != 'string')
      payload = JSON.stringify(payload);
   console.log("payload: " + payload);
   var result = { 'result' : { 'msg' : 'echo', 'payload' : payload} }; 

app.listen(8080, function () {

Here is the package.json to which more dependencies can be added:

  "name": "openwhisk-docker-node",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "dependencies": {
    "express": "^4.11.2",
    "body-parser": "^1.11.0"
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "author": "",
  "license": ""

The Dockerfile extends the IBM Node image:

FROM registry.ng.bluemix.net/ibmnode
ADD ./app.js ./
ADD ./node_modules/ ./node_modules/
ENV NODE_ENV production
CMD ["node", "app.js"]

Invoke these commands to run the code. At this point, images need to be pushed to DockerHub:

npm install
docker build -t nheidloff/open-whisk-docker-node .
docker push nheidloff/open-whisk-docker-node
wsk action create --docker docker-node nheidloff/open-whisk-docker-node
wsk action invoke --blocking --result docker-node --param payload niklas

The dashboard shows the successful invocation.

Image title

docker, openwhisk

Published at DZone with permission of Niklas Heidloff , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}