How to get a Node.js application into a Docker container?

Author: Jose
February 19, 2016
How to get a Node.js application into a Docker container?

Overview

Linuxcontainer technology that Docker looks built upon has been in the tech ecosystem since 2008. However, the sudden hype of Docker seems awaken from some clueless land of technologies. Solomon Hykes, a serial entrepreneur, hacker started using the technology Docker for his internal project for his founded company, DotCloud. Back in March 2013, Docker jailbroken as an open source and later in March 2014, the technology halted LXC as the default execution environment to replace it with its own libcontainer library written in the Go programming language.

Recently, on 4th February 2016, the very first stable version of Docker – 1.10.0 got released. As of now, the technology owns 28,774 stars, 7,999 forks, and 1301 contributors on GitHub.

What is Docker?

Docker is an open source software deployed as an extension of Linux Containers to create, deploy, and run application in a very easy manner by using containers. Don’t be confused with the term ‘container’. Containers are like packaged applications in which all needful parts like libraries and other dependencies are pre-installed, and help developers to distribute it as one package. This makes the developer to run applications on any Linux machine regardless the customized settings of it used for writing and testing the code.

Node.js application into a Docker container

Alright! So from the above context, it’s interpreted that Docker is a container technology in which containers are packed up with necessary libraries and dependencies in a standardized unit for software development. Well, developers are experimenting with many and all available technologies today incorporating them into the containers to build a ready-to-use tech-packages for their specific applications.

Following is one of such a kind of experiments that observes how to deploy a Node.js application into a Docker container. If you have a Docker installed Linux machine and are enlightened on the basis of of how a Node.js application is structured, then this guide adequately can guide you to create a simple web application in Node.js, to build a Docker image for your application, and to run the image as a container.

Creating Node.js application

First of all you have to create a new directory where you can dump all your files. The next thing is to create a package.json file in the directory create that describes your app and its libraries and dependencies.

{
“name”: “docker_web_app”,
“version”: “1.0.0”,
“description”: “Node.js on Docker”,
“author”: “First Last <first.last@example.com>”,
“main”: “server.js”,
“scripts”: {
“start”: “node server.js”
},
“dependencies”: {
“express”: “^4.13.3”
}
}

Then, using the Express.js framework, create a server.js file defining a web app.

‘use strict’;

const express = require(‘express’);

// Constants
const PORT = 8080;

// App
const app = express();
app.get(‘/’, function (req, res) {
res.send(‘Hello world\n’);
});

app.listen(PORT);
console.log(‘Running on http://localhost:’ + PORT);

Create a Dockerfile

The first thing you need to do is create an empty file called Dockerfile.

touch Dockerfile

Now, open the Dockerfile in your favorite text editor.

Then you have to specify from what image you want to build from. You can use the latest LTS (long term support) version argon of node which is available from the Docker Hub.

FROM node:argon

In order to keep the application code inside the image, you have to build a directory, which, of course, will be the working directory for your application.

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

The image comes with pre-installed NPM and Node.js. So installation of your app dependencies using the npm binary is the next thing to be done.

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

Now you need to use the ‘COPY’ instruction in order to bundle your app’s source code inside the Docker image.

# Bundle app source
COPY . /usr/src/app

You will have to use the EXPOSE instruction to have it mapped by the docker daemon, as your app binds to port 8080.

EXPOSE 8080

Define the command in order to run your app using CMD which defines your runtime. You can use the basic npm start which will run node server.js to start your server.

CMD [ “npm”, “start” ]

And below is how your Dockerfile should look like.

FROM node:argon

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 8080
CMD [ “npm”, “start” ]

Build your image

To run the following command to build the Docker image, go to the directory where you have the Dockerfile. With the -t flag you can tag your image and later you can easily find the image using the docker images command.

$ docker build -t /node-web-app .

The image you created listed by Docker.

$ docker images

# Example
REPOSITORY TAG ID CREATED
node argon 539c0211cd76 3 weeks ago
/node-web-app latest d64d3505b0d2 1 minute ago

Run the image

When you run the image with -d, it runs the container in detached mode leaving the container working in background. Meanwhile, the flag -p redirects a public port to a private port inside the container. Now you have to run the image you previously built.

$ docker run -p 49160:8080 -d /node-web-app

Print the output of your app.

# Get container ID
$ docker ps

# Print app output
$ docker logs

# Example
Running on http://localhost:8080

Use exec command, if you want to go inside the container.

# Enter the container
$ docker exec -it /bin/bash

Test

The first thing you need to do is get the port of your app that Docker mapped to run the test of your app.

$ docker ps

# Example
ID IMAGE COMMAND … PORTS
ecce33b30ebf /node-web-app:latest npm start … 49160->8080

You can see in the above image that Docker has mapped to the port 8080 inside the container to the port 49160 on your machine.

$ curl -i localhost:49160

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 12
Date: Sun, 02 Jun 2013 03:53:22 GMT
Connection: keep-alive

Hello world

Now all you have to do is call your app using curl. In case if you haven’t installed curl, install it via sudo apt-get install curl.

I hope this tutorial will transfer you the vital knowledge to running a simple Node.js application on Docker. Now let’s discuss what’s the core benefits of using Docker.

Why Docker?

We have been discussing what Docker, its history and the tutorial of dockerizing a Node.js web app. But still the question is left – why Docker? What are its benefits for software development ecosystem. Certainly with enabling technology of Docker, it provides a consistent environment at low overhead. Let’s talk about further in the following.

1. Simplifying Configuration

This is the prime benefit of using Docker which simplifies configuration. It lets you run any platform with its own configuration on top of your infrastructure. The real twist in the tale is that Docker enables the freedom to run your applications across multiple IaaS/PaaS without any extra tweaks.

2. Developer Productivity

Scalability in productivity is the key of every succeeding organization. Whatever product or strategy that gives a real boon for developer productivity is a great tool. Docker is one of such an artefact that helps developers to give the best output from concept and at a faster pace in production.

3. Rapid deployment

You would probably say these containers are more than awesome. In olden days before the times of Virtual Machines, you had to work with hardware resources to bring out each and every application which ridiculously took days. Later with the arrival of virtualization, this days were brought down to minutes of work. And now, all you have to do can be done in seconds with Docker by creating just a container for the whole process and not booting up an OS.

4. Server Consolidation

The more sturdy server, the more greasy is the dribbling performance the application can enable. Virtual Machines are used to consolidate multiple applications. Meanwhile, the application isolation abilities of Docker consolidate multiple servers which is cost effective. At the same time, Docker doesn’t need memory footprints of multiple OSes. It has the incredible functionality to share unused memory across the occasions which qualifies a thicker server consolidation than you can get with virtual machines.

Winding up

Broad is the use cases and functionalities of Docker. Biggies like Google, Amazon have already adopted the technology to their garage. It’s you the developers will have to do the rest contributing a good hell of time for a better development culture at least for the coming generation. I’m sure many of the ones who read this article do know more than I’ve enclosed here. If you know anything, please share your views in comments below