How to Dockerise a Node.js application

Tutorial Difficulty Level    

In this article we’ll show you how to create and deploy a Docker image for an application you have writen written in Node.js.

As this is primarily a docker tutorial, we will keep our Node.js application nice and simple, just a standard “Hello World” app.

By adding your application as a Dockerfile you gain a lot – you can launch a fully capable development environment on any computer supporting Docker; you don’t have to install libraries, dependencies, download packages, mess with config files etc. The working environment of the application remains consistent across the whole workflow. This means the app runs exactly the same for developer, tester, and client, be it on development, staging or production server.

To get started, fire up a Docker Host (with Dockly) Quick Instance from http://xoa.comp.dkit.ie

Our application will use the Express Framework, one of the most popular Node.js frameworks. Create a new directory and initialize npm:

When asked for the details of the application (name, version, etc.), just confirm the default values with enter.

Npm will create a package.json that will hold the dependencies of the app. Let’s add the Express Framework as the first dependency:

The file should look like this now:

With everything installed, we can create an index.js file with a simple HTTP server that will serve our Hello World website:

The application is ready to launch:

Go to port 8081 on the IP of your VM in a browser to see the output. Next, we move on the the Docker container part.

Contrary to VMs,  the container doesn’t hold the whole operating system – just applications, dependencies, and configuration. This makes Docker containers much lighter and faster than regular VMs.

As you know by now, the Docker container is launched on the basis of a Docker image, a template with the application details. The Docker image is created with instructions written in the Dockerfile. Let’s add our Dockerfile to the directory with our application:

With the instructions ready all that remains is to run the command docker build, set the name of our image with -t parameter, and choose the directory with the Dockerfile:

The application has been “baked” into the image.  Now execute the following string to run the container and publish it on the host with the same port 8081:

-t is important so we can exit by pressing CTRL+C.

Happy? We are. Let’s tag our image for storing in the DkIT Registry so we can use it again later. Previously we would have done this with

but we are now going to introduce the idea of namespaces, which boils down to keeping all our Dockerfiles under the one “branch” of the registry, much like a directory structure. This is done with something like:

and so you would push with

The result is as follows:

Which makes a lot more sense when you think long term, as eventually a student will have LOTS of Docker images:

and each image could have LOTS of tags:

Using namespaces keeps all your images together and decreases the chances of you or somebody else accidentally “overwriting” (we use this term very loosely) one just because it has the same name.

As the Registry does not ask for authentication, we are using an “honour system” here where students are not expected to ever use someone else’s namespace. This system also applies to tagging images for the DkIT Registry from inside https://docker.comp.dkit.ie