A General Docker Tutorial
Last updated
Was this helpful?
Last updated
Was this helpful?
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.
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.
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.
Let's get started!
Navigate to https://docs.docker.com/get-docker/ to download and install the lastest version of Docker for your OS of choice.
Navigate to the IBM Cloud dashboard and click on "Catalog".
Search for the "Language Translator" service, and click the corresponding tile.
Choose to region and select the "Lite" (free of charge) plan, click the "Create" button.
You will be redirected to the service's overview page.
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.
The next steps will demonstrate on how to build a Dockerized Node.js application that provides an endpoint to translate phrases.
Open your local terminal and create a temporary directory to host the source code.
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.
To run our application as a container, issue the following command with your Language Translator API key:
For example, here's what I used:
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:
You should the following output:
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.
To stop the container you need to first find the container ID, run the following command to find it:
In the example above, the container ID is d426e0fac2eb
.
Stop the image with the following command, replacing the ID with your own.
Run the following command to remove the container, replacing the ID with your own.
You can now remove the image by running the following:
You should output similar to what is seen below:
Congratulations on creating your first containerized application!
You can check your container's logs by running:
For example...