Docker for Node.js Development

docker.png

Docker containers are the coolest! If you haven’t had the blessing of working with them you’ve been missing out. In brief they are an abstraction layer (like virtual machines) on top of your linux kernel. So you can run isolated processes and file systems on the same machine with a very little computing resource footprint. For a cool introduction check out this video on youtube. Using containers for software development has MANY benefits that I won’t list in this post. I encourage you to read A LOT about the subject.

In this post I’ll show you how to get your node.js environment up and running on a Docker container. I won’t show how to install Docker since that would just be a copy/paste from the official documentation which is already very good. For detailed instructions on how to install and verify it was done correctly on a MAC go here. If you’re running another OS you will find the documentation in that site, if it’s supported.

Once that’s done then there are a few thing you need to do to be ready to kick ass in your DevOps.

Add environment variables

By running the command boot2docker shellinit you’ll get certain environment variables that you need to set. Once you run the command copy the output and paste it in the file ~/.bash_profile which should end looking something like:

# Adding Docker variables export DOCKER_CERT_PATH=/Users/someuser/.boot2docker/certs/boot2docker-vm export DOCKER_HOST=tcp://192.168.59.103:2376 export DOCKER_TLS_VERIFY=1

This is so you can use boot2docker without having to export the variables for every single new terminal session you open.

Set the docker IP in the hosts file

I’m assuming that you know what the hosts file is for and how does it work. Docker uses a linux virtual machine to run (in the background) on a MAC. Type boot2docker ip to get the IP address of the VM. To be able to access our web application easily we cd ~/ and then we open the file /private/etc/hostsand add the ip we got from the previous command along with the host name we want to use (in my case I used docker).

Get Docker File

Next you’ll want to clone my git repo which has the Docker file necessary to build a Docker image specifically for Node.js development on Ubuntu.

# I like to clone it to the home directory but you can put it anywhere cd ~/ # clone repository with Docker files git clone https://github.com/ccerrato147/dockerfiles.git # go to the Docker file cd docker/UbuntuNode # verify docker file is there. the output should be "Dockerfile" ls

Then open the Dockerfile. In the file there are comments (lines that start with #) that explain every single command. For all the instructions that can go in a Docker file look at the Dockerfile Reference.

Build the Docker Image

No to build the Docker image from the file execute the following commands by replacing “toji/fe2” with the repository/image name pair that you want to use.

docker build -t toji/fe2 .

Several commands will start to execute and you just have to wait. After it completed successfully then you will verify your image was created by running the command

docker images

In which the name you assigned in the previous command should be listed, for example toji/fe2 as was the case for me. If there were errors then the image will be created but it won’t a name.

Now it’s time for the fun part!

Run the Docker Container

We’ll run our node.js application in the container by mapping a folder from our host (in my case my MacBook) to a folder in the Docker container. I’m assuming you already have a node.js application that you want to run and also that you are familiar with how node.js works. If not you can clonedocker-node-hello which is a simple node.js hello world application.

In the instructions of the Dockerfile there was one that created a folder called Toji. This is where I’ll be mapping the node.js application directory but you can name it however you want.

Run the command if you want leave the container running in the background

docker run -it -d -P -v $HOME/Projects/docker-node-hello:/home/Toji --name RunningContainer toji/fe2

Replace $HOME/Projects/docker-node-hello for the directory in you host where you have your node application.

You could have also run it and entered it with bash at the same time using

docker run -it toji/fe2 bash

The difference is that in the second case we run and access the Docker container at the same time. But also, if we type exit then the container is no longer running. In the previous command the -d allows it to execute in the background. Once it’s running type the command docker ps and take note of the por mapping because the port we exposed on the Docker container (8087) is mapped to another port on you host machine (32777 in my case).

you can attach to it by using

docker exec -it RunningContainer bash

Executing the node.js

Now you should be inside the context of the container. So cd into/home/Toji and run npm install to download all the node modules. Now we can finally run node index.js (or the file that is particular for your node application) and then open a browser and use docker:32777 and we should see “hello world” appear in the browser. docker:32777 are the name we assigned in the hosts file to the boot2Docker virtual machine and 32777 is the map port I am getting from the port 8087 that the container is using internally.

I hope everything worked out okay for you and if you need any help let me know in the comments or email me at ccerrato147@gmail.com and I’ll be happy to work with you.

Have a great time and don’t forget to be awesome!

Originally published at blog.carloscerrato.com on April 30, 2015.