Containerization Recipe - Create a simple http server with nodeJS and docker




It's been a while since I published my articles about docker, where I talked about the reason for the hype surrounding it, the influence it had on the way we perceive virtualisation and deploy software. I think it's time to move ahead and talk about application containerization.

What are containerized applications?

Application Containerization is a virtualisation method most commonly used to run and deploy distributed software without the need for a full virtual machine and ecosystem.
In this article, we are going to touch and feel  virtualisation by creating a basic, very simple http server that is going to run inside a docker container. 

First things first, you can't containerize what doesn't exist. So, without further ado, let's start writing out app. 

My weapon of choice for this task is Javascript that provides a fairly good and intuitive (nodeJS) module for implementing http servers from scratch. I could actually use express, but to keep things simple, let's stick to this one.
I will start by creating a new directory that will host my server.js file. Then, I will proceed to writing the code: 






A bit about the code:

This is a fairly simple piece of code written in es6 syntax. I start by requiring the 'http' module and storing it in a constant variable named http.
Then I invoke the createServer method with an anonymous function which receives a request and response as arguments. 
On request to the local address(via port 3000), we are sending our response ('Hello from our server') which should be displayed to the client. That's it. 
Look, the code is not really important, as long as you understand what we are trying to acomplish, and what is our expected result. 
Let's save our server.js file and see if it works:







So what's next?

Now we need to create the Dockerfile which will include our "recipe" for our containerised server app.
The Dockerfile is basically a set of instructions for creating our image layers with its own set of rules, syntax and best practices. The main idea is to create an environment to host our application which will include our application code and all of it's dependencies. Think about it, this way, we create a standalone piece of deployable software. 

So what will our image include?

Our image needs to include all of our code and dependencies (The same way we would need to provision a VM in order for it to be able to run our app). So, we need to list these "ingredients" in our Dockerfile which then will be built.

  • First of all we will need a small and thin OS layer for our process to run in the container. 
  • Since we are writing in JavaScript (nodeJS), we will need "nodeJs" installed. 
  • Our app code needs to find its way somehow into our container. No code - no app. Right?
  • And finally, we would need to execute the command to run our app by calling the node runtime.
I think that's it...

Now let's see how to put our set of instruction in the Dockerfile



Now, we will add a Dockerfile to the same directory as my server.js file. That, is our recipe. 

Ingredience:
  • First, we are using alpine as our OS layer with a latest tag.
  • Then, we are installing nodeJs with the apk package manager. I could obviously use a nodeJs base image, but, let's just spice it up a bit.
  • The next step is to put our code somehow inside of the container. In the following line, we are copying all of our app content to the src directory in the container.
  • The following line sets our working directory to the same directory which will host our app code (Which is /src)
  • Then, we will expose port 3000 in the container. 
  • Finally, we need to run our code which will be relative to the working directory as we defined. We will execute our program by calling the node runtime, that would be our containers entrypoint. 
Once my recipe is ready, it's time to put it in the oven.(Build our docker image)



It looks like we are ready to create a container from our freshly cooked image...




Hm... Looks good. Let's see if it worked?


I hoped you enjoyed reading this post, see you in the next one. 





Comments

Post a Comment

Popular posts from this blog

צעדים ראשונים - המדריך לבודק המתחיל חלק א

5 Ways to Fail With Your Test Automation

What's new in Test Automation? - Someone is pulling the strings once again