> For the complete documentation index, see [llms.txt](https://drnugent.gitbook.io/openshift-microservices/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://drnugent.gitbook.io/openshift-microservices/step-3-build-and-run-a-docker-image.md).

# 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](https://www.ibm.com/watson/services/language-translator/).‌

## **Docker** <a href="#docker" id="docker"></a>

For this Lab, we will use Docker.

To download docker onto your local system, [download docker desktop](https://www.docker.com/products/docker-desktop).

{% hint style="info" %}
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.
{% endhint %}

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

## **Overview** <a href="#steps" id="steps"></a>

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!](/files/-M0QkqdjIQ3jJ1Nx-ObW)

## **Step 3.1: Create a Language Translation Service**

Open your IBM Cloud dashboard and click **Catalog**.

![](/files/-M0QlGVriRrLJifLY18F)

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

![](/files/-M0Qm4hXMoCpJYXRBJqN)

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.

![](/files/-M0QmIaDL2oQ8UPdmS0b)

{% hint style="info" %}
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.
{% endhint %}

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

![](/files/-M0QmAw4FClWG601ycsc)

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.

![](/files/-M0Ql_GrLDBw7GiAsuHu)

**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](https://hub.docker.com/).  Replace `<docker-username>` in the above command with your docker account name.

{% hint style="info" %}
If you do not have a Docker account, you'll need to create one at hub.docker.com.
{% endhint %}

*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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://drnugent.gitbook.io/openshift-microservices/step-3-build-and-run-a-docker-image.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
