Lab 1 - Start with a Docker Image
This Lab will help you understand basics of Docker technology, you will build, ship, and run a container image.
Last updated
Was this helpful?
This Lab will help you understand basics of Docker technology, you will build, ship, and run a container image.
Last updated
Was this helpful?
This workshop is an introduction to Docker, which is a runtime for containers. You will create a containerized Node.js application that provides a service to translate phrases from one language to another. The application uses the IBM Watson in IBM Language Translation service.
For this Lab you will use Docker.
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 virtualizes the Operating System (OS), and thanks to it only the application, and specific dependencies like libraries and binaries are being packaged in the image.
Starting such an image is much faster omitting start of an OS. In addition the image now is very portable among various host servers running the Docker engine. And since ther is no OS, there isn't its surface for security vulnerabilities connected to it.
The following steps would allow you to create the Watson translation service in the cloud. You will record the API Key to access your service later. You will create a node.js based microservice. This microservice will respond to requests with results of the translations coming from IBM Watson service. As soon as you are ready with the microservice you will be able to start Build - Ship - Run containerization process. You will build an image, and push it to a public repository - Docker Hub, and run the containerized microservice.
Open your IBM Cloud dashboard using your IBM Cloud account with this URL: : http://cloud.ibm.com .
Search translator to find the 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.
You will be redirected to the service landing page.
Click on Service Credentials on the left bar.
If you do not see a credential provided for you, you can create a new set of credentials. Save your apikey and url values 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!
Open your local terminal and clone the reposiotry with the example (you might want to use git, or simply download the zip file from the following location https://github.com/IBM/docker-nodejs-language-service ).
Change into the directory you just cloned and build the docker image
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 would need to create one directly on hub.docker.com - choose sign up button.
Alternatively, you can also build directly from github using the following command without cloning the repository:
This command uses the Dockerfile to download a Node.js 10 base image and then install our Express.js application on top.
Let's explore the contents of this Dockerfile
:
FROM node:10
... builds our image on top of the Node.js 10 image.
WORKDIR /usr/src/app
... creates a working directory for our application to live in.
COPY package*.json ./
... copies the package.json file to our working directory. RUN npm install ... install our dependencies. We just have two dependencies in this application: express and ibm-watson.
COPY . .
... copy the rest of our source code into the docker image
EXPOSE 8080
... expose port 8080. We will still have to forward our local port to this docker container port later.
CMD [ "node", "server.js" ]
... starts the application by running node server.js.
In my case, I would run
You should see output as follows:
If the command returns error curl: (7) Failed to connect to localhost port 8080: Connection refused - check logs:
(in my case this command looks like this
docker logs 7f72ca211fffbf8de355b53e197e9213ecc5481e0b6045f82b420dcbb0a4e78e
If the log says: Identifier 'PORT' has already been declared
you might want to comment the offending line. Edit it with vi:
vim server.js
add comment, ie. // before start of line 11, it should look like this:
//const PORT = process.env.PORT || 3000;
In addition you might want to stop and delete the image, then rebuild it - going back to step 4. Ask your instructor if you have any issues with that. Use the following commands:
The text is translated to Spanish (en-sp)
by default. You can specify the langauge by passing in the lang flag as follows:
You should now see the same text translated to German:
Another example ...
in Polish
You can see the supported languages (both from and to)
in the Language Translator documentation.
Congratulations! You just containerized a Node.js application that provides transation services.
You can first stop the container. You need the container tag or the id to stop it. Let's look it up first
In my case, the container is called cranky_davinci
and has an id of 419104eff9be
.
Stop the image with the following command. You can replace the id with your container id.
Run the following command to remove the container. Replace the id with your container id identified in the step above.
You can now delete the image. You again need the image id.
Now, delete the image as follows.
You can check the logs for your container using
For example ...
Congratulations again on creating your first docker container that hosts a Natural Language Translation service!
In order to read and learn more about Docker - please use this link: http://blumareks.blogspot.com/2019/09/basic-docker-functionality-containers.html
Please claim your pre-provisioned cluster, and set up your environment for the next labs.