Saturday, December 22, 2018

Getting started with Node and express using Docker labs

Introduction

For writing web applications we need server middle-ware that can run the business logic and expose the same as web services. Webservices are backbone of any modern web application. Modern webservices are lightweight webservices that follow REST paradigm as against legacy SOAP webservices which are heavy. The REST webservices make use of the HTTP protocol aspects like HTTP Verbs and HTTP Headers. Further the modern REST webservices use light weight JSON data format as against heavier XML data format used by legacy SOAP webservices. Thus REST webservices are lightweight and easier to implement than SOAP webservices.

Node.js is a server side programming platform based on Javascript for creating webservices. It uses Google V8 Javascript engine to run the javascript code. Compared to Java based server middleware like Tomcat, Spring Boot et-cetera, the Node.js is a much more lightweight platform. Further developing web services in Node.js platform is much more simpler than developing in complex Java based platforms.

Express.js is a framework that further simplifies the programming in Node.js. By using Express.js we can easily develop web services. In this tutorial we shall proceed to familiarize with Node.js itself using the docker shell. Finally we shall proceed to creating a simple Node.js + Express.js service.

Task 1: Play with Node.js in docker


  1. Go to Docker labs https://labs.play-with-docker.com
  2. Login using Docker credentials.
  3. In labs home page, Open a new docker instance by clicking the "add new instance" hyperlink.







  4. In the main pane a docker instance shell shall be opened.
  5. In the shell type the following command to run a Node.js image in interactive mode.

    docker run -it node:10-slim node

    Note: For full command line reference of docker command, check docker document reference
  6. Once the node based docker image initializes, an interactive node shell shall be presented.
  7. In the node shell one can play with various Javascript expressions.

    > 1 + 2
    (Prints 3)

    > message = 'Hello Word'
    > console.log(message)
    (Prints "Hello World")

Task 2: Create a simple Hello World service

  1. Follow the first 4 steps describe above in task 1
  2. In the shell type the following command to run a Node.js image in interactive mode. In addition expose the webservice port by using -p command line option. Once the below command is run a blue hyperlink shall appear, which is link to the exposed port.

    docker run -it -p 8080 node:10-slim node











  3. In the node shell type/copy paste following script and press enter

    var http = require('http');http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World!!!');}).listen(8080);

    The formatted code looks like the following:

    var http = require('http');
    http.createServer(function (req, res) {
         res.writeHead(200, {'Content-Type': 'text/plain'});
         res.end('Hello World!!!');
    }).listen(8080); 

  4. Click the blue hyperlink as highlighted above. It opens a new tab with output of the code above, which is "Hello World!!!"
Now lets see how this works. As evident in step 2, First we exposed a docker port using -p command line option, which docker logs conveniently exposed to us as a hyperlink. At this point we shall get an error in the browser if we click the hyperlink. The reason is that though we expose the port, there is no Application application to recieve the HTTP packets and send a response.
Hence we move on to step 3, where in we use a Javascript to listen to the port (8080) exposed in docker, and then respond on recieving a http request. Each time when a HTTP request is recieved in Port 8080, the Node server invokes the function(req, res).  Consequently the code  writes a plain text response "Hello World" back to the browser as HTTP response using the response object passed param res.

Task 3 Create above service using Express.js

Since there is no ready-made Docker image with express pre-installed, we shall use a docker image with Node as above. Then we shall use the Node's npm command to install Express. Then we shall run the express based Javascript. The complete steps as below:

  1. Follow the first 4 steps describe above in task 1
  2. In the shell type the following command to run a Node.js image in interactive mode. In addition expose the webservice port by using -p command line option. Once the below command is run a blue hyperlink shall appear, which is link to the exposed port.

    docker run -it -p 8080 node:10-slim bash

    Here the we use the bash shell instead of Node shell since we need to first install Express.
  3. Now in the bash shell type the following

    npm install express
  4. Now in the bash shell, to open the Node interactive shell type the following

    node
  5. In the node interactive shell copy paste the following javacript code.

    const express = require('express'); const app = express(); const port = 8080; app.get('/', (req, res) => res.send('Hello World!!!')); app.listen(port, () => console.log(`App listening on port ${port}!`));

    The actual code looks as below:

    const express = require('express');
    const app = express();
    const port = 8080;
    app.get('/', (req, res) =>
         res.send('Hello World!!!'));
    app.listen(port, () =>
         console.log(`App listening on port ${port}!`));

  6. Click the blue hyperlink exposed by Docker. It opens a new tab with output of the code above, which is "Hello World!!!".

Tuesday, September 11, 2018

A simple docker guide for dummies - Part 1

1. Overview

Docker is a virtualization technology which is used to bundle and run our applications. As against hardware level virtualization offered by likes of VMWare, Virtualbox, AWS, docker offers a light weight alternative. The reason is that in container virtualization technology, which is used in docker, we make use of underlying OS and add a thin layer of application virtualization over that. In a nutshell we need not have entire OS stack in the virtual image.

In this guide we shall look into docker in a hands-on way, so we shall need a docker environment. We can easily install docker in Linux, Mac and Windows 10 OSes. For Windows 8/7 there are alternatives available.

The best part is that we need not have docker installed to play with docker. Yes, we have the convenience of playing with it, from our browser. All we need to do, is to create a free docker hub account. Using the account we can login to online docker "lab"  - https://labs.play-with-docker.com
In the web application click "+ Add new instance" to open a new shell with docker environment enabled. We can now play with the environment.

For running docker we need docker image. Images are obtained from Docker repository which is similar to a GIT repository. It has same concepts of local repo and global repo. So one is comfortable with GIT, they will be comfortable with Docker repo concepts too.
We can use a ready-made docker image or else build our own, from scratch or else from other images (Note: Even to build from scratch we can use a docker image called “scratch” !). To build a docker image,

  1. Create a docker file (Docker defaults the filename of same to Dockerfile)
  2. Build a docker image with aforementioned docker file. Docker shall automatically pull dependent images from remote global repo (if it is not found cached in local repo).
  3. Once built the image is pushed to the local repo.
  4. Optionally publish the image to Docker central repo (docker hub) using docker account.
  5. If pushed to global repository (Using one’s docker account) it can be pulled from anywhere into local repo.
  6. To run the image, instruct docker to run specific image from local repo. The image is not found in local repo, it will pull the image from central repo.

2. Docker Image

2.1. Build a docker image

We can build docker image from a docker file. A simple docker file looks as below


FROM alpine
COPY hello.txt /hello.txt
RUN echo "Hello from RUN" >> /hello.txt
ENTRYPOINT ["cat", "/hello.txt"]  

The explanation of above docker file is as below
  • FROM alpine :  Build custom image from base image alpine (A lightweight linux distribution)
  • COPY : Copy from host file system into docker image (Note: We need to create the file in host system - As explained below)
  • RUN: Run a built-in command in temporary image. The changes affected by the command shall reflect permanently in docker image built.
  • ENTRYPOINT : Instructs the built image to execute a command at startup.
Now that we understand the basic structure of Dockerfile, lets do a hands-on in docker labs shell. Create a docker labs shell as explained in Overview section above. Once we have our Docker labs shell ready in the browser, let's create both Dockerfile and hello.txt. To achieve that, copy the following in Docker bash shell and press enter key.

# Create a hello.txt to be copies by docker COPY command below
echo "Hello pre-generated in host" >> hello.txt
# Create the Dockerfile to build docker image
cat >> Dockerfile << EOF
FROM alpine
COPY hello.txt /hello.txt
RUN echo "Hello from RUN" >> /hello.txt
ENTRYPOINT ["cat", "/hello.txt"]
EOF

Then build using docker build command

docker build –t <tagname> <directory_containing_Dockerfile> 

Assuming that Dockerfile is in current directory (.), 

docker build -t MyDockerImage .

The above command shall create a new custom image with tag name MyDockerImage in local Docker repository.

2.2. Tag a Docker image

One can optionally tag existing docker images with another tag name. This is useful if we are to have a simple tag name for local image and proper repo tag name (reponame/tagname) for the image to be published to our repo.


docker tag <existing_tag_in_repo> <repo/tagname>

In our example lets tag image built as described in section above

docker tag MyDockerImage mydockerrepo/MyDockerImage

In the above example, mydockerrepo is the name of docker account we are to create with docker hub.

2.3. Push local image to central repo

One can optionally push the docker image to central repository so that it can be accessed from anywhere. Docker offers 1 private repo and unlimited public repos. To push the docker image from local repo to central repo

Login to the central repo


docker login

Use docker push command.

docker push <repo-name/image-name>

E.g. 


docker push mydockerrepo/MyDockerImage

The docker image once pushed can be pulled to local repository using docker pull.


docker pull mydockerrepo/MyDockerImage

The image is also automatically pulled when we use docker run command to run an image.

3. Docker Container

The docker container is used to run a docker image.

docker run mydockerrepo/MyDockerImage

 The above command spawns a new docker container that runs the “mydockerrepo/MyDockerImage”

The docker containers can be listed using docker ps command. 

To list active containers :


docker ps

To list all containers:


docker ps -a

We can use docker ps command in combination with docker rm to remove all docker containers.

docker rm $(docker ps -a -q)

or else using xargs command


docker ps -a -q | xargs docker rm



To be cont...

Sunday, November 8, 2015

Solving the wildcard SSL certificate problem

The best thing to happen with SSL certificates is the introduction of wildcard certificates. This provides the site Administrators the ability to have as many sub-domains as they wish, without going for separate certificate for each one. Very convenient and cost effective indeed.

However this convenience comes with a cost - Security threat. If some hacker manages to get hold of the private key, then the hackers can create their own valid sub-domains using the certificate. The sub-domain can then be used as launchpad for malicious attacks by hosting malicious scripts. The hackers could also compromise the webserver or reverse proxy of the host to achieve same results. This is a big issue indeed!

The fore said issue could be solved by adding a WELL_KNOWN_SERVICE URL, which the browsers shall consult to get list of valid sub-domains. For example, *.yoursite.com certificate could have a WELL_KNOWN_SERVICE URL wksvc.yoursite.com. All a site admin would have to do is to update the list exposed by the service. The WELL_KNOWN_SERVICE could be a REST service with JSON binding. A better workaround is to allow domain web-masters to generate their own private keys, just like Digicert.com does.

Another possible attack is having a long legitimate looking sub-domain. For example, for many users, www.citybank.coo.in would look like a legit site, except that the site doesn't belong to Citybank but Coo.in! The solution for this is for the browsers to ensure that the wildcard domain appears in the primary area of browser URL bar. Further it should be highlighted in bold and a background colour. For example, it could look like www.citybank.coo.in/mypayments. With coo.in properly highlighted, users could easily understand that the site belongs to coo.in and not City Bank!


Saturday, July 6, 2013

The Quantum Universe(s) - A simple explanation of Multiverse!


Imagination is a way to extrapolate and see beyond what we actually see. For example more than two thousand years back, Sage Agasthya was able to describe a DC battery cell! Often our imaginations are a potent mix of existing knowledge and a extrapolation of the existing knowledge. Okay, enough of Philosophy!

The Universe as we know, is made of galaxies, yes billions of them. But is there only a single universe that exist? What if there co-exist more than one universe? Co-existence? Let us list some related information which we are already know.
0. Quantum theory states about dual behavior of matter - Particle state and wave state
1. Materials are made of matter
2. Materials co-exist in this universe, and are separated by a dimension we call distance in X, Y & Z.
3. In radio communication, multiple signals can co-exist in time (TDMA/CDMA ....)

Now let us do a tricky extrapolation! What if we have multiple universes co-exist all separated by time domain; At some sub-particle realm, what if it is just like TDMA? Why not? If more than one radio signals can co-exist just divided by time, why can't there be universes that co-exist, at a sub particle realm which we are not aware of?

If that indeed is a case, that opens up a wide range of possibilities. Imagine the possibility of meeting a Alien civilization right here from where we are though the probability may be very less!Imagine traveling billions of kilometers - from today's perspective just in a blink of eye! Imagine us doing commerce with a civilization that is in that parallel universe? Wow!

Getting started with Node and express using Docker labs

Introduction For writing web applications we need server middle-ware that can run the business logic and expose the same as web servic...