Deploying a Node.js app on IBM Cloud

This is a quick referece for deploying a Node.js application on the IBM Cloud using the command line interface (CLI). It’s part of the IBM Cloud Essentials course at CognitiveClass.

Introduction

We start from the Node.js application source code: in this practice we will use an simple application from IBM Cloud team.

We’re using the IBM Cloud CLI that is based on the Cloud Foundry client with some specific extensions for IBM. The installation instructions for the CLI (and more documentation) are available on the IBM Cloud documentation site:

Once the CLI is properly installed, we can verify the current version:

$ ibmcloud -v
ibmcloud version 0.18.0+e00f50c-2019-07-26T15:37:40+00:00

For the rest of the practice, we must login on the service with our cloud account:

$ ibmcloud login
API endpoint: https://cloud.ibm.com
Region: eu-gb

Email> {{ your e-mail }}

Password> {{ password }}
Authenticating...
OK

Targeted account {{ your name }}'s Account ({{ ... }}) <-> {{ ... }}

API endpoint:      https://cloud.ibm.com
Region:            eu-gb
User:              {{ ... }}
Account:           {{ your name }}'s Account ({{ ... }}) <-> {{ ... }}
Resource group:    No resource group targeted, use 'ibmcloud target -g RESOURCE_GROUP'
CF API endpoint:
Org:
Space:

Tip: If you are managing Cloud Foundry applications and services
- Use 'ibmcloud target --cf' to target Cloud Foundry org/space interactively, or use 'ibmcloud target --cf-api ENDPOINT -o ORG -s SPACE' to target the org/space.
- Use 'ibmcloud cf' if you want to run the Cloud Foundry CLI with current IBM Cloud CLI context.

Check out the application

Our sample application is on GitHub; we clone the repository to checkout the source code:

git clone https://github.com/ibmecod/nodejs-cloudant.git

Resource creation

This sample application requieres a Cloudant DB database. We must create this service and bind to the application after the deployment.

Now we’re going to just create the service with service create, specifying the type of service, the plan and an unique name:

ibmcloud service create cloudantNoSQLDB Lite CloudantDB-ce

Application deployment

Let’s deploy the application. It’s important to use an unique name because the final URL is based on this name. We disable the auto-start to bind the service on the next step.

ibmcloud app push cloud-essentials-1908 -c "node app.js" -m 128M --no-manifest --no-start

Resource binding

Now we bind the database (and any other requiered resource) using the command service bind with the application and the resource names:

ibmcloud service bind cloud-essentials-1908 CloudantDB-ce

Application start

Finally, let’s start the application:

ibmcloud app start cloud-essentials-1908

The aplication is avaiable on the URL: https://cloud-essentials-1908.eu-gb.mybluemix.net/

Redeploy

After any modification, we can deploy again the application with the app push command:

ibmcloud app push cloud-essentials-1908 -c "node app.js" --no-manifest

Clean up

It we need to remove the application (and any resource), we have to use the delete command for the application and the service delete for the resources:

ibmcloud delete cloud-essentials-1908 -r
ibmcloud service delete CloudantDB-ce