Step 3: Build and Run a Docker Image

In this step, we'll create a containerized Node.js application that provides a service to do on-the-fly language translation. This service can later be deployed into an infrastructure composed of microservices, if desired. The application uses the IBM Language Translation service.‌

Docker

For this Lab, we will use Docker.

To download docker onto your local system, download docker desktop.

Docker container technology separates applications from the underlying Operating System and infrastructure, which is similar to older Virtual Machine technology that separates an operating system from the bare metal / server hardware.

Thanks to Docker containers, only the application, and specific dependencies like libraries and binaries are being packaged in the image.

Overview

First, we will create the Language Translation service and record the API Key to access your service later. Then we will create a node.js based microservice. This microservice will respond to requests with results of the translations coming from IBM Watson. As soon as you are ready with the microservice you will be able to start the Build - Ship - Run containerization process. You will build an image, and push it to a public repository (Docker Hub) and run the containerized microservice.

Build / Ship / Run!

Step 3.1: Create a Language Translation Service

Open your IBM Cloud dashboard and click Catalog.

Search for translator to find the appropriate service. (You can also find the service by navigating to the AI section on the left bar.)

Click on the service to create a new instance. Pick the Lite free of charge plan on the next page and click Create to finish creating the service.

Is your Create button greyed out? You may still be in the temporary account that contains your OpenShift cluster. You'll need to switch back to your own account in order to create new services, using the pull-down menu in the upper-right of your screen, between the Manage menu and the Cloud Shell icon.

Next, we'll copy down the credentials for the new service that we've created. Click Service credentials in the left hand menu:

If you do not see a credential provided for you, you can create a new set of credentials. Save your apikey somewhere for the next section in this workshop.

Congratulations! You created your first Language Translator service. The next steps will show you how to build a Docker container for a Node.js application that provides an end point to translate text!

Step 3.2: Build a Docker Image

The cloud-hosted language translation service isn't very interesting by itself; we'll need to write a microservice for our application to call the language translator and get back the output. Luckily, we've written a microservice for you in a GitHub repo; all you have to do is package it into a Docker container.

Open your local command line and change to the /data directory.

mkdir data
cd data
git clone https://github.com/lidderupk/nodejs-docker.git

Now that we've cloned our repo, we'll build a docker image:

cd nodejs-docker
docker build -t <docker-username>/node-container .

The docker-username is required if you want to publish your image to Dockerhub. Replace <docker-username> in the above command with your docker account name.

If you do not have a Docker account, you'll need to create one at hub.docker.com.

Alternatively, you can also build directly from github using the following command without cloning the repository:

docker build -t <docker-username>/node-container https://github.com/lidderupk/nodejs-docker.git

Congratulations, you've created a docker image! Now, let's run it:

Step 3.3: Run the Docker Image

docker run -p 8080:8080 -e "nlp_key=<api_key>" -d <docker-username>/node-container

Remember to replace <api_key> and <docker_username> with your respective credentials.

Once your container is running, we can test the application:

curl "localhost:8080/translate?text=how%20are%20you"

You should see output as follows:

{
  "translations": [
    {
      "translation": "¿Cómo estás?"
    }
  ],
  "word_count": 3,
  "character_count": 11
}

Congratulations, your microservice is now running in a Docker container and pinging a IBM Watson cloud language translation service! Next, we will focus on deploying containers into a Red Hat OpenShift environment.

Last updated

Was this helpful?