To help you understand the basics of Docker technology we have created a tutorial where you will build and run a Node.js application with Docker. The application will translate phrases from one language to another by using Watson's Language Translator service.
The Docker architecture - containers are separated from the OS
Docker container technology separates applications from the underlying Operating System and infrastructure, which is an analog to VM technology that is separating an operating systems from the bare metal - server hardware.
Docker technology emulates the Operating System (OS), making it possible to containerize only the application and dependencies, like libraries and binaries, by being packaged in an image. Running an image is much faster as now the OS is emulated. In addition, the image is now portable and can be shared between services.
Creating your first containerized application with Docker
In our tutorial, you'll be given the source code for the sample application, but to make it useful we'll need to provde an API key for the Language Translator service. Once we have have the API key we'll update the source code, containerize the application, run it, and test a few phrases.
A Docker pipeline is about building, shipping, and running containers
Search for the "Language Translator" service, and click the corresponding tile.
The "Language Translator" tile
Choose to region and select the "Lite" (free of charge) plan, click the "Create" button.
Choose the "Lite" plan
You will be redirected to the service's overview page.
2. Copy the Language Translator API key
From the service's overview page, choose the "Service Credentials" option on the lefthand navigation bar.
A credential containing an API key should be automatically create but if you do not see one, you can create a new credential. Save the API key somewhere for the next section in this workshop.
Save the API key for future reference
The next steps will demonstrate on how to build a Dockerized Node.js application that provides an endpoint to translate phrases.
3. Clone the source code
Open your local terminal and create a temporary directory to host the source code.
4. Build the application with Docker
To build the application with Docker run the following:
This command uses the Dockerfile in the base directory to download a Node.js 10 base image and install our application on top.
Let's explore the contents of the Dockerfile ...
... builds our image on top of the official Node.js 10 image.
... creates a working directory for our application to live in.
... copies the source's package.json file to our working directory.
... installs our dependencies as defined in our package.json.
... copies the rest of our source code into the working directory.
... exposes port 8080.
... starts the application.
5. Run the Docker image
To run our application as a container, issue the following command with your Language Translator API key:
For example, here's what I used:
6. Test the application
You should the following output:
The text is translated to Spanish by default. You can specify the langauge by passing in other language flags, for example:
cd ~
mkdir openshift-workshop
cd openshift-workshop
git clone https://github.com/IBM/node-docker-language-translation
cd node-docker-language-translation
docker build . -t translator:v1
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
docker run -p 8080:8080 -e "lt_key=<api_key>" translator:v1
docker run -p 8080:8080 -e "lt_key=T1ReDZISYE4cpqQnQHKTWe1F9iUy6hhxkRu0aWqzmxQ3" translator:v1
{
"translations": [
{
"translation": "Dowiedz się więcej o otwartej technologii"
}
],
"word_count": 6,
"character_count": 34
}
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d426e0fac2eb translator:v1 "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:8080->8080/tcp strange_northcutt
$ docker logs 4450279a9f50
Running on http://0.0.0.0:8080
No language passed to translate to. Converting to Spanish by default.
{
"translations": [
{
"translation": "Hola"
}
],
"word_count": 1,
"character_count": 5
}