In my previous article I described how to use Swagger to document APIs in Node.js applications via a simple hello world sample. Below I demonstrate how to deploy the same sample to Bluemix via Docker and how to use the API Management service to enforce client ids and secrets when invoking the APIs so that API owners can monitor the usage of their APIs.
Deploying Node.js Applications to IBM Bluemix as Docker Containers
First you need to add a Dockerfile to your project’s root directory. The documentation describes how to do this. Since I don’t need SSH I use a simpler Dockerfile.
FROM registry.ng.bluemix.net/ibmnode:latest COPY . /node WORKDIR /node RUN npm install EXPOSE 9080 CMD ["node", "/node/app.js"]
Since the IBM Containers (Docker hosted on Bluemix) only support specific ports I use the port 9080 instead of 10010 as in the original sample. I also had to change the port in app.js and swagger.yaml. In order to run the application locally you need to invoke the following URLs.
To build the Docker image and to run it locally invoke the following commands from the root directory.
docker build -t node-swagger-hello-world . docker run --name node-swagger-hello-world -p 80:9080 -d -t node-swagger-hello-world
After this you can run the sample in our local Docker environment.
In order to push the image to Bluemix invoke the following commands.
cf login cf ic login docker tag node-swagger-hello-world registry.ng.bluemix.net/nheidloff/node-swagger-hello-world docker push registry.ng.bluemix.net/nheidloff/node-swagger-hello-world
To run the sample on Bluemix create a Docker container group.
After this you can run your REST API on Bluemix.
Usage of the API Management Service
With the API Management service in Bluemix you can manage and monitor your APIs. In the next part I describe how to enforce a client id and secret when invoking the API so that you can track which applications called which APIs. The API definition can be imported via pointing to the Swagger 2.0 definition above and additional settings can be configured in the dashboard of the API Management service.
In order to invoke APIs applications developers need to register applications.
Applications can subscribe to plans with the APIs they are interested in and test the APIs directly from the developer portals by providing the client ids and secrets.