Today I’d love to share how to wrap a Node microservice or monolithic application inside a Docker container. I assume that you’ve already installed Docker, boot2docker and Node in your machine.

If you don’t, please check the official Docker page and Node page and, after picking your operating system, follow the instructions. I’ve created a simple Node application with Hapi.js that once called returns the classic “hello world” message as response. Obviously you can have a way more complex application as well. My main purpose here is talking about how to setup Docker with your Node.js application.

Getting started

In order to wrap your application inside a Docker container you need to create a Dockerfile. This file basically defines the setup for the environment where your application will run. Checking the Docker containers currently available on Docker hub, you can see for Node the official container page that will allow you to pick the right Node version for your application.

As you can see there are many different Node containers available. You can also use other solutions – like using Ubuntu, Fedora or CentOS as a base operating system where your node application is going to run. However I prefer using the official one because my server side application doesn’t require any particular configuration.

For this example we are going to use the 5-onbuild that is doing the “dirty work” for us. Obviously this is a sample application but you can customise your container in whichever way you prefer. What the onbuild version is doing is basically:

  • Creating a directory of the application inside the container.
  • Copying package.json in that folder.
  • Running the “npm install” command.
  • Running the “npm start” command.

It’s fundamental to define all the dependencies and the start script inside the package.json, otherwise the application is not going to work inside the container. Inside our Dockerfile we are going to write:

FROM node:5-onbuild
EXPOSE 8080

So basically we are inheriting all of the steps described above regarding the onbuild Dockerfile. Plus we are exposing port 8080 – that is the one used by our Node application:

const server = new Hapi.Server();
server.connection({ port: 8080 });

It’s best practice for any Dockerfile to always start with a FROM command, and to reuse the images that are already created by the community on Docker hub. So remember when you want to try something different check always what’s available on Docker hub.

Good. So now let’s package our container and try if it works correctly. In order to build the container we’ll need to run the following command:

docker build -t <username>/<applicationName> .

Basically here we are saying to docker: dockerize the entire folder application (dot at the end of the command). The container will be called /. This is another Docker best practice. Potentially you can call your container what you want, but the pattern is to first add your Docker username then slash (“/”), and then your application name:

docker build -t lucamezzalira/docker-hapi .

Now lets see if it worked:

docker run -p 49160:8080 -d lucamezzalira/docker-hapi

With this command we are running our container, therefore our Node application, and mapping the port 8080 of the container to port 49160 of the host. You can easily check if the application is working correctly, just by typing docker ps in your console. You should see something like this:

So now, because I’m working on Mac, I need to retrieve the boot2docker IP and check if in the port 49160 I’m able to see my hello world application up and running. In order to do that I’ll run the command:

boot2docker ip

That should return the external IP where my application  is running. You can also see what the container is IP using the command:

docker inspect CONTAINER ID (for example: docker inspect afb5810152f6)

The container ID is easily retrievable via docker ps (the first column in the picture above). This command will return a JSON file with a lot of information related to the container.

So now that I’ve got the IP where my application is running and the related port I can type inside my bowser the address and see the result!

Obviously you can also map that IP to etc/hosts file adding something more meaningful like boot2docker or whatever name your think is more appropriate!

If you want to download the container I’ve prepared for this post from Docker hub, just type:

docker pull lucamezzalira/docker-hapi

This is a very basic introduction to Docker world and Node, I’m working on a sample microservices application that will involve few interesting concepts and patterns, so keep an eye on my blog ?

How to Dockerize your Node application

About The Author
- Solutions Architect at @dazn_de, Google Developer Expert (#GDE) and London JavaScript Community Manager @london_JS

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>